1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.openimaj.tools;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37
38 import org.kohsuke.args4j.Option;
39 import org.openimaj.io.FileUtils;
40
41
42
43
44
45
46
47
48 public abstract class InOutToolOptions {
49
50 @Option(name = "--input", aliases = "-i", required = false, usage = "Input location", metaVar = "STRING")
51 String input = null;
52
53 @Option(name = "--output", aliases = "-o", required = false, usage = "output location", metaVar = "STRING")
54 protected String output = null;
55
56 @Option(
57 name = "--remove-existing-output",
58 aliases = "-rm",
59 required = false,
60 usage = "If existing output exists, remove it")
61 boolean force = false;
62
63 @Option(name = "--input-file", aliases = "-if", required = false, usage = "Get a set of inputs as listed in a file")
64 private String inputFile = null;
65
66 @Option(name = "--no-continue", aliases = "-nc", required = false, usage = "Do not continue an existing output")
67 boolean contin = false;
68
69
70
71
72 public String getInput() {
73 return this.input;
74 }
75
76
77
78
79 public String getOutput() {
80 return this.output;
81 }
82
83
84
85
86
87
88
89
90 public void setInput(String input) {
91 this.inputFile = null;
92 this.input = input;
93 }
94
95
96
97
98
99 public void setOutput(String output) {
100 this.output = output;
101 }
102
103
104
105
106
107 public boolean overwriteOutput() {
108 return this.isForce();
109 }
110
111
112
113
114
115
116
117
118
119
120 @SafeVarargs
121 public static <T> void prepareMultivaluedArgument(List<T> modeOptions, T... defaults) {
122 final Set<T> modes = new HashSet<T>();
123 for (final T mode : modeOptions) {
124 modes.add(mode);
125 }
126 modeOptions.clear();
127 modeOptions.addAll(modes);
128 if (modeOptions.isEmpty()) {
129 for (final T t : defaults) {
130 modeOptions.add(t);
131 }
132 }
133 }
134
135
136
137
138 public boolean isForce() {
139 return force;
140 }
141
142
143
144
145 public boolean isContinue() {
146 return this.contin;
147 }
148
149
150
151
152
153 public String[] getAllInputs() {
154 boolean multifiles = this.getInputFile() != null;
155 File inputFileF = null;
156 if (multifiles) {
157 inputFileF = new File(this.getInputFile());
158 multifiles = inputFileF.exists() && inputFileF.canRead();
159 }
160 if (!multifiles) {
161 if (this.input == null)
162 return null;
163 return new String[] { this.input };
164 } else {
165 try {
166 final String[] lines = FileUtils.readlines(inputFileF);
167 return lines;
168 } catch (final IOException e) {
169 if (this.input == null)
170 return null;
171 return new String[] { this.input };
172 }
173 }
174 }
175
176
177
178
179 public String getInputFile() {
180 return inputFile;
181 }
182
183
184
185
186
187 public void setInputFile(String inputFile) {
188 this.inputFile = inputFile;
189 }
190 }