CLI { public static void main(String[] args) { if (args.length == 0) { usage(); System.exit(0); } String toolArguments[] = new String[args.length -1]; System.arraycopy(args, 1, toolArguments, 0, toolArguments.length); String toolName = args[0]; String formatName = StreamFactoryRegistry.DEFAULT_FORMAT; int idx = toolName.indexOf("."); if (-1 < idx) { formatName = toolName.substring(idx + 1); toolName = toolName.substring(0, idx); } CmdLineTool tool = toolLookupMap.get(toolName); try { if (null == tool) { throw new TerminateToolException(1, "Tool " + toolName + " is not found."); } if ((0 == toolArguments.length && tool.hasParams()) || 0 < toolArguments.length && "help".equals(toolArguments[0])) { if (tool instanceof TypedCmdLineTool) { System.out.println(((TypedCmdLineTool) tool).getHelp(formatName)); } else if (tool instanceof BasicCmdLineTool) { System.out.println(tool.getHelp()); } System.exit(0); } if (tool instanceof TypedCmdLineTool) { ((TypedCmdLineTool) tool).run(formatName, toolArguments); } else if (tool instanceof BasicCmdLineTool) { if (-1 == idx) { ((BasicCmdLineTool) tool).run(toolArguments); } else { throw new TerminateToolException(1, "Tool " + toolName + " does not support formats."); } } else { throw new TerminateToolException(1, "Tool " + toolName + " is not supported."); } } catch (TerminateToolException e) { if (e.getMessage() != null) { System.err.println(e.getMessage()); } if (e.getCause() != null) { System.err.println(e.getCause().getMessage()); e.getCause().printStackTrace(System.err); } System.exit(e.getCode()); } }  static Set<String> getToolNames(); static void main(String[] args); static final String CMD; }
@Test public void testMainHelpMessage() { try { CLI.main(new String[]{}); } catch (ExitException e) { assertEquals(0, e.status()); } }
@Test public void testUnknownToolMessage() { try { CLI.main(new String[]{"unknown name"}); } catch (ExitException e) { assertEquals(1, e.status()); } }
@Test public void testToolParameterMessage() { try { CLI.main(new String[]{"DoccatTrainer", "-param", "value"}); } catch (ExitException e) { assertEquals(1, e.status()); } }
@Test public void testUnknownFileMessage() { try { CLI.main(new String[]{"Doccat", "unknown.model"}); } catch (ExitException e) { assertEquals(-1, e.status()); } }