@Test public void noArgsPrintsMainHelp() throws Exception { Cli cli = new Cli("mytest"); PrintStream out = System.out; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(outputStream); System.setOut(printStream); cli.run(new String[]{}); System.setOut(out); String help = outputStream.toString("UTF-8"); assertThat(help, containsString("mytest")); }
public void run(String[] args) { try { if (args.length == 0) { printMainHelp(); return; } String argument = args[0]; if (argument.equals("-h") || argument.equals(HELP)) { printMainHelp(); } else if (commands.containsKey(argument)) { Command command = commands.get(argument); Parser parser = command.getParser(); Arguments parsedArguments = parser.parse(args, 1); if (parsedArguments.has(NO_COLOR)) { setUseAnsiColors(false); } else if (parsedArguments.has(FORCE_COLOR)) { setUseAnsiColors(true); } if (parsedArguments.has(HELP)) { printHelp(command, parser); } else { configureLogging(parsedArguments); command.execute(parsedArguments, classLoader); } } else { throw new CliError("Unknown command or argument: '" + argument + "'", 1); } } catch (Exception e) { printException(args, e); throw e; } }
Cli { public void run(String[] args) { try { if (args.length == 0) { printMainHelp(); return; } String argument = args[0]; if (argument.equals("-h") || argument.equals(HELP)) { printMainHelp(); } else if (commands.containsKey(argument)) { Command command = commands.get(argument); Parser parser = command.getParser(); Arguments parsedArguments = parser.parse(args, 1); if (parsedArguments.has(NO_COLOR)) { setUseAnsiColors(false); } else if (parsedArguments.has(FORCE_COLOR)) { setUseAnsiColors(true); } if (parsedArguments.has(HELP)) { printHelp(command, parser); } else { configureLogging(parsedArguments); command.execute(parsedArguments, classLoader); } } else { throw new CliError("Unknown command or argument: '" + argument + "'", 1); } } catch (Exception e) { printException(args, e); throw e; } } }
Cli { public void run(String[] args) { try { if (args.length == 0) { printMainHelp(); return; } String argument = args[0]; if (argument.equals("-h") || argument.equals(HELP)) { printMainHelp(); } else if (commands.containsKey(argument)) { Command command = commands.get(argument); Parser parser = command.getParser(); Arguments parsedArguments = parser.parse(args, 1); if (parsedArguments.has(NO_COLOR)) { setUseAnsiColors(false); } else if (parsedArguments.has(FORCE_COLOR)) { setUseAnsiColors(true); } if (parsedArguments.has(HELP)) { printHelp(command, parser); } else { configureLogging(parsedArguments); command.execute(parsedArguments, classLoader); } } else { throw new CliError("Unknown command or argument: '" + argument + "'", 1); } } catch (Exception e) { printException(args, e); throw e; } } Cli(String applicationName);  Cli(String applicationName, ClassLoader classLoader); }
Cli { public void run(String[] args) { try { if (args.length == 0) { printMainHelp(); return; } String argument = args[0]; if (argument.equals("-h") || argument.equals(HELP)) { printMainHelp(); } else if (commands.containsKey(argument)) { Command command = commands.get(argument); Parser parser = command.getParser(); Arguments parsedArguments = parser.parse(args, 1); if (parsedArguments.has(NO_COLOR)) { setUseAnsiColors(false); } else if (parsedArguments.has(FORCE_COLOR)) { setUseAnsiColors(true); } if (parsedArguments.has(HELP)) { printHelp(command, parser); } else { configureLogging(parsedArguments); command.execute(parsedArguments, classLoader); } } else { throw new CliError("Unknown command or argument: '" + argument + "'", 1); } } catch (Exception e) { printException(args, e); throw e; } } Cli(String applicationName);  Cli(String applicationName, ClassLoader classLoader); void addCommand(Command command); void run(String[] args); static void setStdout(Consumer<String> printer); static void setStderr(Consumer<String> printer); static Consumer<String> getStdout(); static Consumer<String> getStderr(); static void stdout(Object message); static void stderr(Object message); static void setUseAnsiColors(boolean useAnsiColors); }
Cli { public void run(String[] args) { try { if (args.length == 0) { printMainHelp(); return; } String argument = args[0]; if (argument.equals("-h") || argument.equals(HELP)) { printMainHelp(); } else if (commands.containsKey(argument)) { Command command = commands.get(argument); Parser parser = command.getParser(); Arguments parsedArguments = parser.parse(args, 1); if (parsedArguments.has(NO_COLOR)) { setUseAnsiColors(false); } else if (parsedArguments.has(FORCE_COLOR)) { setUseAnsiColors(true); } if (parsedArguments.has(HELP)) { printHelp(command, parser); } else { configureLogging(parsedArguments); command.execute(parsedArguments, classLoader); } } else { throw new CliError("Unknown command or argument: '" + argument + "'", 1); } } catch (Exception e) { printException(args, e); throw e; } } Cli(String applicationName);  Cli(String applicationName, ClassLoader classLoader); void addCommand(Command command); void run(String[] args); static void setStdout(Consumer<String> printer); static void setStderr(Consumer<String> printer); static Consumer<String> getStdout(); static Consumer<String> getStderr(); static void stdout(Object message); static void stderr(Object message); static void setUseAnsiColors(boolean useAnsiColors); static final String HELP; static final String NO_COLOR; static final String FORCE_COLOR; static final String DEBUG; static final String STACKTRACE; static final String LOGGING; }