3 * Copyright © 2014 František Kučera (frantovo.cz)
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml.cli;
20 import java.util.Arrays;
21 import java.util.Collection;
24 * Converts command line arguments from String array to object.
25 * Checks basic constraints (if only supported options are used and if they have correct number of
28 * @author Ing. František Kučera (frantovo.cz)
30 public class CLIParser {
32 public CLIOptions parseOptions(String[] args) throws CLIParserException {
33 CLIOptions options = new CLIOptions();
35 for (int i = 0; i < args.length; i++) {
38 boolean matches = false;
40 for (Token t : Token.values()) {
42 int parsedArgs = t.parse(args, i, options);
49 throw new CLIParserException("Unknown option: " + arg);
54 options.setOutputStream(System.out);
59 private static String fetchNext(String[] args, int index) throws CLIParserException {
60 if (index < args.length) {
63 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
67 private static enum Token {
69 INPUT_FILE("--input-file") {
71 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
72 int originalIndex = index;
73 options.setInputFile(new File(fetchNext(args, ++index)));
74 return index - originalIndex;
77 INPUT_STDIN("--input-stdin") {
79 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
80 options.setInputStream(System.in);
84 SYSTEM_ID("--system-id", "--input-url") {
86 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
87 int originalIndex = index;
88 options.setSystemId(fetchNext(args, ++index));
89 return index - originalIndex;
92 READER_PROPERTY("--reader-property") {
94 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
95 int originalIndex = index;
96 String name = fetchNext(args, ++index);
97 String value = fetchNext(args, ++index);
98 options.addReaderProperty(name, value);
99 return index - originalIndex;
102 READER_FEATURE("--reader-feature") {
104 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
105 int originalIndex = index;
106 String name = fetchNext(args, ++index);
107 String value = fetchNext(args, ++index);
108 options.addReaderFeature(name, Boolean.valueOf(value));
109 return index - originalIndex;
112 READER_NAMESPACE_AWARE("--reader-namespace-aware") {
114 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
115 int originalIndex = index;
116 String value = fetchNext(args, ++index);
117 options.addReaderFeature(Constants.SAX_PARSER_NAMESPACE_AWARE, Boolean.valueOf(value));
118 return index - originalIndex;
123 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
124 int originalIndex = index;
125 options.setAction(fetchNext(args, ++index));
126 return index - originalIndex;
129 ACTION_PROPERTY("--action-property") {
131 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
132 int originalIndex = index;
133 String name = fetchNext(args, ++index);
134 String value = fetchNext(args, ++index);
135 options.addActionProperty(name, value);
136 return index - originalIndex;
141 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
142 int originalIndex = index;
143 for (index++; index < args.length; index++) {
144 options.addActionData(args[index]);
146 return index - originalIndex;
150 private final Collection<String> options;
152 private Token(String... options) {
153 this.options = Arrays.asList(options);
157 * @param option e.g. „--input-file“
158 * @return whether option is this token
160 public boolean matches(String option) {
161 return options.contains(option);
165 * Parse String arguments and fill values into the options object.
167 * @param args CLI arguments
168 * @param index index of the option matched by this token, like „--input-file“
169 * @param options object to be filled
170 * @return number of parsed arguments – if option has no arguments (just boolean flag),
171 * return 0, otherwise return positive integer: number of eaten arguments.
172 * @throws CLIParserException
174 public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;