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.
/** * 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 testAnyExceptionWrappedInParameterException() { class App { @Option(names = "-queue", type = String.class, split = ",") ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(2); } try { CommandLine.populateCommand(new App(), "-queue a,b,c".split(" ")); fail("ParameterException expected"); } catch (ParameterException ex) { assertEquals("IllegalStateException: Queue full while processing argument at or before arg[1] 'a,b,c' in [-queue, a,b,c]: java.lang.IllegalStateException: Queue full", ex.getMessage()); } }
@Test public void testTimeFormatHHmmsssInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:587"); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Time': '23:59:587' is not a HH:mm[:ss[.SSS]] time", expected.getMessage()); } } @Test
@Test public void testTimeFormatHHmmssSSSInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:58;123"); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Time': '23:59:58;123' is not a HH:mm[:ss[.SSS]] time", expected.getMessage()); } } @Test
@Test public void testIntFieldsAreDecimal() { try { CommandLine.populateCommand(new SupportedTypes(), "-int", "0xFF", "-Integer", "0xFFFF"); fail("Should fail on hex input"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-int': '0xFF' is not an int", expected.getMessage()); } } @Test
@Test public void testRangeArityMismatchingTypeCount() { class InvalidMapTypes { @Option(names = "-D", arity = "1..3", type = Integer.class) // invalid: only one type TreeMap<Integer, String> map; } try { CommandLine.populateCommand(new InvalidMapTypes(), "-D", "1=a"); fail("expect exception"); } catch (ParameterException ex) { assertEquals("field java.util.TreeMap<Integer, String> picocli.CommandLineArityTest$1InvalidMapTypes.map needs two types (one for the map key, one for the value) but only has 1 types configured.", ex.getMessage()); } }
@Test public void testBooleanOptionsArity1_nErrorIfValueMissing() { try { CommandLine.populateCommand(new BooleanOptionsArity1_nAndParameters(), "-bool".split(" ")); fail("Missing param was accepted for boolean with arity=1"); } catch (CommandLine.ParameterException expected) { assertEquals("Missing required parameter for option '-bool' at index 0 (<aBoolean>)", expected.getMessage()); } }
@Test public void testBooleanOptionsArity1_nErrorIfValueNotTrueOrFalse() { // ignores varargs try { CommandLine.populateCommand(new BooleanOptionsArity1_nAndParameters(), "-bool abc".split(" ")); fail("Invalid format abc was accepted for boolean"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-bool': 'abc' is not a boolean", expected.getMessage()); } } @Test
@Test public void testDateFormatYYYYmmddInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Date", "20170131"); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Date': '20170131' is not a yyyy-MM-dd date", expected.getMessage()); } } @Test
@Test public void testBooleanOptionsArity0_nShortFormFailsIfAttachedWithSepParamNotABoolean() { // ignores varargs try { CommandLine.populateCommand(new BooleanOptionsArity0_nAndParameters(), "-rv=234 -bool".split(" ")); fail("was able to assign 234 to boolean"); } catch (CommandLine.ParameterException ex) { assertEquals("Invalid value for option '-other': '234' is not a boolean", ex.getMessage()); } }
@Test public void testTimeFormatHHmmssSSSSInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:58.1234"); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Time': '23:59:58.1234' is not a HH:mm[:ss[.SSS]] time", expected.getMessage()); } } @Test
@Test public void testShortFieldsAreDecimal() { try { CommandLine.populateCommand(new SupportedTypes(), "-short", "0xFF", "-Short", "0x6FFE"); fail("Should fail on hex input"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-short': '0xFF' is not a short", expected.getMessage()); } } @Test
@Test public void testBooleanOptionsArity0_nFailsIfAttachedParamNotABoolean() { // ignores varargs try { CommandLine.populateCommand(new BooleanOptionsArity0_nAndParameters(), "-bool=123 -other".split(" ")); fail("was able to assign 123 to boolean"); } catch (CommandLine.ParameterException ex) { assertEquals("Invalid value for option '-bool': '123' is not a boolean", ex.getMessage()); } } @Test
@Test public void testShortOptionsWithSeparatorButNoValueFailsIfValueRequired() { try { CommandLine.populateCommand(new CompactFields(), "-rvo=".split(" ")); fail("Expected exception"); } catch (ParameterException ex) { assertEquals("Missing required parameter for option '-o' (<outputFile>)", ex.getMessage()); } } @Test
@Test public void testParserCaseInsensitiveEnumValuesAllowed_invalidInput() throws Exception { PojoWithEnumOptions fields = new PojoWithEnumOptions(); CommandLine cmd = new CommandLine(fields).setCaseInsensitiveEnumValuesAllowed(true); try { cmd.parse("-u=millisecondINVALID"); fail("Expected exception"); } catch (ParameterException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Invalid value for option '-u': expected one of ")); } }
@Test public void testTimeFormatHHmmssColonInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:"); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Time': '23:59:' is not a HH:mm[:ss[.SSS]] time", expected.getMessage()); } } @Test
@Test public void testByteFieldsAreDecimal() { try { CommandLine.populateCommand(new SupportedTypes(), "-byte", "0x1F", "-Byte", "0x0F"); fail("Should fail on hex input"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-byte': '0x1F' is not a byte", expected.getMessage()); } } @Test
@Test public void testTimeFormatHHmmssDotInvalidError() throws ParseException { try { CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:58."); fail("Invalid format was accepted"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-Time': '23:59:58.' is not a HH:mm[:ss[.SSS]] time", expected.getMessage()); } } @Test
@Test public void testLongFieldsAreDecimal() { try { CommandLine.populateCommand(new SupportedTypes(), "-long", "0xAABBCC", "-Long", "0xAABBCCDD"); fail("Should fail on hex input"); } catch (CommandLine.ParameterException expected) { assertEquals("Invalid value for option '-long': '0xAABBCC' is not a long", expected.getMessage()); } } @Test