picocli.CommandLine.UnmatchedArgumentException Java Examples

The following examples show how to use picocli.CommandLine.UnmatchedArgumentException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleValuePositionalCanBeConfiguredToConsumeUnknownOption() {
    class App {

        @Option(names = "-x") int x;
        @Parameters String y;
    }

    App app = new App();
    new CommandLine(app).setUnmatchedOptionsArePositionalParams(true).parseArgs("-x", "3", "-z");
    assertEquals(3, app.x);
    assertEquals("-z", app.y);

    CommandLine cmd = new CommandLine(new App()).setUnmatchedOptionsArePositionalParams(true);
    expect(cmd, "Unmatched argument at index 3: '4'", UnmatchedArgumentException.class, "-x", "3", "-z", "4");
}
 
Example #2
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testSplitInOptionCollection() {
    class Args {
        @Option(names = "-a", split = ",")
        List<String> values;
    }
    Args args = CommandLine.populateCommand(new Args(), "-a=a,b,c");
    assertEquals(Arrays.asList("a", "b", "c"), args.values);

    args = CommandLine.populateCommand(new Args(), "-a=a,b,c", "-a", "B", "-a=C");
    assertEquals(Arrays.asList("a", "b", "c", "B", "C"), args.values);

    args = CommandLine.populateCommand(new Args(), "-a", "a,b,c", "-a", "B", "-a", "C");
    assertEquals(Arrays.asList("a", "b", "c", "B", "C"), args.values);

    args = CommandLine.populateCommand(new Args(), "-a=a,b,c", "-a", "B", "-a", "C", "-a", "D,E,F");
    assertEquals(Arrays.asList("a", "b", "c", "B", "C", "D", "E", "F"), args.values);

    try {
        CommandLine.populateCommand(new Args(), "-a=a,b,c", "B", "C");
        fail("Expected UnmatchedArgumentException");
    } catch (UnmatchedArgumentException ok) {
        assertEquals("Unmatched arguments from index 1: 'B', 'C'", ok.getMessage());
    }
}
 
Example #3
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test //#639
public void testUnknownOptionAsOptionValue() {
    class App {
        @Option(names = {"-x", "--xvalue"})
        String x;

        @Option(names = {"-y", "--yvalues"}, arity = "1")
        List<String> y;
    }
    App app = new App();
    CommandLine cmd = new CommandLine(app).setUnmatchedOptionsAllowedAsOptionParameters(true);
    cmd.parseArgs("-x", "-unknown");
    assertEquals("-unknown", app.x);

    cmd.parseArgs("-y", "-unknown");
    assertEquals(Arrays.asList("-unknown"), app.y);

    cmd = new CommandLine(new App());
    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "Unknown option: '-unknown'; Expected parameter for option '--xvalue' but found '-unknown'", UnmatchedArgumentException.class, "-x", "-unknown");
    expect(cmd, "Unknown option: '-unknown'; Expected parameter for option '--yvalues' but found '-unknown'", UnmatchedArgumentException.class, "-y", "-unknown");
}
 
Example #4
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValueOptionArity2_NDoesNotConsumeUnknownOptionsByDefault() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y", arity = "2..N") String[] y;
    }

    CommandLine cmd = new CommandLine(new App());
    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "option '-y' at index 0 (<y>) requires at least 2 values, but only 1 were specified: [-z]",
            MissingParameterException.class, "-y", "-z");

    expect(cmd, "Unknown option: '-z'; Expected parameter 2 (of 2 mandatory parameters) for option '-y' but found '-z'",
            UnmatchedArgumentException.class, "-y", "1", "-z");

    expect(cmd, "Unknown option: '-z'; Expected parameter 3 (of 2 mandatory parameters) for option '-y' but found '-z'",
            UnmatchedArgumentException.class, "-y", "1", "2", "-z");
}
 
