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, either version 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 package cz.frantovo.alt2xml.cli;
21 import java.util.Arrays;
22 import java.util.Collection;
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
29 * @author Ing. František Kučera (frantovo.cz)
31 public class CLIParser {
33 public CLIOptions parseOptions(String[] args) throws CLIParserException {
34 CLIOptions options = new CLIOptions();
36 for (int i = 0; i < args.length; i++) {
39 boolean matches = false;
41 for (Token t : Token.values()) {
43 int parsedArgs = t.parse(args, i, options);
50 throw new CLIParserException("Unknown option: " + arg);
55 options.setOutputStream(System.out);
60 private static String fetchNext(String[] args, int index) throws CLIParserException {
61 if (index < args.length) {
64 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
68 private static enum Token {
70 INPUT_FILE("--input-file") {
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;
78 INPUT_STDIN("--input-stdin") {
80 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
81 options.setInputStream(System.in);
85 SYSTEM_ID("--system-id", "--input-url") {
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;
93 READER_PROPERTY("--reader-property") {
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;
103 READER_FEATURE("--reader-feature") {
105 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
106 int originalIndex = index;
107 String name = fetchNext(args, ++index);
108 String value = fetchNext(args, ++index);
109 options.addReaderFeature(name, Boolean.valueOf(value));
110 return index - originalIndex;
115 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
116 int originalIndex = index;
117 options.setAction(fetchNext(args, ++index));
118 return index - originalIndex;
121 ACTION_PROPERTY("--action-property") {
123 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
124 int originalIndex = index;
125 String name = fetchNext(args, ++index);
126 String value = fetchNext(args, ++index);
127 options.addActionProperty(name, value);
128 return index - originalIndex;
133 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
134 int originalIndex = index;
135 for (index++; index < args.length; index++) {
136 options.addActionData(args[index]);
138 return index - originalIndex;
142 private final Collection<String> options;
144 private Token(String... options) {
145 this.options = Arrays.asList(options);
149 * @param option e.g. „--input-file“
150 * @return whether option is this token
152 public boolean matches(String option) {
153 return options.contains(option);
157 * Parse String arguments and fill values into the options object.
159 * @param args CLI arguments
160 * @param index index of the option matched by this token, like „--input-file“
161 * @param options object to be filled
162 * @return number of parsed arguments – if option has no arguments (just boolean flag),
163 * return 0, otherwise return positive integer: number of eaten arguments.
164 * @throws CLIParserException
166 public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;