/** * Generates a bash completion script for the specified command class. * @param args command line options. Specify at least the {@code commandLineFQCN} mandatory parameter, which is * the fully qualified class name of the annotated {@code @Command} class to generate a completion script for. * Other parameters are optional. Specify {@code -h} to see details on the available options. */ public static void main(String... args) { AbstractParseResultHandler<List<Object>> resultHandler = new CommandLine.RunLast(); DefaultExceptionHandler<List<Object>> exceptionHandler = CommandLine.defaultExceptionHandler(); if (exitOnError()) { exceptionHandler.andExit(EXIT_CODE_INVALID_INPUT); } List<Object> result = new CommandLine(new App()).parseWithHandlers(resultHandler, exceptionHandler, args); int exitCode = result == null ? EXIT_CODE_SUCCESS : (Integer) result.get(0); if ((exitCode == EXIT_CODE_SUCCESS && exitOnSuccess()) || (exitCode != EXIT_CODE_SUCCESS && exitOnError())) { System.exit(exitCode); } }
@Test public void testExitCodeFromExceptionHandler() { @Command class App implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "blah"); } } exit.expectSystemExitWithStatus(25); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); assertEquals(format("" + "blah%n", "<main command>"), systemErrRule.getLog()); }
public static void main(final String... args) { final PantheonCommand pantheonCommand = new PantheonCommand( getLogger(), new BlockImporter(), new RunnerBuilder(), new PantheonControllerBuilder(), new SynchronizerConfiguration.Builder()); pantheonCommand.parse( new RunLast().andExit(SUCCESS_EXIT_CODE), defaultExceptionHandler().andExit(ERROR_EXIT_CODE), args); } }
static void runCommandLine( Function<ClientConfig, JetInstance> jetClientFn, PrintStream out, PrintStream err, boolean shouldExit, String[] args ) { CommandLine cmd = new CommandLine(new JetCommandLine(jetClientFn, out, err)); if (args.length == 0) { cmd.usage(out); } else { DefaultExceptionHandler<List<Object>> excHandler = new ExceptionHandler<List<Object>>().useErr(err).useAnsi(Ansi.AUTO); if (shouldExit) { excHandler.andExit(1); } List<Object> parsed = cmd.parseWithHandlers(new RunAll().useOut(out).useAnsi(Ansi.AUTO), excHandler, args); // only top command was executed if (parsed != null && parsed.size() == 1) { cmd.usage(out); } } }
@Test public void testSystemExitForOtherExceptions() { @Command class App implements Runnable { public void run() { throw new RuntimeException("blah"); } } exit.expectSystemExitWithStatus(25); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String actual = systemErrRule.getLog(); assertTrue(actual.startsWith("picocli.CommandLine$ExecutionException: Error while running command (picocli.CommandLineParseWithHandlersTest")); assertTrue(actual.contains("java.lang.RuntimeException: blah")); } }); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); }
@Test public void testExitCodeFromExceptionHandler() { @Command class App implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "blah"); } } exit.expectSystemExitWithStatus(25); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); assertEquals(format("" + "blah%n", "<main command>"), systemErrRule.getLog()); }
@Test(expected = InternalError.class) public void testNoSystemExitForErrors() { @Command class App implements Runnable { public void run() { throw new InternalError("blah"); } } new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); }
/** * Generates a bash completion script for the specified command class. * @param args command line options. Specify at least the {@code commandLineFQCN} mandatory parameter, which is * the fully qualified class name of the annotated {@code @Command} class to generate a completion script for. * Other parameters are optional. Specify {@code -h} to see details on the available options. */ public static void main(String... args) { new CommandLine(new App()).parseWithHandlers( new CommandLine.RunLast().andExit(EXIT_CODE_SUCCESS), CommandLine.defaultExceptionHandler().andExit(EXIT_CODE_INVALID_INPUT), args); }
@Test public void testSystemExitForOtherExceptions() { @Command class App implements Runnable { public void run() { throw new RuntimeException("blah"); } } exit.expectSystemExitWithStatus(25); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String actual = systemErrRule.getLog(); assertTrue(actual.startsWith("picocli.CommandLine$ExecutionException: Error while running command (picocli.CommandLineParseWithHandlersTest")); assertTrue(actual.contains("java.lang.RuntimeException: blah")); } }); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); }
public static void main(String... args) { CommandLine cmd = new CommandLine(new ExitCodeDemo()); cmd.parseWithHandlers( new CommandLine.RunLast().andExit(123), CommandLine.defaultExceptionHandler().andExit(456), args); } }
@Test(expected = InternalError.class) public void testNoSystemExitForErrors() { @Command class App implements Runnable { public void run() { throw new InternalError("blah"); } } new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), defaultExceptionHandler().andExit(25)); }