/** * Gets the options parsed. * @return the options parsed. * Each string is of the form "-a", for short options, or "--alpha", for * long options. Note that the hyphen "-" or double-hyphen "--" is * included in the strings. For long options, the complete names are * returned, even if abbreviations were parsed. */ public String[] getOptions() { return getStrings(_optList); }
/** * Constructs an argument parser for the specified arguments and * short options specification. * @param args the command-line arguments, as passed to the method main. * @param shortOpts the short options specification. Each option is * specified by a single character. For options that require values, * this character must be followed by a single colon ':'. For example, * "a:b" specifies two options "-a" and "-b", and the first option "-a" * requires a value. */ public ArgsParser(String[] args, String shortOpts) throws OptionException { _args = args; _shortOpts = shortOpts; _longOpts = new String[0]; init(); }
String shortOpts = "ha:b"; String[] longOpts = {"help","alpha=","beta"}; ArgsParser ap = new ArgsParser(arg,shortOpts,longOpts); String[] opts = ap.getOptions(); String[] vals = ap.getValues(); for (int i=0; i<opts.length; ++i) { String opt = opts[i]; assertTrue(false); } else if (opt.equals("-a") || opt.equals("--alpha")) { a = ArgsParser.toFloat(val); } else if (opt.equals("-b") || opt.equals("--beta")) { b = true; String[] otherArgs = ap.getOtherArgs(); assertTrue(otherArgs.length==1); assertTrue(otherArgs[0].equals("foo"));
private void init() throws OptionException { int n = _args.length; _optList = new ArrayList<String>(n); _valList = new ArrayList<String>(n); _argList = new ArrayList<String>(n); for (int i=0; i<n; ++i) _argList.add(_args[i]); while (!_argList.isEmpty()) { String arg = _argList.get(0); if (arg.charAt(0)!='-' || arg.equals("-")) break; _argList.remove(0); if (arg.equals("--")) break; if (arg.charAt(1)=='-') { doLongOption(arg); } else { doShortOption(arg); } } }
private void doShortOption(String arg) throws OptionException { String argString = arg.substring(1); while (!argString.equals("")) { char opt = argString.charAt(0); argString = argString.substring(1); String val = ""; if (shortOptionHasValue(opt)) { if (argString.equals("")) { if (_argList.isEmpty()) throw new OptionException("option -"+opt+" requires a value"); argString = _argList.remove(0); } val = argString; argString = ""; } _optList.add("-"+opt); _valList.add(val); } }
private void doLongOption(String arg) throws OptionException { String argString = arg.substring(2); String opt = argString; String val = null; int i = argString.indexOf('='); if (i>=0) { opt = argString.substring(0,i); val = (i+1<argString.length())?argString.substring(i+1):""; } boolean hasValue = longOptionHasValue(opt); opt = _longOpt; if (hasValue) { if (val==null) { if (_argList.isEmpty()) throw new OptionException("option --"+opt+" requires a value"); val = _argList.remove(0); } } else if (val!=null) { throw new OptionException("option --"+opt+" must not have a value"); } _optList.add("--"+opt); _valList.add(val); }
/** * Gets the values corresponding to the options parsed. * @return the values parsed. * The value is null for any option that does not expect a value. */ public String[] getValues() { return getStrings(_valList); }
/** * Constructs an argument parser for the specified arguments and * short and long options specifications. * @param args the command-line arguments, as passed to the method main. * @param shortOpts the short options specification. Each option is * specified by a single character. For options that require values, * this character must be followed by a single colon ':'. For example, * "a:b" specifies two options "-a" and "-b", and the first option "-a" * requires a value. * @param longOpts the long options specification. Each option is * is specified by a single string, containing the long option name. * For options that require values, this string must end in the * character "=". For example, the string "--alpha=" specifies a * long option "--alpha" that requires a value. */ public ArgsParser(String[] args, String shortOpts, String[] longOpts) throws OptionException { _args = args; _shortOpts = shortOpts; _longOpts = longOpts; init(); }
/** * Gets the other arguments, those that do not correspond to options. * @return the other arguments. */ public String[] getOtherArgs() { return getStrings(_argList); }