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.io.InputStream;
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, InputStream in) throws CLIParserException {
33 CLIOptions options = new CLIOptions();
35 for (int i = 0; i < args.length; i++) {
38 for (Token t : Token.values()) {
40 t.parse(args, i, options);
44 throw new CLIParserException("Unknown option: " + arg);
47 // Default output: STDOUT
48 options.setOutputStream(System.out);
53 private static String fetchNext(String[] args, int index) throws CLIParserException {
54 if (index < args.length) {
57 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
61 private static enum Token {
63 INPUT_FILE("--input-file") {
65 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
66 int originalIndex = index;
67 options.setInputFile(new File(fetchNext(args, ++index)));
68 return index - originalIndex;
71 INPUT_STDIN("--input-stdin") {
73 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
74 options.setInputStream(System.in);
78 INPUT_URL("--input-url") {
80 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
81 int originalIndex = index;
82 options.setInputUrl(fetchNext(args, ++index));
83 return index - originalIndex;
86 SYSTEM_ID("--system-id") {
88 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
89 int originalIndex = index;
90 options.setSystemId(fetchNext(args, ++index));
91 return index - originalIndex;
94 READER_PROPERTY("--reader-property") {
96 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
97 int originalIndex = index;
98 String name = fetchNext(args, ++index);
99 String value = fetchNext(args, ++index);
100 options.addReaderProperty(name, value);
101 return index - originalIndex;
106 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
107 int originalIndex = index;
108 options.setAction(fetchNext(args, ++index));
109 return index - originalIndex;
112 ACTION_PROPERTY("--action-property") {
114 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
115 int originalIndex = index;
116 String name = fetchNext(args, ++index);
117 String value = fetchNext(args, ++index);
118 options.addActionProperty(name, value);
119 return index - originalIndex;
124 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
125 int originalIndex = index;
126 for (index++; index < args.length; index++) {
127 options.addActionData(args[index]);
129 return index - originalIndex;
133 private final String option;
135 private Token(String option) {
136 this.option = option;
140 * @param option e.g. „--input-file“
141 * @return whether option is this token
143 public boolean matches(String option) {
144 return this.option.equals(option);
148 * Parse String arguments and fill values into the options object.
150 * @param args CLI arguments
151 * @param index index of the option matched by this token, like „--input-file“
152 * @param options object to be filled
153 * @return number of parsed arguments – if option has no arguments (just boolean flag),
154 * return 0, otherwise return positive integer: number of eaten arguments.
155 * @throws CLIParserException
157 public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;