### Question:
CommandLineParser { public String[] parse(String[] argv) throws CommandLineParserException { int ix = 0; for (; ix < argv.length; ix++) { if (argv[ix].charAt(0) != '-') break; String value = null; int pos = argv[ix].indexOf('='); boolean islong = argv[ix].startsWith("--"); String name; if (pos != -1) { value = argv[ix].substring(pos + 1); name = argv[ix].substring(islong ? 2 : 1, pos); } else name = argv[ix].substring(islong ? 2 : 1); Option option; if (islong) option = getOption(name); else { if (name.length() != 1) throw new CommandLineParserException("Option with single - had long" + " name: " + name); option = getOption(name.charAt(0)); } if (option == null) throw new CommandLineParserException("No such option: " + argv[ix]+ " '" + name + "'"); if (option instanceof BooleanOption) { ((BooleanOption) option).turnon(); if (value != null) throw new CommandLineParserException("This option takes no value " + argv[ix]); } else { StringOption o = (StringOption) option; if (value == null) throw new CommandLineParserException("No value for option " + argv[ix]); o.setValue(value); } } if (argv.length - ix < minargs) throw new CommandLineParserException("Must have at least " + minargs + " arguments; got " + (argv.length - ix)); if (argv.length - ix > maxargs) throw new CommandLineParserException("Can't have more than " + maxargs + " arguments; got " + (argv.length - ix)); String[] args = new String[argv.length - ix]; for (int pos = 0; ix < argv.length; ix++) { if (argv[ix].charAt(0) == '-') throw new CommandLineParserException("Option " + argv[ix] + " appears "+ "after file arguments"); args[pos++] = argv[ix]; } return args; } CommandLineParser(); String[] parse(String[] argv); Option getOption(String longname); Option getOption(char shortname); boolean getOptionState(String longname); String getOptionValue(String longname); int getOptionInteger(String longname, int defaultvalue); double getOptionDouble(String longname, double defaultvalue); void setMinimumArguments(int minargs); void setMaximumArguments(int maxargs); void registerOption(Option option); void addStringOption(String longname, char shortname); void addBooleanOption(String longname, char shortname);  }### Answer:
@Test public void testEmpty() { String[] args = parser.parse(new String[] {}); assertEquals(0, args.length); }
@Test public void testSingleArgument() { String[] args = parser.parse(new String[] { "foo" }); StringUtilsTest.assertEqual(new String[]{"foo"}, args); }
@Test public void testUnknownOption() { try { parser.parse(new String[] { "-t", "foo" }); fail("didn't catch unregistered option"); } catch (CommandLineParser.CommandLineParserException e) { } }