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.copyImageResizer.cli;
20 import cz.frantovo.copyImageResizer.RecursiveOptions;
21 import cz.frantovo.copyImageResizer.SizeSpecification;
23 import java.util.Arrays;
24 import java.util.Collection;
28 * @author Ing. František Kučera (frantovo.cz)
30 public class CLIParser {
32 public RecursiveOptions parseOptions(String[] args) throws CLIParserException {
33 RecursiveOptions options = new RecursiveOptions();
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);
60 * @param option name of the option for error messages | if null, the previous argument is used
63 * @throws CLIParserException
65 private static String fetchNext(String[] args, int index, String option) throws CLIParserException {
66 if (index < args.length) {
69 option = option == null ? args[index - 1] : option;
70 throw new CLIParserException("Expecting value for option: " + option);
74 private static enum Token {
76 INPUT_DIR("--input-dir") { // bash-completion: option
78 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
79 int originalIndex = index;
80 String name = fetchNext(args, ++index, args[originalIndex]);
81 options.setInput(new File(name));
82 return index - originalIndex;
85 OUTPUT_DIR("--output-dir") { // bash-completion: option
87 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
88 int originalIndex = index;
89 String name = fetchNext(args, ++index, args[originalIndex]);
90 options.setOutput(new File(name));
91 return index - originalIndex;
94 SIZE("--size") { // bash-completion: option
96 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
97 int originalIndex = index;
98 int width = parseInt(fetchNext(args, ++index, args[originalIndex]));
99 int height = parseInt(fetchNext(args, ++index, args[originalIndex]));
100 boolean resizeSmaller = parseBoolean(fetchNext(args, ++index, args[originalIndex]));
101 String directory = width + "x" + height;
102 options.addSize(new SizeSpecification(width, height, directory, resizeSmaller));
103 return index - originalIndex;
106 SIZE_NAMED("--size-named") { // bash-completion: option
108 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
109 int originalIndex = index;
110 int width = parseInt(fetchNext(args, ++index, args[originalIndex]));
111 int height = parseInt(fetchNext(args, ++index, args[originalIndex]));
112 boolean resizeSmaller = parseBoolean(fetchNext(args, ++index, args[originalIndex]));
113 String directory = fetchNext(args, ++index, args[originalIndex]);
114 options.addSize(new SizeSpecification(width, height, directory, resizeSmaller));
115 return index - originalIndex;
118 SKIP_ERRORS("--skip-errors") { // bash-completion: option
121 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
122 options.setErrorMode(RecursiveOptions.ERROR_MODE.FAIL_LATER);
127 SKIP_ERRORS_SILENTLY("--skip-errors-silently") { // bash-completion: option
130 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
131 options.setErrorMode(RecursiveOptions.ERROR_MODE.SILENT_SKIP);
137 private final Collection<String> options;
139 private Token(String... options) {
140 this.options = Arrays.asList(options);
144 * @param option e.g. „--input-file“
145 * @return whether option is this token
147 public boolean matches(String option) {
148 return options.contains(option);
151 private static int parseInt(String number) throws CLIParserException {
153 return Integer.parseInt(number);
154 } catch (Exception e) {
155 throw new CLIParserException("Unable to parse integer: " + number, e);
159 private static boolean parseBoolean(String value) throws CLIParserException {
166 throw new CLIParserException("Value „" + value + "“ is not a valid boolean. Expecting „true“ or „false“.");
171 * Parse String arguments and fill values into the options object.
173 * @param args CLI arguments
174 * @param index index of the option matched by this token, like „--input-file“
175 * @param options object to be filled
176 * @return number of parsed arguments – if option has no arguments (just boolean flag),
177 * return 0, otherwise return positive integer: number of eaten arguments.
178 * @throws CLIParserException
180 public abstract int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException;