/** @deprecated use {@link #parseWithHandler(IParseResultHandler2, String[])} instead * @since 2.0 */ @Deprecated public List<Object> parseWithHandler(IParseResultHandler handler, PrintStream out, String... args) { return parseWithHandlers(handler, out, Help.Ansi.AUTO, defaultExceptionHandler(), args); } /**
return parseWithHandlers(handler, new DefaultExceptionHandler<R>(), args);
/** * 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); } }
Method method = candidates.get(0); CommandLine cmd = new CommandLine(method); List<Object> list = cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args); return list == null ? null : list.get(0);
List<Object> results = cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args); @SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0); return result;
public static void main(String... args) { CommandLine cmd = new CommandLine(new ExitCodeDemo()); cmd.parseWithHandlers( new CommandLine.RunLast().andExit(123), CommandLine.defaultExceptionHandler().andExit(456), args); } }
List<Object> results = cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args); @SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0); return result;
@Test public void testOptionalListParameterInCommandClass() { @Command() class TestCommand implements Callable<String> { @Parameters(arity="0..*") private List<String> values; public String call() throws Exception { return values == null ? "null" : values.toString(); } } // seems to be working for @Command-class @Parameters CommandLine commandLine = new CommandLine(new TestCommand()); List<Object> firstExecutionResultWithParametersGiven = commandLine.parseWithHandlers( new RunLast(), new DefaultExceptionHandler<List<Object>>(), new String[] {"arg0", "arg1"}); List<Object> secondExecutionResultWithoutParameters = commandLine.parseWithHandlers( new RunLast(), new DefaultExceptionHandler<List<Object>>(), new String[] {}); assertEquals("[arg0, arg1]", firstExecutionResultWithParametersGiven.get(0)); assertEquals("null", secondExecutionResultWithoutParameters.get(0)); }
@Test public void testOptionalListParameterShouldNotRememberValuesInCommandMethods() { @Command() class TestCommand { @Command(name="method") public String methodCommand(@Parameters(arity="0..*") List<String> methodValues) { return methodValues == null ? "null" : methodValues.toString(); } } CommandLine commandLine = new CommandLine(new TestCommand()); // problematic for @Command-method @Parameters List<Object> methodFirstExecutionResultWithParametersGiven = commandLine.parseWithHandlers( new RunLast(), new DefaultExceptionHandler<List<Object>>(), new String[] {"method","arg0", "arg1"}); List<Object> methodSecondExecutionResultWithoutParameters = commandLine.parseWithHandlers( new RunLast(), new DefaultExceptionHandler<List<Object>>(), new String[] {"method"}); assertEquals("[arg0, arg1]", methodFirstExecutionResultWithParametersGiven.get(0)); // fails, still "[arg0, arg1]" assertEquals("null", methodSecondExecutionResultWithoutParameters.get(0)); }
cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
@Test public void testHandlerThrowsExecutionException2() { @Command class App { } IParseResultHandler2<Void> handler = new IParseResultHandler2<Void>() { public Void handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }; IExceptionHandler2<Void> exceptionHandler = new IExceptionHandler2<Void>() { public Void handleParseException(ParameterException ex, String[] args) { return null; } public Void handleExecutionException(ExecutionException ex, ParseResult parseResult) { return null; } }; try { new CommandLine(new App()).parseWithHandlers(handler, exceptionHandler, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.getMessage()); } }
@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)); }
@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 public void testExitCodeFromExceptionHandler2() { @Command class App implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "blah"); } } CustomExceptionHandler<List<Object>> handler = new CustomExceptionHandler<List<Object>>(); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), handler.andExit(25)); assertEquals(format("" + "blah%n" + "Usage: <main class>%n"), systemErrRule.getLog()); assertEquals(25, handler.exitCode); }
@Test public void testExitCodeFromExceptionHandler3() { @Command class App implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "blah"); } } CustomNoThrowExceptionHandler<List<Object>> handler = new CustomNoThrowExceptionHandler<List<Object>>(); new CommandLine(new App()).parseWithHandlers(new RunFirst().andExit(23), handler.andExit(25)); assertEquals(format("" + "blah%n" + "Usage: <main class>%n"), systemErrRule.getLog()); assertEquals(25, handler.exitCode); } static class CustomNoThrowExceptionHandler<R> extends DefaultExceptionHandler<R> {
@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)); }
/** @deprecated use {@link #parseWithHandler(IParseResultHandler2, String[])} instead * @since 2.0 */ @Deprecated public List<Object> parseWithHandler(IParseResultHandler handler, PrintStream out, String... args) { return parseWithHandlers(handler, out, Help.Ansi.AUTO, defaultExceptionHandler(), args); } /**
@Test public void handle() throws Exception { when(mockConfigOptionGetter.get()).thenReturn(temp.newFile()); final List<Object> result = configParsingHandler.handle(mockParseResult); verify(mockCommandLine).setDefaultValueProvider(any(IDefaultValueProvider.class)); verify(mockCommandLine).parseWithHandlers(eq(resultHandler), eq(exceptionHandler), anyString()); assertThat(result, is(empty())); }
@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)); }