001/** 002 * Copyright (c) 2012, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.tools; 031 032import java.io.File; 033import java.io.IOException; 034import java.util.HashSet; 035import java.util.List; 036import java.util.Set; 037 038import org.kohsuke.args4j.Option; 039import org.openimaj.io.FileUtils; 040 041/** 042 * A file tool reads and writes files and knows whether existing outputs should 043 * be deleted 044 * 045 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 046 * 047 */ 048public abstract class InOutToolOptions { 049 050 @Option(name = "--input", aliases = "-i", required = false, usage = "Input location", metaVar = "STRING") 051 String input = null; 052 053 @Option(name = "--output", aliases = "-o", required = false, usage = "output location", metaVar = "STRING") 054 protected String output = null; 055 056 @Option( 057 name = "--remove-existing-output", 058 aliases = "-rm", 059 required = false, 060 usage = "If existing output exists, remove it") 061 boolean force = false; 062 063 @Option(name = "--input-file", aliases = "-if", required = false, usage = "Get a set of inputs as listed in a file") 064 private String inputFile = null; 065 066 @Option(name = "--no-continue", aliases = "-nc", required = false, usage = "Do not continue an existing output") 067 boolean contin = false; 068 069 /** 070 * @return the input string option 071 */ 072 public String getInput() { 073 return this.input; 074 } 075 076 /** 077 * @return the input string option 078 */ 079 public String getOutput() { 080 return this.output; 081 } 082 083 /** 084 * When the input file is set, any existing input file LIST file is removed 085 * (anything from -if) 086 * 087 * @param input 088 * new input location 089 */ 090 public void setInput(String input) { 091 this.inputFile = null; 092 this.input = input; 093 } 094 095 /** 096 * @param output 097 * new input location 098 */ 099 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}