Example #5
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrintSuggestionsPrintStream() {
    CommandLine cmd = new CommandLine(new Demo.GitCommit());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("-fi"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    UnmatchedArgumentException.printSuggestions(ex, new PrintStream(baos));
    String expected = format("" +
            "Possible solutions: --fixup, --file%n");
    assertEquals(expected, baos.toString());
}
 
Example #6
Source File: TypeConversionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapArgumentsMustContainEquals2() {
    class App {
        @Parameters(split = "==") Map<String, String> map;
    }
    try {
        CommandLine.populateCommand(new App(), "a:c", "1:3");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 0: 'a:c', '1:3'", ex.getMessage());
    }
}
 
Example #7
Source File: InteractiveArgTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInteractivePositionalArity_0_1_DoesNotConsumeUnknownOption() throws IOException {
    class App {
        @Parameters(index = "0", arity = "0..1", interactive = true)
        char[] x;

        @Option(names = "-z")
        int z;

        @Parameters(index = "1")
        String[] remainder;
    }

    PrintStream out = System.out;
    InputStream in = System.in;
    try {
        System.setOut(new PrintStream(new ByteArrayOutputStream()));
        System.setIn(inputStream("123"));
        App app = new App();
        CommandLine cmd = new CommandLine(app);
        try {
            cmd.parseArgs("-y", "456", "abc");
            fail("Expect exception");
        } catch (UnmatchedArgumentException ex) {
            assertEquals("Unknown option: '-y'", ex.getMessage());
        }
    } finally {
        System.setOut(out);
        System.setIn(in);
    }
}
 
Example #8
Source File: InteractiveArgTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInteractivePositionalAsListOfCharArraysArity_0_1_DoesNotConsumeUnknownOption() throws IOException {
    class App {
        @Parameters(index = "0..1", arity = "0..1", interactive = true)
        List<char[]> x;

        @Option(names = "-z")
        int z;

        @Parameters(index = "2")
        String[] remainder;
    }

    PrintStream out = System.out;
    InputStream in = System.in;
    try {
        System.setOut(new PrintStream(new ByteArrayOutputStream()));
        System.setIn(inputStream("123"));
        App app = new App();
        CommandLine cmd = new CommandLine(app);
        try {
            cmd.parseArgs("-y", "-w", "456", "abc");
            fail("Expect exception");
        } catch (UnmatchedArgumentException ex) {
            assertEquals("Unknown options: '-y', '-w'", ex.getMessage());
        }
    } finally {
        System.setOut(out);
        System.setIn(in);
    }
}
 
Example #9
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleGroupsWithPositional() {
    class Issue1027 {
        @ArgGroup(exclusive = false, multiplicity = "1..4")
        List<StudentGrade> gradeList;

        @ArgGroup(exclusive = false, multiplicity = "1")
        List<StudentGrade> anotherList;
    }

    Issue1027 bean4 = new Issue1027();
    new CommandLine(bean4).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0".split(" "));
    assertEquals(4, bean4.gradeList.size());
    assertEquals(new StudentGrade("Abby", "4.0"),  bean4.gradeList.get(0));
    assertEquals(new StudentGrade("Billy", "3.5"), bean4.gradeList.get(1));
    assertEquals(new StudentGrade("Caily", "3.5"), bean4.gradeList.get(2));
    assertEquals(new StudentGrade("Danny", "4.0"), bean4.gradeList.get(3));

    assertEquals(1, bean4.anotherList.size());
    assertEquals(new StudentGrade("Abby", "4.0"),  bean4.anotherList.get(0));

    Issue1027 bean5 = new Issue1027();
    try {
        new CommandLine(bean5).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0 Egon 3.5".split(" "));
        fail("Expected exception");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 8: 'Egon', '3.5'", ex.getMessage());
    }
}
 
Example #10
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorNonNullList() {
    List<String> args = Arrays.asList("a", "b", "c");
    UnmatchedArgumentException ex = new UnmatchedArgumentException(new CommandLine(new Example()), args);
    assertEquals(args, ex.getUnmatched());
    assertNotSame(args, ex.getUnmatched());
}
 
Example #11
Source File: TypeConversionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapArgumentsMustContainEquals() {
    class App {
        @Parameters Map<String, String> map;
    }
    try {
        CommandLine.populateCommand(new App(), "a:c", "1:3");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 0: 'a:c', '1:3'", ex.getMessage());
    }
}
 
Example #12
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrintSuggestionsPrintStreamAutoFlushes() {
    CommandLine cmd = new CommandLine(new Demo.GitCommit());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("-fi"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    UnmatchedArgumentException.printSuggestions(ex, new PrintStream(new BufferedOutputStream(baos)));
    String expected = format("" +
            "Possible solutions: --fixup, --file%n");
    assertEquals(expected, baos.toString());
}
 
Example #13
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrintSuggestionsPrintWriter() {
    CommandLine cmd = new CommandLine(new Demo.GitCommit());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("-fi"));
    StringWriter sw = new StringWriter();
    UnmatchedArgumentException.printSuggestions(ex, new PrintWriter(sw));
    String expected = format("" +
            "Possible solutions: --fixup, --file%n");
    assertEquals(expected, sw.toString());
}
 
Example #14
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrintSuggestionsPrintWriterAutoFlushes() {
    CommandLine cmd = new CommandLine(new Demo.GitCommit());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("-fi"));
    StringWriter sw = new StringWriter();
    UnmatchedArgumentException.printSuggestions(ex, new PrintWriter(new BufferedWriter(sw)));
    String expected = format("" +
            "Possible solutions: --fixup, --file%n");
    assertEquals(expected, sw.toString());
}
 
