/** * If a ParameterException occurs during {@link #parseScriptArguments(CommandLine, String[])} * then this method gets called to report the problem. * The default behavior is to show the exception message using {@link #printErrorMessage(String)}, * then call {@link #printHelpMessage(CommandLine)}. * The return value becomes the return value for the Script.run which will be the exit code * if we've been called from the command line. * * @param pe The ParameterException that occurred * @param args The argument array * @return The value that Script.run should return. This implementation returns 1, subclasses may override. */ public Object handleParameterException(ParameterException pe, String[] args) { printErrorMessage(String.format("args: %s%n%s", Arrays.toString(args), pe.getMessage())); printHelpMessage(pe.getCommandLine()); return 1; }
private void internalHandleParseException(ParameterException ex, PrintStream out, Help.Ansi ansi, String[] args) { out.println(ex.getMessage()); if (!UnmatchedArgumentException.printSuggestions(ex, out)) { ex.getCommandLine().usage(out, ansi); } } /** This implementation always simply rethrows the specified exception.
@Test public void testSetFailsIfObjectNotSet_ForSetterMethod() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof NullPointerException); } }
parse("sub1 -x abc".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Top); parse("-o OPT sub1 -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub1); parse("-o OPT sub2 -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub2);
@Test public void testIssue207ParameterExceptionProvidesAccessToFailedCommand_Declarative() { @Command(subcommands = {Sub207A.class, Sub207B.class}) class Top { @Option(names = "-o", required = true) String option; } try { new CommandLine(new Top()).parse("sub207A -x abc".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Top); } try { new CommandLine(new Top()).parse("-o OPT sub207A -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub207A); } try { new CommandLine(new Top()).parse("-o OPT sub207B -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub207B); } List<CommandLine> parsed = new CommandLine(new Top()). parse("-o OPT sub207A -x ABC".split(" ")); assertEquals(2, parsed.size()); assertEquals("OPT", ((Top) parsed.get(0).getCommand()).option); assertEquals("ABC", ((Sub207A) parsed.get(1).getCommand()).x); }
/** * TokenTypes.properties generator entry point. * @param args the command line arguments * @throws CheckstyleException if parser or lexer failed or if there is an IO problem **/ public static void main(String... args) throws CheckstyleException { final CliOptions cliOptions = new CliOptions(); final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH); try { final ParseResult parseResult = cmd.parseArgs(args); if (parseResult.isUsageHelpRequested()) { cmd.usage(System.out); } else { writePropertiesFile(cliOptions); } } catch (ParameterException ex) { System.err.println(ex.getMessage()); ex.getCommandLine().usage(System.err); } }
@Test public void testExceptionHandlingCreatesCommandLineIfNecessary() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); assertNull(spec.commandLine()); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { assertNotNull(spec.commandLine()); // has been set ParameterException pex = (ParameterException) ex; assertSame(pex.getCommandLine(), spec.commandLine()); assertSame(spec, pex.getCommandLine().getCommandSpec()); } } }
/** * If a ParameterException occurs during {@link #parseScriptArguments(CommandLine, String[])} * then this method gets called to report the problem. * The default behavior is to show the exception message using {@link #printErrorMessage(String)}, * then call {@link #printHelpMessage(CommandLine)}. * The return value becomes the return value for the Script.run which will be the exit code * if we've been called from the command line. * * @param pe The ParameterException that occurred * @param args The argument array * @return The value that Script.run should return. This implementation returns 1, subclasses may override. */ public Object handleParameterException(ParameterException pe, String[] args) { printErrorMessage(String.format("args: %s%n%s", Arrays.toString(args), pe.getMessage())); printHelpMessage(pe.getCommandLine()); return 1; }
@Test public void testSetInvokesMethod_FailsForGetterMethod() throws Exception { Method getX = ModelMethodBindingBean.class.getDeclaredMethod("getX"); getX.setAccessible(true); ModelMethodBindingBean bean = new ModelMethodBindingBean(); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(bean, getX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof IllegalArgumentException); assertEquals("wrong number of arguments", pex.getCause().getMessage()); } }
@Test public void testExceptionHandlingUsesCommandLineIfAvailable() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); CommandLine cmd = new CommandLine(spec); spec.commandLine(cmd); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(cmd, pex.getCommandLine()); } }
@Test public void testSetFailsIfObjectNotSet_ForSetterMethod() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof NullPointerException); } }
private void internalHandleParseException(ParameterException ex, PrintStream out, Help.Ansi ansi, String[] args) { out.println(ex.getMessage()); if (!UnmatchedArgumentException.printSuggestions(ex, out)) { ex.getCommandLine().usage(out, ansi); } } /** This implementation always simply rethrows the specified exception.
parse("sub1 -x abc".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Top); parse("-o OPT sub1 -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub1); parse("-o OPT sub2 -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub2);
ex.getCommandLine().usage(System.err);
@Test public void testIssue207ParameterExceptionProvidesAccessToFailedCommand_Declarative() { @Command(subcommands = {Sub207A.class, Sub207B.class}) class Top { @Option(names = "-o", required = true) String option; } try { new CommandLine(new Top()).parse("sub207A -x abc".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Top); } try { new CommandLine(new Top()).parse("-o OPT sub207A -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub207A); } try { new CommandLine(new Top()).parse("-o OPT sub207B -wrong ABC".split(" ")); } catch (ParameterException ex) { assertTrue(ex.getCommandLine().getCommand() instanceof Sub207B); } List<CommandLine> parsed = new CommandLine(new Top()). parse("-o OPT sub207A -x ABC".split(" ")); assertEquals(2, parsed.size()); assertEquals("OPT", ((Top) parsed.get(0).getCommand()).option); assertEquals("ABC", ((Sub207A) parsed.get(1).getCommand()).x); }
@Test public void testExceptionHandlingCreatesCommandLineIfNecessary() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); assertNull(spec.commandLine()); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { assertNotNull(spec.commandLine()); // has been set ParameterException pex = (ParameterException) ex; assertSame(pex.getCommandLine(), spec.commandLine()); assertSame(spec, pex.getCommandLine().getCommandSpec()); } } }
@Test public void testSetInvokesMethod_FailsForGetterMethod() throws Exception { Method getX = ModelMethodBindingBean.class.getDeclaredMethod("getX"); getX.setAccessible(true); ModelMethodBindingBean bean = new ModelMethodBindingBean(); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(bean, getX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof IllegalArgumentException); assertEquals("wrong number of arguments", pex.getCause().getMessage()); } }
@Test public void testExceptionHandlingUsesCommandLineIfAvailable() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); CommandLine cmd = new CommandLine(spec); spec.commandLine(cmd); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(cmd, pex.getCommandLine()); } }
/** * TokenTypes.properties generator entry point. * @param args the command line arguments * @throws CheckstyleException if parser or lexer failed or if there is an IO problem **/ public static void main(String... args) throws CheckstyleException { final CliOptions cliOptions = new CliOptions(); final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH); try { final ParseResult parseResult = cmd.parseArgs(args); if (parseResult.isUsageHelpRequested()) { cmd.usage(System.out); } else { writePropertiesFile(cliOptions); } } catch (ParameterException ex) { System.err.println(ex.getMessage()); ex.getCommandLine().usage(System.err); } }