/** * Same as {@link #execute(String...)} but catches all thrown * {@link CliException}s. If an exception is thrown it's message will be added * to the returned error message. * @param args The arguments to parse. * @return A string containing an error message or {@link Optional#absent()} * if no error occurred. */ public Optional<String> safeExecute(String... args) { try { return execute(args); } catch (final CliException e) { return Optional.of(Joiner.on("\n").join(e.getMessage(), printHelp())); } }
return Optional.of(printHelp());
/** * Test for setting a custom help formatter. */ @Test public void testCustomHelpFormatter() { final HelpFormatter defaultFormatter = DefaultHelpFormatter.INSTANCE; final HelpFormatter customFormatter = new DummyHelpFormatter(); final Menu m = Menu.builder() .addHelpOption("h", "help", "help") .commandLineSyntax("") .helpFormatter(customFormatter) .build(); assertEquals(" -h,--help help\n", defaultFormatter.format(m)); assertEquals("[h, help]", customFormatter.format(m)); assertNotEquals(defaultFormatter.format(m), m.printHelp()); assertEquals(customFormatter.format(m), m.printHelp()); }