Example #15
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnmatchedListWithNullElements() {
    CommandLine cmd = new CommandLine(new Demo.GitCommit());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("-fi", null));
    StringWriter sw = new StringWriter();
    UnmatchedArgumentException.printSuggestions(ex, new PrintWriter(sw));
    String expected = format("" +
            "Possible solutions: --fixup, --file%n");
    assertEquals(expected, sw.toString());
}
 
Example #16
Source File: UnmatchedArgumentExceptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiddenOptionsNotSuggested() {
    class MyApp {
        @Option(names = "--aaa", hidden = true) int a;
        @Option(names = "--apples", hidden = false) int apples;
        @Option(names = "--bbb", hidden = false) int b;
    }
    CommandLine cmd = new CommandLine(new MyApp());
    UnmatchedArgumentException ex = new UnmatchedArgumentException(cmd, Arrays.asList("--a", null));
    StringWriter sw = new StringWriter();
    UnmatchedArgumentException.printSuggestions(ex, new PrintWriter(sw));
    String expected = format("" +
            "Possible solutions: --apples%n");
    assertEquals(expected, sw.toString());
}
 
Example #17
Source File: DataDefender.java    From DataDefender with Apache License 2.0 5 votes vote down vote up
public int handleParseException(ParameterException ex, String[] args) {
    CommandLine cmd = ex.getCommandLine();
    PrintWriter writer = cmd.getErr();

    writer.println(ex.getMessage());
    UnmatchedArgumentException.printSuggestions(ex, writer);
    writer.print(cmd.getHelp().fullSynopsis()); // since 4.1

    CommandSpec spec = cmd.getCommandSpec();
    writer.printf("Try '%s --help' for more information.%n", spec.qualifiedName());

    return cmd.getExitCodeExceptionMapper() != null
                ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
                : spec.exitCodeOnInvalidInput();
}
 
Example #18
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamArityAloneIsInsufficient() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int.class).build();
    assertFalse(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    try {
        commandLine.parseArgs("1", "2", "3");
        fail("Expected exception");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 1: '2', '3'", ex.getMessage());
    }
}
 
Example #19
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionArityAloneIsInsufficient() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(int.class).build();
    assertFalse(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    try {
        commandLine.parseArgs("-c", "1", "2", "3");
        fail("Expected exception");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 2: '2', '3'", ex.getMessage());
    }
}
 
Example #20
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueVarArgOptionDoesNotConsumeUnknownOptionsByDefault() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y", arity = "*") String[] y;
    }

    CommandLine cmd = new CommandLine(new App());
    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "Unknown option: '-z'; Expected parameter for option '-y' but found '-z'", UnmatchedArgumentException.class, "-y", "-z");
    expect(cmd, "Unknown option: '-z'; Expected parameter for option '-y' but found '-z'", UnmatchedArgumentException.class, "-y", "3", "-z");
}
 
