FluoProgram { public static void runFluoCommand(List<FluoCommand> fluoCommands, String[] args) { JCommander.Builder jCommanderBuilder = JCommander.newBuilder(); fluoCommands.forEach(jCommanderBuilder::addCommand); JCommander jcommand = jCommanderBuilder.build(); try { jcommand.parse(args); } catch (ParameterException e) { System.err.println(e.getMessage()); String commandName = Optional.ofNullable(jcommand.getParsedCommand()).orElse(""); JCommander parsedJCommandOrProgram = Optional.ofNullable(jcommand.findCommandByAlias(commandName)).orElse(jcommand); parsedJCommandOrProgram.setProgramName(String.format("fluo %s", commandName)); parsedJCommandOrProgram.usage(); throw e; } String parsedCommandType = jcommand.getParsedCommand(); JCommander parsedJCommand = jcommand.findCommandByAlias(parsedCommandType); String programName = String.format("fluo %s", parsedCommandType); parsedJCommand.setProgramName(programName); FluoCommand parsedFluoCommand = (FluoCommand) Iterables.getOnlyElement(parsedJCommand.getObjects()); if (parsedFluoCommand.isHelp()) { parsedJCommand.usage(); return; } try { parsedFluoCommand.execute(); } catch (FluoCommandException e) { System.err.println(String.format("%s failed - %s", programName, e.getMessage())); throw e; } }  static void main(String[] args); static void runFluoCommand(List<FluoCommand> fluoCommands, String[] args);  }
@Test(expected = ParameterException.class) public void testUnparsableCommand() { FluoProgram.runFluoCommand(Collections.singletonList(new MockFluoCommand()), new String[] {"invalid", "command"}); }
@Test public void testHelpCommand() { mockFluoCommand.help = true; FluoProgram.runFluoCommand(Collections.singletonList(mockFluoCommand), new String[] {"test"}); assertFalse(mockFluoCommand.isExecuted()); }
@Test public void testExecutedCommand() { FluoProgram.runFluoCommand(Collections.singletonList(mockFluoCommand), new String[] {"test"}); assertTrue(mockFluoCommand.isExecuted()); }
@Test(expected = FluoCommandException.class) public void testExecutionError() { mockFluoCommand.throwException = true; FluoProgram.runFluoCommand(Collections.singletonList(mockFluoCommand), new String[] {"test"}); }