1 /**
2 * Copyright (c) 2012, The University of Southampton and the individual contributors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * * Neither the name of the University of Southampton nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * A file tool reads and writes files and knows whether existing outputs should
43 * be deleted
44 *
45 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
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 * @return the input string option
71 */
72 public String getInput() {
73 return this.input;
74 }
75
76 /**
77 * @return the input string option
78 */
79 public String getOutput() {
80 return this.output;
81 }
82
83 /**
84 * When the input file is set, any existing input file LIST file is removed
85 * (anything from -if)
86 *
87 * @param input
88 * new input location
89 */
90 public void setInput(String input) {
91 this.inputFile = null;
92 this.input = input;
93 }
94
95 /**
96 * @param output
97 * new input location
98 */
99 public void setOutput(String output) {
100 this.output = output;
101 }
102
103 /**
104 * @return the force option, whether the output should be overwritten if it
105 * exists
106 */
107 public boolean overwriteOutput() {
108 return this.isForce();
109 }
110
111 /**
112 * Fixes a problem with args4j with multivalued arguments being preserved
113 * within the same JVM
114 *
115 * @param <T>
116 * @param modeOptions
117 * @param defaults
118 * optional default values if the modeoptions is empty
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 * @return should files be forcefully removed
137 */
138 public boolean isForce() {
139 return force;
140 }
141
142 /**
143 * @return should existing files be continued from
144 */
145 public boolean isContinue() {
146 return this.contin;
147 }
148
149 /**
150 * @return all the inputs from the -if options file, or the single input if
151 * -i was not defined
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 * @return the input list file, each line is an input
178 */
179 public String getInputFile() {
180 return inputFile;
181 }
182
183 /**
184 * @param inputFile
185 * the new input file
186 */
187 public void setInputFile(String inputFile) {
188 this.inputFile = inputFile;
189 }
190 }