franta-hg@3
|
1 |
/**
|
franta-hg@3
|
2 |
* copy-image-resizer
|
franta-hg@3
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@3
|
4 |
*
|
franta-hg@3
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@3
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@3
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@3
|
8 |
* (at your option) any later version.
|
franta-hg@3
|
9 |
*
|
franta-hg@3
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@3
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@3
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@3
|
13 |
* GNU General Public License for more details.
|
franta-hg@3
|
14 |
*
|
franta-hg@3
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@3
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@3
|
17 |
*/
|
franta-hg@4
|
18 |
package cz.frantovo.copyImageResizer.cli;
|
franta-hg@3
|
19 |
|
franta-hg@5
|
20 |
import cz.frantovo.copyImageResizer.RecursiveOptions;
|
franta-hg@3
|
21 |
import java.io.File;
|
franta-hg@3
|
22 |
import java.util.Arrays;
|
franta-hg@3
|
23 |
import java.util.Collection;
|
franta-hg@3
|
24 |
|
franta-hg@3
|
25 |
/**
|
franta-hg@3
|
26 |
*
|
franta-hg@3
|
27 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@3
|
28 |
*/
|
franta-hg@3
|
29 |
public class CLIParser {
|
franta-hg@3
|
30 |
|
franta-hg@5
|
31 |
public RecursiveOptions parseOptions(String[] args) throws CLIParserException {
|
franta-hg@5
|
32 |
RecursiveOptions options = new RecursiveOptions();
|
franta-hg@3
|
33 |
|
franta-hg@3
|
34 |
for (int i = 0; i < args.length; i++) {
|
franta-hg@3
|
35 |
String arg = args[i];
|
franta-hg@3
|
36 |
|
franta-hg@3
|
37 |
boolean matches = false;
|
franta-hg@3
|
38 |
|
franta-hg@3
|
39 |
for (Token t : Token.values()) {
|
franta-hg@3
|
40 |
if (t.matches(arg)) {
|
franta-hg@3
|
41 |
int parsedArgs = t.parse(args, i, options);
|
franta-hg@3
|
42 |
i = i + parsedArgs;
|
franta-hg@3
|
43 |
matches = true;
|
franta-hg@3
|
44 |
}
|
franta-hg@3
|
45 |
}
|
franta-hg@3
|
46 |
|
franta-hg@3
|
47 |
if (!matches) {
|
franta-hg@3
|
48 |
throw new CLIParserException("Unknown option: " + arg);
|
franta-hg@3
|
49 |
}
|
franta-hg@3
|
50 |
}
|
franta-hg@3
|
51 |
|
franta-hg@3
|
52 |
return options;
|
franta-hg@3
|
53 |
}
|
franta-hg@3
|
54 |
|
franta-hg@3
|
55 |
private static String fetchNext(String[] args, int index) throws CLIParserException {
|
franta-hg@3
|
56 |
if (index < args.length) {
|
franta-hg@3
|
57 |
return args[index];
|
franta-hg@3
|
58 |
} else {
|
franta-hg@3
|
59 |
throw new CLIParserException("Expecting value for option: " + args[index - 1]);
|
franta-hg@3
|
60 |
}
|
franta-hg@3
|
61 |
}
|
franta-hg@3
|
62 |
|
franta-hg@3
|
63 |
private static enum Token {
|
franta-hg@3
|
64 |
|
franta-hg@3
|
65 |
INPUT_DIR("--input-dir") { // bash-completion: option
|
franta-hg@3
|
66 |
@Override
|
franta-hg@5
|
67 |
public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
|
franta-hg@3
|
68 |
int originalIndex = index;
|
franta-hg@3
|
69 |
String name = fetchNext(args, ++index);
|
franta-hg@3
|
70 |
options.setInput(new File(name));
|
franta-hg@3
|
71 |
return index - originalIndex;
|
franta-hg@3
|
72 |
}
|
franta-hg@3
|
73 |
},
|
franta-hg@3
|
74 |
OUTPUT_DIR("--output-dir") { // bash-completion: option
|
franta-hg@3
|
75 |
@Override
|
franta-hg@5
|
76 |
public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
|
franta-hg@3
|
77 |
int originalIndex = index;
|
franta-hg@3
|
78 |
String name = fetchNext(args, ++index);
|
franta-hg@3
|
79 |
options.setOutput(new File(name));
|
franta-hg@3
|
80 |
return index - originalIndex;
|
franta-hg@3
|
81 |
}
|
franta-hg@3
|
82 |
},
|
franta-hg@3
|
83 |
SIZE("--size") { // bash-completion: option
|
franta-hg@3
|
84 |
@Override
|
franta-hg@5
|
85 |
public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
|
franta-hg@3
|
86 |
int originalIndex = index;
|
franta-hg@3
|
87 |
int width = parseInt(fetchNext(args, ++index));
|
franta-hg@3
|
88 |
int height = parseInt(fetchNext(args, ++index));
|
franta-hg@3
|
89 |
String directory = width + "x" + height;
|
franta-hg@5
|
90 |
options.addSize(new RecursiveOptions.SizeSpecification(width, height, directory));
|
franta-hg@3
|
91 |
return index - originalIndex;
|
franta-hg@3
|
92 |
}
|
franta-hg@3
|
93 |
},
|
franta-hg@3
|
94 |
SIZE_NAMED("--size-named") { // bash-completion: option
|
franta-hg@3
|
95 |
@Override
|
franta-hg@5
|
96 |
public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
|
franta-hg@3
|
97 |
int originalIndex = index;
|
franta-hg@3
|
98 |
int width = parseInt(fetchNext(args, ++index));
|
franta-hg@3
|
99 |
int height = parseInt(fetchNext(args, ++index));
|
franta-hg@3
|
100 |
String directory = fetchNext(args, ++index);
|
franta-hg@5
|
101 |
options.addSize(new RecursiveOptions.SizeSpecification(width, height, directory));
|
franta-hg@3
|
102 |
return index - originalIndex;
|
franta-hg@3
|
103 |
}
|
franta-hg@3
|
104 |
};
|
franta-hg@3
|
105 |
|
franta-hg@3
|
106 |
private final Collection<String> options;
|
franta-hg@3
|
107 |
|
franta-hg@3
|
108 |
private Token(String... options) {
|
franta-hg@3
|
109 |
this.options = Arrays.asList(options);
|
franta-hg@3
|
110 |
}
|
franta-hg@3
|
111 |
|
franta-hg@3
|
112 |
/**
|
franta-hg@3
|
113 |
* @param option e.g. „--input-file“
|
franta-hg@3
|
114 |
* @return whether option is this token
|
franta-hg@3
|
115 |
*/
|
franta-hg@3
|
116 |
public boolean matches(String option) {
|
franta-hg@3
|
117 |
return options.contains(option);
|
franta-hg@3
|
118 |
}
|
franta-hg@3
|
119 |
|
franta-hg@3
|
120 |
private static int parseInt(String number) throws CLIParserException {
|
franta-hg@3
|
121 |
try {
|
franta-hg@3
|
122 |
return Integer.parseInt(number);
|
franta-hg@3
|
123 |
} catch (Exception e) {
|
franta-hg@3
|
124 |
throw new CLIParserException("Unable to parse integer: " + number, e);
|
franta-hg@3
|
125 |
}
|
franta-hg@3
|
126 |
}
|
franta-hg@3
|
127 |
|
franta-hg@3
|
128 |
/**
|
franta-hg@3
|
129 |
* Parse String arguments and fill values into the options object.
|
franta-hg@3
|
130 |
*
|
franta-hg@3
|
131 |
* @param args CLI arguments
|
franta-hg@3
|
132 |
* @param index index of the option matched by this token, like „--input-file“
|
franta-hg@3
|
133 |
* @param options object to be filled
|
franta-hg@3
|
134 |
* @return number of parsed arguments – if option has no arguments (just boolean flag),
|
franta-hg@3
|
135 |
* return 0, otherwise return positive integer: number of eaten arguments.
|
franta-hg@3
|
136 |
* @throws CLIParserException
|
franta-hg@3
|
137 |
*/
|
franta-hg@5
|
138 |
public abstract int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException;
|
franta-hg@3
|
139 |
}
|
franta-hg@3
|
140 |
}
|