Options { private Options(String[] spec, String[] gspec, Options opt, Function<String, String> env) { this.gspec = gspec; if (gspec == null && opt == null) { this.spec = spec; } else { ArrayList<String> list = new ArrayList<>(); list.addAll(Arrays.asList(spec)); list.addAll(Arrays.asList(gspec != null ? gspec : opt.gspec)); this.spec = list.toArray(new String[list.size()]); } Map<String, Boolean> myOptSet = new HashMap<>(); Map<String, Object> myOptArg = new HashMap<>(); parseSpec(myOptSet, myOptArg); if (opt != null) { for (Entry<String, Boolean> e : opt.optSet.entrySet()) { if (e.getValue()) myOptSet.put(e.getKey(), true); } for (Entry<String, Object> e : opt.optArg.entrySet()) { if (!e.getValue().equals("")) myOptArg.put(e.getKey(), e.getValue()); } opt.reset(); } unmodifiableOptSet = Collections.unmodifiableMap(myOptSet); unmodifiableOptArg = Collections.unmodifiableMap(myOptArg); defOpts = env != null ? env.apply(usageName.toUpperCase() + "_OPTS") : null; defArgs = (defOpts != null) ? defOpts.split("\\s+") : new String[0]; } private  Options(String[] spec, String[] gspec, Options opt, Function<String, String> env); static Options compile(String[] optSpec); static Options compile(String[] optSpec, Function<String, String> env); static Options compile(String optSpec); static Options compile(String optSpec, Function<String, String> env); static Options compile(String[] optSpec, Options gopt); static Options compile(String[] optSpec, String[] gspec); Options setStopOnBadOption(boolean stopOnBadOption); Options setOptionsFirst(boolean optionsFirst); boolean isSet(String name); Object getObject(String name); @SuppressWarnings("unchecked") List<Object> getObjectList(String name); List<String> getList(String name); String get(String name); int getNumber(String name); List<Object> argObjects(); List<String> args(); void usage(PrintStream err); String usage(); IllegalArgumentException usageError(String s); Options parse(Object[] argv); Options parse(List<?> argv); Options parse(Object[] argv, boolean skipArg0); Options parse(List<?> argv, boolean skipArg0); @Override String toString(); static final String NL; }
@Test public void testOptions() { final String[] usage = { "test - test Options usage", " text before Usage: is displayed when usage() is called and no error has occurred.", " so can be used as a simple help message.", "", "Usage: testOptions [OPTION]... PATTERN [FILES]...", " Output control: arbitary non-option text can be included.", " -? --help show help", " -c --count=COUNT show COUNT lines", " -h --no-filename suppress the prefixing filename on output", " -q --quiet, --silent suppress all normal output", " --binary-files=TYPE assume that binary files are TYPE", " TYPE is 'binary', 'text', or 'without-match'", " -I equivalent to --binary-files=without-match", " -d --directories=ACTION how to handle directories (default=skip)", " ACTION is 'read', 'recurse', or 'skip'", " -D --devices=ACTION how to handle devices, FIFOs and sockets", " ACTION is 'read' or 'skip'", " -R, -r --recursive equivalent to --directories=recurse" }; Options opt = Options.compile(usage).parse("test -c 2 --binary-files=foo --binary-files bar pattern".split("\\s")); assertTrue(opt.isSet("count")); assertEquals(2, opt.getNumber("count")); assertFalse(opt.isSet("no-filename")); assertTrue(opt.isSet("binary-files")); assertEquals(Arrays.asList("foo", "bar"), opt.getList("binary-files")); assertEquals(Arrays.asList("test", "pattern"), opt.args()); }