java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 16:28:49 +0200
changeset 67 96ce967c0182
parent 55 c703fb7f088f
child 98 944a81a54bb9
permissions -rw-r--r--
cli: support --input-url and multiple names per option (might be used for short variants like -i, -a etc.)
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.cli;
    19 
    20 import java.io.File;
    21 import java.util.Arrays;
    22 import java.util.Collection;
    23 
    24 /**
    25  * Converts command line arguments from String array to object.
    26  * Checks basic constraints (if only supported options are used and if they have correct number of
    27  * parameters)
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 public class CLIParser {
    32 
    33 	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    34 		CLIOptions options = new CLIOptions();
    35 
    36 		for (int i = 0; i < args.length; i++) {
    37 			String arg = args[i];
    38 
    39 			boolean matches = false;
    40 
    41 			for (Token t : Token.values()) {
    42 				if (t.matches(arg)) {
    43 					int parsedArgs = t.parse(args, i, options);
    44 					i = i + parsedArgs;
    45 					matches = true;
    46 				}
    47 			}
    48 
    49 			if (!matches) {
    50 				throw new CLIParserException("Unknown option: " + arg);
    51 			}
    52 		}
    53 
    54 		// Default output:
    55 		options.setOutputStream(System.out);
    56 
    57 		return options;
    58 	}
    59 
    60 	private static String fetchNext(String[] args, int index) throws CLIParserException {
    61 		if (index < args.length) {
    62 			return args[index];
    63 		} else {
    64 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
    65 		}
    66 	}
    67 
    68 	private static enum Token {
    69 
    70 		INPUT_FILE("--input-file") {
    71 					@Override
    72 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    73 						int originalIndex = index;
    74 						options.setInputFile(new File(fetchNext(args, ++index)));
    75 						return index - originalIndex;
    76 					}
    77 				},
    78 		INPUT_STDIN("--input-stdin") {
    79 					@Override
    80 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    81 						options.setInputStream(System.in);
    82 						return 0;
    83 					}
    84 				},
    85 		SYSTEM_ID("--system-id", "--input-url") {
    86 					@Override
    87 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    88 						int originalIndex = index;
    89 						options.setSystemId(fetchNext(args, ++index));
    90 						return index - originalIndex;
    91 					}
    92 				},
    93 		READER_PROPERTY("--reader-property") {
    94 					@Override
    95 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    96 						int originalIndex = index;
    97 						String name = fetchNext(args, ++index);
    98 						String value = fetchNext(args, ++index);
    99 						options.addReaderProperty(name, value);
   100 						return index - originalIndex;
   101 					}
   102 				},
   103 		ACTION("--action") {
   104 					@Override
   105 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   106 						int originalIndex = index;
   107 						options.setAction(fetchNext(args, ++index));
   108 						return index - originalIndex;
   109 					}
   110 				},
   111 		ACTION_PROPERTY("--action-property") {
   112 					@Override
   113 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   114 						int originalIndex = index;
   115 						String name = fetchNext(args, ++index);
   116 						String value = fetchNext(args, ++index);
   117 						options.addActionProperty(name, value);
   118 						return index - originalIndex;
   119 					}
   120 				},
   121 		ACTION_DATA("--") {
   122 					@Override
   123 					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   124 						int originalIndex = index;
   125 						for (index++; index < args.length; index++) {
   126 							options.addActionData(args[index]);
   127 						}
   128 						return index - originalIndex;
   129 					}
   130 				};
   131 
   132 		private final Collection<String> options;
   133 
   134 		private Token(String... options) {
   135 			this.options = Arrays.asList(options);
   136 		}
   137 
   138 		/**
   139 		 * @param option e.g. „--input-file“
   140 		 * @return whether option is this token
   141 		 */
   142 		public boolean matches(String option) {
   143 			return options.contains(option);
   144 		}
   145 
   146 		/**
   147 		 * Parse String arguments and fill values into the options object.
   148 		 *
   149 		 * @param args CLI arguments
   150 		 * @param index index of the option matched by this token, like „--input-file“
   151 		 * @param options object to be filled
   152 		 * @return number of parsed arguments – if option has no arguments (just boolean flag),
   153 		 * return 0, otherwise return positive integer: number of eaten arguments.
   154 		 * @throws CLIParserException
   155 		 */
   156 		public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;
   157 	}
   158 }