Option { public static Option[] parseCLArgs(String[] args, Option[] templates) { int i = 0; List<Option> options = new ArrayList<Option>(args.length); try { while (i < args.length) { String name = args[i++]; Option template = findTemplate(name, templates); StringBuilder values = new StringBuilder(); if (!Type.LIST.equals(template.getType())) { for (int j = 0; j < template.getLen(); j++) { values.append(args[i++]).append(" "); } } else if (hasMoreFlags(args, --i, templates)) { throw new IllegalArgumentException(); } else { for (String arg : Arrays.copyOfRange(args, i, args.length)) { values.append(arg).append(" "); } template.setValue(values.toString().trim()); Option[] result = new Option[options.size() + 1]; result = options.toArray(result); result[result.length - 1] = template; return result; } template.setValue(values.toString().trim()); options.add(template); } return options.toArray(new Option[options.size()]); } catch (Exception e) { throw new IllegalArgumentException(e); } } Option(Type type, String name, int numvalues, String description); static Option createFlag(String name, String description); static Option createList(String description); static Option createOption(String name, String description); static Option createOption(String name, int numvalues, String description); static Option findListOption(Option[] options); static Option findOption(String name, Option[] options); static Option[] parseCLArgs(String[] args, Option[] templates); String getDescription(); int getLen(); String getName(); Type getType(); String getValue(); String[] getValues(); void setValue(String value); @Override String toString(); static final int UNKNOWN; static final int UNLIMITED_LENGTH; }
@Test public void testBadParse() { try { Option.parseCLArgs("-z".split("\\s+"), failTemplates); } catch (IllegalArgumentException ex) { return; } fail("Unexpected command line args should through an IllegalArgumentException"); }
@Test public void testFlagAfterListFail() { try { Option.parseCLArgs("-a e f g -a".split("\\s+"), templates); } catch (IllegalArgumentException ex) { return; } fail("List options (which have no identifier) must occur after all other flags having an identifier."); }
@Test public void testParseCLArgs() { Option[] options = Option.parseCLArgs("-a -b b -c c c c c c -d d d e f g".split("\\s+"), templates); Option a = Option.findOption("-a", options); assertTrue("".equals(a.getValue())); Option b = Option.findOption("-b", options); assertTrue("b".equals(b.getValue())); assertTrue("b".equals(b.getValues()[0])); assertTrue(b.getValues().length == 1); Option c = Option.findOption("-c", options); assertTrue("c c c c c".equals(c.getValue())); assertTrue(Arrays.equals(new String[] { "c", "c", "c", "c", "c" }, c.getValues())); assertTrue(c.getValues().length == 5); Option d = Option.findOption("-d", options); assertTrue("d d".equals(d.getValue())); assertTrue(Arrays.equals(new String[] { "d", "d" }, d.getValues())); assertTrue(d.getValues().length == 2); Option list = Option.findListOption(options); assertTrue("e f g".equals(list.getValue())); assertTrue(Arrays.equals(new String[] { "e", "f", "g" }, list.getValues())); assertTrue(list.getValues().length == 3); }