1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ********************************************************************** 5 * Copyright (c) 2002-2004, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ********************************************************************** 8 * Author: Alan Liu 9 * Created: November 15 2002 10 * Since: ICU 2.4 11 ********************************************************************** 12 */ 13 package com.ibm.icu.dev.tool; 14 15 /** 16 * A command-line option. A UOption specifies the name of an option 17 * and whether or not it takes an argument. It is a mutable object 18 * that later contains the option argument, if any, and a boolean 19 * flag stating whether the option was seen or not. 20 * 21 * The static method parseArgs() takes an array of command-line 22 * arguments and an array of UOptions and parses the command-line 23 * arguments. 24 * 25 * This deliberately resembles the icu4c file uoption.[ch]. 26 */ 27 public class UOption { 28 29 // Deliberated public data members 30 public String longName; 31 public String value; 32 public Fn optionFn; 33 public Object context; 34 public char shortName; 35 public int hasArg; 36 public boolean doesOccur; 37 38 // Values of hasArg 39 public static final int NO_ARG = 0; 40 public static final int REQUIRES_ARG = 1; 41 public static final int OPTIONAL_ARG = 2; 42 43 // Analog of UOptionFn. We don't pass in the context because the 44 // functor can get it from the UOption. 45 public interface Fn { handle(UOption option)46 int handle(UOption option); 47 } 48 49 /** 50 * Create a UOption with the given attributes. 51 */ create(String aLongName, char aShortName, int hasArgument)52 public static UOption create(String aLongName, 53 char aShortName, 54 int hasArgument) { 55 return new UOption(aLongName, aShortName, hasArgument); 56 } 57 58 /** 59 * Create a UOption with the given attributes. 60 * Synonym for create(), for C compatibility. 61 */ DEF(String aLongName, char aShortName, int hasArgument)62 public static UOption DEF(String aLongName, 63 char aShortName, 64 int hasArgument) { 65 return create(aLongName, aShortName, hasArgument); 66 } 67 68 // Standard canned options. These create a new object when 69 // called. Since the UOption object is mutable, we cannot use 70 // static final instances. HELP_H()71 public static UOption HELP_H() { return create("help", 'h', NO_ARG); } HELP_QUESTION_MARK()72 public static UOption HELP_QUESTION_MARK() { return create("help", '?', NO_ARG); } VERBOSE()73 public static UOption VERBOSE() { return create("verbose", 'v', NO_ARG); } QUIET()74 public static UOption QUIET() { return create("quiet", 'q', NO_ARG); } VERSION()75 public static UOption VERSION() { return create("version", 'V', NO_ARG); } COPYRIGHT()76 public static UOption COPYRIGHT() { return create("copyright", 'c', NO_ARG); } 77 DESTDIR()78 public static UOption DESTDIR() { return create("destdir", 'd', REQUIRES_ARG); } SOURCEDIR()79 public static UOption SOURCEDIR() { return create("sourcedir", 's', REQUIRES_ARG); } ENCODING()80 public static UOption ENCODING() { return create("encoding", 'e', REQUIRES_ARG); } ICUDATADIR()81 public static UOption ICUDATADIR() { return create("icudatadir", 'i', REQUIRES_ARG); } PACKAGE_NAME()82 public static UOption PACKAGE_NAME() { return create("package-name", 'p', REQUIRES_ARG); } BUNDLE_NAME()83 public static UOption BUNDLE_NAME() { return create("bundle-name", 'b', REQUIRES_ARG); } 84 85 /** 86 * Java Command line argument parser. 87 * 88 * This function takes the argv[] command line and a description of 89 * the program's options in form of an array of UOption structures. 90 * Each UOption defines a long and a short name (a string and a character) 91 * for options like "--foo" and "-f". 92 * 93 * Each option is marked with whether it does not take an argument, 94 * requires one, or optionally takes one. The argument may follow in 95 * the same argv[] entry for short options, or it may always follow 96 * in the next argv[] entry. 97 * 98 * An argument is in the next argv[] entry for both long and short name 99 * options, except it is taken from directly behind the short name in 100 * its own argv[] entry if there are characters following the option letter. 101 * An argument in its own argv[] entry must not begin with a '-' 102 * unless it is only the '-' itself. There is no restriction of the 103 * argument format if it is part of the short name options's argv[] entry. 104 * 105 * The argument is stored in the value field of the corresponding 106 * UOption entry, and the doesOccur field is set to 1 if the option 107 * is found at all. 108 * 109 * Short name options without arguments can be collapsed into a single 110 * argv[] entry. After an option letter takes an argument, following 111 * letters will be taken as its argument. 112 * 113 * If the same option is found several times, then the last 114 * argument value will be stored in the value field. 115 * 116 * For each option, a function can be called. This could be used 117 * for options that occur multiple times and all arguments are to 118 * be collected. 119 * 120 * All options are removed from the argv[] array itself. If the parser 121 * is successful, then it returns the number of remaining non-option 122 * strings. (Unlike C, the Java argv[] array does NOT contain 123 * the program name in argv[0].) 124 * 125 * An option "--" ends option processing; everything after this 126 * remains in the argv[] array. 127 * 128 * An option string "-" alone is treated as a non-option. 129 * 130 * If an option is not recognized or an argument missing, then 131 * the parser returns with the negative index of the argv[] entry 132 * where the error was detected. 133 * 134 * @param argv this parameter is modified 135 * @param start the first argument in argv[] to examine. Must be 136 * 0..argv.length-1. Arguments from 0..start-1 are ignored. 137 * @param options this parameter is modified 138 * @return the number of unprocessed arguments in argv[], including 139 * arguments 0..start-1. 140 */ parseArgs(String argv[], int start, UOption options[])141 public static int parseArgs(String argv[], int start, UOption options[]) { 142 String arg; 143 int i=start, remaining=start; 144 char c; 145 boolean stopOptions=false; 146 147 while(i<argv.length) { 148 arg=argv[i]; 149 if(!stopOptions && arg.length()>1 && arg.charAt(0)=='-') { 150 /* process an option */ 151 c=arg.charAt(1); 152 UOption option=null; 153 arg=arg.substring(2); 154 if(c=='-') { 155 /* process a long option */ 156 if(arg.length()==0) { 157 /* stop processing options after "--" */ 158 stopOptions=true; 159 } else { 160 /* search for the option string */ 161 int j; 162 for(j=0; j<options.length; ++j) { 163 if(options[j].longName != null && arg.equals(options[j].longName)) { 164 option=options[j]; 165 break; 166 } 167 } 168 if(option==null) { 169 /* no option matches */ 170 syntaxError("Unknown option " + argv[i]); 171 } 172 option.doesOccur=true; 173 174 if(option.hasArg!=NO_ARG) { 175 /* parse the argument for the option, if any */ 176 if(i+1<argv.length && !(argv[i+1].length()>1 && argv[i+1].charAt(0)=='-')) { 177 /* argument in the next argv[], and there is not an option in there */ 178 option.value=argv[++i]; 179 } else if(option.hasArg==REQUIRES_ARG) { 180 /* there is no argument, but one is required: return with error */ 181 syntaxError("Option " + argv[i] + " lacks required argument"); 182 } 183 } 184 } 185 } else { 186 /* process one or more short options */ 187 for (;;) { 188 /* search for the option letter */ 189 int j; 190 for(j=0; j<options.length; ++j) { 191 if(c==options[j].shortName) { 192 option=options[j]; 193 break; 194 } 195 } 196 if(option==null) { 197 /* no option matches */ 198 syntaxError("Unknown option '" + c + "' in " + argv[i]); 199 } 200 option.doesOccur=true; 201 202 if(option.hasArg!=NO_ARG) { 203 /* parse the argument for the option, if any */ 204 if(arg.length()!=0) { 205 /* argument following in the same argv[] */ 206 option.value=arg; 207 /* do not process the rest of this arg as option letters */ 208 break; 209 } else if(i+1<argv.length && !(argv[i+1].length()>1 && argv[i+1].charAt(0)=='-')) { 210 /* argument in the next argv[], and there is not an option in there */ 211 option.value=argv[++i]; 212 /* this break is redundant because we know that *arg==0 */ 213 break; 214 } else if(option.hasArg==REQUIRES_ARG) { 215 /* there is no argument, but one is required: return with error */ 216 syntaxError("Option -" + c + " lacks required argument"); 217 } 218 } 219 220 /* get the next option letter */ 221 option=null; 222 if (arg.length()==0) break; 223 c=arg.charAt(0); 224 arg=arg.substring(1); 225 } 226 } 227 228 if(option!=null && option.optionFn!=null && option.optionFn.handle(option)<0) { 229 /* the option function was called and returned an error */ 230 syntaxError("Option handler failed for " + argv[i]); 231 } 232 233 /* go to next argv[] */ 234 ++i; 235 } else { 236 /* move a non-option up in argv[] */ 237 argv[remaining++]=arg; 238 ++i; 239 } 240 } 241 return remaining; 242 } 243 244 /** 245 * Allows the default to be set in an option list. 246 * @param s 247 * @return this setDefault(String s)248 */public UOption setDefault(String s) { 249 value = s; 250 return this; 251 } 252 253 /** 254 * Convenient method. 255 */ parseArgs(String argv[], UOption options[])256 public static int parseArgs(String argv[], UOption options[]) { 257 return parseArgs(argv, 0, options); 258 } 259 260 /** 261 * Constructor. 262 */ UOption(String aLongName, char aShortName, int hasArgument)263 private UOption(String aLongName, 264 char aShortName, 265 int hasArgument) { 266 longName = aLongName; 267 shortName = aShortName; 268 hasArg = hasArgument; 269 } 270 271 /** 272 * Throw an exception indicating a syntax error. 273 */ syntaxError(String message)274 private static void syntaxError(String message) { 275 throw new IllegalArgumentException("Error in argument list: " + message); 276 } 277 } 278