Example #21
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionArity2DoesNotConsumeUnknownOptionByDefault() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y", arity = "2") String[] y;
    }

    CommandLine cmd = new CommandLine(new App());
    expect(cmd, "option '-y' at index 0 (<y>) requires at least 2 values, but only 1 were specified: [-z]",
            MissingParameterException.class, "-y", "-z");

    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "Unknown option: '-z'; Expected parameter 2 (of 2 mandatory parameters) for option '-y' but found '-z'",
            UnmatchedArgumentException.class, "-y", "3", "-z");
}
 
Example #22
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionCanBeConfiguredToConsumeUnknownOption() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y") String[] y;
    }

    App app = new App();
    new CommandLine(app).setUnmatchedOptionsAllowedAsOptionParameters(true).parseArgs("-y", "-z");
    assertArrayEquals(new String[]{"-z"}, app.y);

    // arity=1 for multi-value options
    CommandLine cmd = new CommandLine(new App()).setUnmatchedOptionsAllowedAsOptionParameters(true);
    expect(cmd, "Unknown option: '-z'", UnmatchedArgumentException.class, "-y", "3", "-z");
}
 
Example #23
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionDoesNotConsumeUnknownOptionByDefault() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y") String[] y;
    }

    CommandLine cmd = new CommandLine(new App());
    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "Unknown option: '-z'; Expected parameter for option '-y' but found '-z'", UnmatchedArgumentException.class, "-y", "-z");
    expect(cmd, "Unknown option: '-z'", UnmatchedArgumentException.class, "-y", "3", "-z");
}
 
Example #24
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleValueOptionDoesNotConsumeUnknownOptionByDefault() {
    class App {
        @Option(names = "-x") int x;
        @Option(names = "-y") String y;
    }

    CommandLine cmd = new CommandLine(new App());
    cmd.setUnmatchedOptionsAllowedAsOptionParameters(false);
    expect(cmd, "Unknown option: '-z'; Expected parameter for option '-y' but found '-z'", UnmatchedArgumentException.class, "-y", "-z");
}
 
Example #25
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalArity2_NDoesNotConsumeUnknownOption() {
    class App {
        @Option(names = "-x") int x;
        @Parameters(arity = "2..*") String[] y;
    }

    expect(new App(), "positional parameter at index 0..* (<y>) requires at least 2 values, but none were specified.",
            MissingParameterException.class, "-x", "3", "-z");

    expect(new App(), "positional parameter at index 0..* (<y>) requires at least 2 values, but only 1 were specified: [4]",
            MissingParameterException.class, "-x", "3", "-z", "4");

    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "4", "-z");
}
 
Example #26
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueVarArgPositionalDoesNotConsumeUnknownOption() {
    class App {
        @Option(names = "-x") int x;
        @Parameters(arity = "*") String[] y;
    }

    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "-z");
    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "-z", "4");

    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "4", "-z");
}
 
Example #27
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueVarArgPositionalDoesNotConsumeUnknownOptionSimple() {
    class App {
        @Parameters(arity = "*") String[] y;
    }

    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-z", "4");
    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "4", "-z");
}
 
Example #28
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalDoesNotConsumeUnknownOption() {
    class App {
        @Option(names = "-x") int x;
        @Parameters String[] y;
    }

    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "-z");
    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "-z", "4");
    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "4", "-z");
}
 
Example #29
Source File: UnmatchedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleValuePositionalDoesNotConsumeUnknownOption() {
    class App {
        @Option(names = "-x") int x;
        @Parameters String y;
    }

    expect(new App(), "Missing required parameter: '<y>'", MissingParameterException.class, "-x", "3", "-z");
    expect(new App(), "Unknown option: '-z'", UnmatchedArgumentException.class, "-x", "3", "-z", "4");
}
 
Example #30
Source File: PicoCLIOptionsImplTest.java    From besu with Apache License 2.0 4 votes vote down vote up
@Test
public void testNotExistantOptionsFail() {
  assertThatExceptionOfType(UnmatchedArgumentException.class)
      .isThrownBy(() -> commandLine.parseArgs("--does-not-exist", "1"));
}