@Test public void testUnmatchedArgsInitiallyEmpty() throws Exception { class SingleValue { @Parameters(index = "0..2") String[] str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()); assertTrue(cmd.getUnmatchedArguments().isEmpty()); CommandLine cmd2 = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); assertTrue(cmd2.getUnmatchedArguments().isEmpty()); }
@Test public void testTraceWarningIfUnmatchedArgsWhenUnmatchedArgumentsAllowed() throws Exception { PrintStream originalErr = System.err; ByteArrayOutputStream baos = new ByteArrayOutputStream(2500); System.setErr(new PrintStream(baos)); setTraceLevel("INFO"); class App { @Parameters(index = "0", arity = "2", split = "\\|", type = {Integer.class, String.class}) Map<Integer,String> message; } CommandLine cmd = new CommandLine(new App()).setUnmatchedArgumentsAllowed(true).parse("1=a", "2=b", "3=c", "4=d").get(0); assertEquals(Arrays.asList("3=c", "4=d"), cmd.getUnmatchedArguments()); System.setErr(originalErr); String expected = format("" + "[picocli INFO] Picocli version: %s%n" + "[picocli INFO] Parsing 4 command line args [1=a, 2=b, 3=c, 4=d]%n" + "[picocli INFO] Putting [1 : a] in LinkedHashMap<Integer, String> field java.util.Map<Integer, String> %s.message for args[0] at position 0%n" + "[picocli INFO] Putting [2 : b] in LinkedHashMap<Integer, String> field java.util.Map<Integer, String> %s.message for args[0] at position 0%n" + "[picocli INFO] Unmatched arguments: [3=c, 4=d]%n", CommandLine.versionString(), App.class.getName(), App.class.getName()); String actual = new String(baos.toByteArray(), "UTF8"); //System.out.println(actual); assertEquals(expected, actual); setTraceLevel("WARN"); }
@Test public void testCommandAllowsOnlyUnmatchedAnnotation() { setTraceLevel("OFF"); class App { @Unmatched String[] unmatched; } CommandLine cmd = new CommandLine(new App()); cmd.parse("a", "b"); assertEquals(Arrays.asList("a", "b"), cmd.getUnmatchedArguments()); }
@Test public void testUnmatchedAnnotationWithInstantiatedList() { setTraceLevel("OFF"); class App { @Unmatched List<String> unmatched = new ArrayList<String>(); @Option(names = "-o") String option; } App app = new App(); CommandLine commandLine = new CommandLine(app); commandLine.parse("-t", "-x", "abc"); assertEquals(Arrays.asList("-t", "-x", "abc"), commandLine.getUnmatchedArguments()); assertEquals(Arrays.asList("-t", "-x", "abc"), app.unmatched); }
@Test public void testUnmatchedAnnotationInstantiatesList() { setTraceLevel("OFF"); class App { @Unmatched List<String> unmatched; @Option(names = "-o") String option; } App app = new App(); CommandLine commandLine = new CommandLine(app); commandLine.parse("-t", "-x", "abc"); assertEquals(Arrays.asList("-t", "-x", "abc"), commandLine.getUnmatchedArguments()); assertEquals(Arrays.asList("-t", "-x", "abc"), app.unmatched); }
@Test public void testParseNestedSubCommandsAllowingUnmatchedArguments() { setTraceLevel("OFF"); List<CommandLine> result1 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("-a", "-b", "cmd1"); assertEquals(Arrays.asList("-b"), result1.get(0).getUnmatchedArguments()); List<CommandLine> result2 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("cmd1", "sub21"); assertEquals(Arrays.asList("sub21"), result2.get(1).getUnmatchedArguments()); List<CommandLine> result3 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("cmd1", "sub22sub1"); assertEquals(Arrays.asList("sub22sub1"), result3.get(1).getUnmatchedArguments()); List<CommandLine> result4 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("sub11"); assertEquals(Arrays.asList("sub11"), result4.get(0).getUnmatchedArguments()); List<CommandLine> result5 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("sub21"); assertEquals(Arrays.asList("sub21"), result5.get(0).getUnmatchedArguments()); List<CommandLine> result6 = createNestedCommand().setUnmatchedArgumentsAllowed(true) .parse("sub22sub1"); assertEquals(Arrays.asList("sub22sub1"), result6.get(0).getUnmatchedArguments()); } /** Subcommand with default constructor */
@SuppressWarnings("deprecation") @Test public void testAnnotateMethod_noArg() throws Exception { setTraceLevel("OFF"); Method m = CommandLine.getCommandMethods(MethodApp.class, "run0").get(0); CommandLine cmd1 = new CommandLine(m); assertEquals("run-0", cmd1.getCommandName()); assertEquals(Arrays.asList(), cmd1.getCommandSpec().args()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); cmd1.parseWithHandler(((IParseResultHandler) null), new PrintStream(baos), new String[]{"--y"}); assertEquals(Arrays.asList("--y"), cmd1.getUnmatchedArguments()); // test execute Object ret = CommandLine.invoke(m.getName(), MethodApp.class, new PrintStream(new ByteArrayOutputStream())); assertNull("return value", ret); setTraceLevel("WARN"); } @Test
@Test public void testBooleanOptionsArity0_nShortFormFailsIfAttachedParamNotABooleanWithUnmatchedArgsAllowed() { // ignores varargs setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new BooleanOptionsArity0_nAndParameters()).setUnmatchedArgumentsAllowed(true); cmd.parse("-rv234 -bool".split(" ")); assertEquals(Arrays.asList("-234"), cmd.getUnmatchedArguments()); } @Test
@Test public void testCompactFieldsWithUnmatchedArguments() { setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new CompactFields()).setUnmatchedArgumentsAllowed(true); cmd.parse("-oout -r -vp1 p2".split(" ")); assertEquals(Arrays.asList("-p1"), cmd.getUnmatchedArguments()); }
@Test public void testUnmatchedAnnotationInstantiatesArray() { setTraceLevel("OFF"); class App { @Unmatched String[] unmatched; @Option(names = "-o") String option; } App app = new App(); CommandLine commandLine = new CommandLine(app); commandLine.parse("-t", "-x", "abc"); assertEquals(Arrays.asList("-t", "-x", "abc"), commandLine.getUnmatchedArguments()); assertArrayEquals(new String[]{"-t", "-x", "abc"}, app.unmatched); }
@Test public void testEmptyModelParse() { setTraceLevel("OFF"); CommandSpec spec = CommandSpec.create(); CommandLine commandLine = new CommandLine(spec); commandLine.setUnmatchedArgumentsAllowed(true); commandLine.parse("-p", "123", "abc"); assertEquals(Arrays.asList("-p", "123", "abc"), commandLine.getUnmatchedArguments()); }
@Test public void testMultipleUnmatchedAnnotations() { setTraceLevel("OFF"); class App { @Unmatched String[] unmatched1; @Unmatched String[] unmatched2; @Unmatched List<String> unmatched3; @Unmatched List<String> unmatched4; @Option(names = "-o") String option; } App app = new App(); CommandLine commandLine = new CommandLine(app); commandLine.parse("-t", "-x", "abc"); assertEquals(Arrays.asList("-t", "-x", "abc"), commandLine.getUnmatchedArguments()); assertArrayEquals(new String[]{"-t", "-x", "abc"}, app.unmatched1); assertArrayEquals(new String[]{"-t", "-x", "abc"}, app.unmatched2); assertEquals(Arrays.asList("-t", "-x", "abc"), app.unmatched3); assertEquals(Arrays.asList("-t", "-x", "abc"), app.unmatched4); }
assertEquals(Arrays.asList("-yy"), cmd.getUnmatchedArguments()); assertEquals("A", ((App) cmd.getCommand()).first); assertEquals(Arrays.asList("-y"), cmd.getUnmatchedArguments()); assertEquals("B", ((App) cmd.getCommand()).second); assertEquals(Arrays.asList("--y"), cmd.getUnmatchedArguments()); assertEquals("C", ((App) cmd.getCommand()).third);
@Test public void testPositionalParamsUnknownArgumentMultiValueWithUnmatchedArgsAllowed() throws Exception { class SingleValue { @Parameters(index = "0..2") String[] str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); cmd.parse("val0", "val1", "val2", "val3"); assertArrayEquals(new String[]{"val0", "val1", "val2"}, ((SingleValue)cmd.getCommand()).str); assertEquals(Arrays.asList("val3"), cmd.getUnmatchedArguments()); }
@Test public void testPositionalParamsUnknownArgumentSingleValueWithUnmatchedArgsAllowed() throws Exception { class SingleValue { @Parameters(index = "0") String str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); cmd.parse("val1", "val2"); assertEquals("val1", ((SingleValue)cmd.getCommand()).str); assertEquals(Arrays.asList("val2"), cmd.getUnmatchedArguments()); }
@Test public void testIfSeparatorSetTheDefaultSeparatorIsNotRecognizedWithUnmatchedArgsAllowed() { @Command(separator = ":") class App { @Option(names = "--opt", required = true) String opt; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new App()).setUnmatchedArgumentsAllowed(true); try { cmd.parse("--opt=abc"); fail("Expected MissingParameterException"); } catch (MissingParameterException ok) { assertEquals("Missing required option '--opt:<opt>'", ok.getMessage()); assertEquals(Arrays.asList("--opt=abc"), cmd.getUnmatchedArguments()); } } @Test
@Test public void testMapPositionalParameterFieldMaxArity() { class App { @Parameters(index = "0", arity = "2", type = {Integer.class, String.class}) Map<Integer,String> message; } try { CommandLine.populateCommand(new App(), "1=a", "2=b", "3=c", "4=d"); fail("UnmatchedArgumentsException expected"); } catch (UnmatchedArgumentException ex) { assertEquals("Unmatched arguments: 3=c, 4=d", ex.getMessage()); } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new App()).setUnmatchedArgumentsAllowed(true); cmd.parse("1=a", "2=b", "3=c", "4=d"); assertEquals(Arrays.asList("3=c", "4=d"), cmd.getUnmatchedArguments()); } @Test
@Test public void testMapPositionalParameterFieldArity3() { class App { @Parameters(index = "0", arity = "3", type = {Integer.class, String.class}) Map<Integer,String> message; } try { CommandLine.populateCommand(new App(), "1=a", "2=b", "3=c", "4=d"); fail("UnmatchedArgumentsException expected"); } catch (UnmatchedArgumentException ex) { assertEquals("Unmatched argument: 4=d", ex.getMessage()); } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new App()).setUnmatchedArgumentsAllowed(true); cmd.parse("1=a", "2=b", "3=c", "4=d"); assertEquals(Arrays.asList("4=d"), cmd.getUnmatchedArguments()); } @Test
@Test public void testStopAtUnmatched_UnmatchedOption() { setTraceLevel("OFF"); class App { @Option(names = "-a") String first; @Parameters String[] positional; } App cmd1 = new App(); CommandLine commandLine1 = new CommandLine(cmd1).setStopAtUnmatched(true); commandLine1.parse("--y", "-a=abc", "positional"); assertEquals(Arrays.asList("--y", "-a=abc", "positional"), commandLine1.getUnmatchedArguments()); assertNull(cmd1.first); assertNull(cmd1.positional); try { // StopAtUnmatched=false, UnmatchedArgumentsAllowed=false new CommandLine(new App()).parse("--y", "-a=abc", "positional"); } catch (UnmatchedArgumentException ex) { assertEquals("Unknown option: --y", ex.getMessage()); } App cmd2 = new App(); CommandLine commandLine2 = new CommandLine(cmd2).setStopAtUnmatched(false).setUnmatchedArgumentsAllowed(true); commandLine2.parse("--y", "-a=abc", "positional"); assertEquals(Arrays.asList("--y"), commandLine2.getUnmatchedArguments()); assertEquals("abc", cmd2.first); assertArrayEquals(new String[]{"positional"}, cmd2.positional); setTraceLevel("WARN"); }
@Test public void testCommandAllowsOnlyUnmatchedAnnotation() { setTraceLevel("OFF"); class App { @Unmatched String[] unmatched; } CommandLine cmd = new CommandLine(new App()); cmd.parse("a", "b"); assertEquals(Arrays.asList("a", "b"), cmd.getUnmatchedArguments()); }