OptionsParser { static Options parse(String[] args) throws OptionsException { if (args.length == 0) { throw new OptionsException("No sub-command provided. " + AVAILABLE_COMMANDS_MSG); } String subCommand = args[0]; switch (subCommand) { case "send": return parseSendCommand(args); case "run": return parseRunCommand(args); case "serve": return parseServeCommand(args); case "help": case "-h": HelpOptions helpOptions = HelpOptions.GENERAL; if (args.length > 1) try { helpOptions = HelpOptions.valueOf(args[1].toUpperCase()); } catch (IllegalArgumentException e) { } return Options.justShowHelp(helpOptions); default: throw new OptionsException("Unknown sub-command: " + subCommand + ". " + AVAILABLE_COMMANDS_MSG); } }    }
@Test public void cannotParseServeRootPathOptionMissingMandatoryArg() { try { OptionsParser.parse(new String[]{"run"}); fail("Should not be able to parse run command without options"); } catch (OptionsException e) { assertEquals("No http requests file provided", e.getMessage()); } }
@Test(expected = OptionsException.class) public void cannotParseEmptyArgs() throws OptionsException { OptionsParser.parse(new String[]{}); }
@Test public void cannotParseSendUsingBothFileAndText() { try { Options options = OptionsParser.parse(new String[]{ "send", "-t", "REQ", "-f", "file.req" }); fail("Should have failed to accept both -f and -t options together but got: " + options); } catch (OptionsException e) { assertEquals("Cannot use both --text and --file options together", e.getMessage()); } try { Options options = OptionsParser.parse(new String[]{ "send", "--file", "file", "-p", "body", "--text", "text.req" }); fail("Should have failed to accept both -f and -t options together but got: " + options); } catch (OptionsException e) { assertEquals("Cannot use both --text and --file options together", e.getMessage()); } }