/** * 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); } }
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 List<Object> handleException(ParameterException ex, PrintStream out, Help.Ansi ansi, String... args) { internalHandleParseException(ex, out, ansi, args); return Collections.<Object>emptyList(); }
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;
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;
return parseWithHandlers(handler, new DefaultExceptionHandler<R>(), args);
cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
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);
cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
@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)); }
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 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 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)); }
/** Convenience method that returns {@code new DefaultExceptionHandler<List<Object>>()}. */ public static DefaultExceptionHandler<List<Object>> defaultExceptionHandler() { return new DefaultExceptionHandler<List<Object>>(); }
CommandLine.Model.CommandSpec parseCommand(final String... args) { final TestPantheonCommand pantheonCommand = new TestPantheonCommand( mockLogger, mockBlockImporter, mockRunnerBuilder, mockControllerBuilder, mockSyncConfBuilder); // parse using Ansi.OFF to be able to assert on non formatted output results pantheonCommand.parse( new RunLast().useOut(outPrintStream).useAnsi(Ansi.OFF), new DefaultExceptionHandler<List<Object>>().useErr(errPrintStream).useAnsi(Ansi.OFF), args); return pantheonCommand.spec; }
private DefaultExceptionHandler<List<Object>> defaultExceptionHandler() { return new DefaultExceptionHandler<List<Object>>(); }
@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 List<Object> handleException(ParameterException ex, PrintStream out, Help.Ansi ansi, String... args) { internalHandleParseException(ex, out, ansi, args); return Collections.<Object>emptyList(); }
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); } }
public static Optional<ExitCode> parse(final String... args) { final CommandLine cli = new CommandLine(new Cli()); final List<?> o = cli.parseWithHandlers(new CommandLine.RunLast().useAnsi(CommandLine.Help.Ansi.ON), CommandLine.defaultExceptionHandler() .useAnsi(CommandLine.Help.Ansi.OFF), args); if (o == null) { return Optional.of(ExitCode.NO_OPERATION); } else { return Optional.ofNullable((ExitCode) o.get(0)); } }