Java Code Examples for picocli.CommandLine.Model.CommandSpec#addOption()

The following examples show how to use picocli.CommandLine.Model.CommandSpec#addOption() . 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: ModelUnmatchedArgsBindingTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnmatchedArgsBinding_forStringArrayConsumer() {
    setTraceLevel("OFF");
    class ArrayBinding implements ISetter {
        String[] array;
        @SuppressWarnings("unchecked") public <T> T set(T value) {
            T old = (T) array;
            array = (String[]) value;
            return old;
        }
    }
    ArrayBinding setter = new ArrayBinding();
    CommandSpec cmd = CommandSpec.create();
    UnmatchedArgsBinding unmatched = UnmatchedArgsBinding.forStringArrayConsumer(setter);
    assertSame(setter, unmatched.setter());

    cmd.addUnmatchedArgsBinding(unmatched);
    cmd.addOption(CommandLine.Model.OptionSpec.builder("-x").build());
    CommandLine.ParseResult result = new CommandLine(cmd).parseArgs("-x", "a", "b", "c");

    assertEquals(Arrays.asList("a", "b", "c"), result.unmatched());
    assertArrayEquals(new String[]{"a", "b", "c"}, setter.array);
    assertSame(unmatched, cmd.unmatchedArgsBindings().get(0));
    assertEquals(1, cmd.unmatchedArgsBindings().size());
}
 
Example 2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubcommandNameNotOverwrittenWhenAddedToParent() {
    CommandSpec toplevel = CommandSpec.create();
    toplevel.addOption(OptionSpec.builder("-o").description("o option").build());

    CommandSpec sub = CommandSpec.create().name("SOMECOMMAND");
    sub.addOption(OptionSpec.builder("-x").description("x option").build());

    CommandLine commandLine = new CommandLine(toplevel);
    CommandLine subCommandLine = new CommandLine(sub);
    assertEquals("SOMECOMMAND", sub.name());
    assertEquals("SOMECOMMAND", subCommandLine.getCommandName());

    commandLine.addSubcommand("sub", subCommandLine);
    assertEquals("SOMECOMMAND", sub.name());
    assertEquals("SOMECOMMAND", subCommandLine.getCommandName());

    subCommandLine.usage(System.out);

    String expected = String.format("" +
            "Usage: <main class> SOMECOMMAND [-x]%n" +
            "  -x     x option%n");
    assertEquals(expected, systemOutRule.getLog());
}
 
Example 3
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testModelUsageHelp() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build());
    spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-c=COUNT] -f=FIXED(BOOLEAN)%n" +
            "  -c, --count=COUNT          number of times to execute%n" +
            "  -f, --fix=FIXED(BOOLEAN)   run with fixed option%n" +
            "  -h, --help                 show help and exit%n" +
            "  -V, --version              show help and exit%n");
    assertEquals(expected, actual);
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testClearArrayOptionOldValueBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").arity("2..3").initialValue(new String[] {"ABC"}).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1", "2", "3");
    assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption("x").getValue());
    assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption('x').getValue());

    cl.parseArgs("-x", "4", "5");
    assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue());
    assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption('x').getValue());

    cl.parseArgs();
    assertArrayEquals(new String[] {"ABC"}, (String[]) cmd.findOption("x").getValue());
    assertArrayEquals(new String[] {"ABC"}, (String[]) cmd.findOption('x').getValue());
}
 
Example 5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubcommandNameIsInitializedWhenAddedToParent() {
    CommandSpec toplevel = CommandSpec.create();
    toplevel.addOption(OptionSpec.builder("-o").description("o option").build());

    CommandSpec sub = CommandSpec.create();
    sub.addOption(OptionSpec.builder("-x").description("x option").build());

    CommandLine commandLine = new CommandLine(toplevel);
    CommandLine subCommandLine = new CommandLine(sub);
    assertEquals("<main class>", sub.name());
    assertEquals("<main class>", subCommandLine.getCommandName());

    commandLine.addSubcommand("sub", subCommandLine);
    assertEquals("sub", sub.name());
    assertEquals("sub", subCommandLine.getCommandName());

    subCommandLine.usage(System.out);

    String expected = String.format("" +
            "Usage: <main class> sub [-x]%n" +
            "  -x     x option%n");
    assertEquals(expected, systemOutRule.getLog());
}
 
Example 6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testResemblesOption_WithOptionsNonDash() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.addOption(OptionSpec.builder("/x").build());

    spec.parser().unmatchedOptionsArePositionalParams(false);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));
    assertFalse(spec.resemblesOption("-a", tracer));
    assertTrue(spec.resemblesOption("/a", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
    assertFalse(spec.resemblesOption("-a", tracer));
    assertTrue(spec.resemblesOption("/a", tracer));
}
 
Example 7
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testResemblesOption_WithOptionsDash() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.addOption(OptionSpec.builder("-x").build());

    spec.parser().unmatchedOptionsArePositionalParams(false);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));
    assertTrue(spec.resemblesOption("-a", tracer));
    assertFalse(spec.resemblesOption("/a", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
    assertTrue(spec.resemblesOption("-a", tracer));
    assertFalse(spec.resemblesOption("/a", tracer));
}
 
Example 8
Source File: CommandSpecFactory.java    From milkman with MIT License 6 votes vote down vote up
private void addBooleanOption(CommandSpec cmdSpec, Option option) {
	cmdSpec.addOption(OptionSpec
			.builder("--"+option.getName(), "-"+option.getAlias())
			.description(option.getDescription())
			.arity("0")
			.required(false)
			.setter(new ISetter() {

				@Override
				public <T> T set(T value) throws Exception {
					option.setValue(Boolean.TRUE.equals(value));
					return null;
				}
				
			})
			.build());
}
 
Example 9
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithListAndAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(List.class).auxiliaryTypes(Integer.class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1", "2", "3");
    assertEquals(Arrays.asList(1, 2, 3), spec.optionsMap().get("-c").getValue());
}
 
Example 10
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithListWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(List.class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1", "2", "3");
    assertEquals(Arrays.asList("1", "2", "3"), spec.optionsMap().get("-c").getValue());
}
 
Example 11
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageMessageFromProgrammaticCommandSpecWithSplitRegexSynopsisLabel() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-x").type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.addPositional(PositionalParamSpec.builder().type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.mixinStandardHelpOptions(true);
    String actual = new CommandLine(spec).getUsageMessage(Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-x=PARAM]... PARAM...%n" +
            "      PARAM...%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x=PARAM%n");
    assertEquals(expected, actual);
}
 
Example 12
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithMapWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(Map.class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1=1.0", "2=2.0", "3=3.0");
    Map<String, String> expected = new LinkedHashMap<String, String>();
    expected.put("1", "1.0");
    expected.put("2", "2.0");
    expected.put("3", "3.0");
    assertEquals(expected, spec.optionsMap().get("-c").getValue());
}
 
Example 13
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithArray() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(int[].class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1", "2", "3");
    assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.optionsMap().get("-c").getValue());
}
 
Example 14
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testDontClearArrayOptionOldValueBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").arity("2..3").initialValue(new String[] {"ABC"}).hasInitialValue(false).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1", "2", "3");
    assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption("x").getValue());

    cl.parseArgs("-x", "4", "5");
    assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue());

    cl.parseArgs();
    assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue());
}
 
Example 15
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearListOptionOldValueBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").type(List.class).initialValue(Arrays.asList("ABC")).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1", "-x", "2", "-x", "3");
    assertEquals(Arrays.asList("1", "2", "3"), cmd.findOption("x").getValue());

    cl.parseArgs("-x", "4", "-x", "5");
    assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue());

    cl.parseArgs();
    assertEquals(Arrays.asList("ABC"), cmd.findOption("x").getValue());
}
 
Example 16
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testDontClearListOptionOldValueBeforeParseIfInitialValueFalse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").type(List.class).initialValue(Arrays.asList("ABC")).hasInitialValue(false).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1", "-x", "2", "-x", "3");
    assertEquals(Arrays.asList("1", "2", "3"), cmd.findOption("x").getValue());

    cl.parseArgs("-x", "4", "-x", "5");
    assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue());

    cl.parseArgs();
    assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue());
}
 
Example 17
Source File: ModelParseResultTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-V", "--verbose").build());
    CommandLine commandLine = new CommandLine(spec);

    ParseResult pr = commandLine.parseArgs("--verbose");

    assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line
    assertTrue(pr.hasMatchedOption('V'));     // single-character alias works too
    assertTrue(pr.hasMatchedOption("verbose"));   // command name without hyphens

    assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE));
    assertEquals("true", pr.matchedOption("verbose").stringValues().get(0));
}
 
Example 18
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testDontClearScalarOptionOldValueBeforeParseIfInitialValueFalse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").type(String.class).initialValue(null).hasInitialValue(false).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1");
    assertEquals("1", cmd.findOption("x").getValue());

    cl.parseArgs("-x", "2");
    assertEquals("2", cmd.findOption("x").getValue());

    cl.parseArgs();
    assertEquals("2", cmd.findOption("x").getValue());
}
 
Example 19
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelp_abbreviateSynopsisWithPositional() throws UnsupportedEncodingException {
    CommandSpec spec = CommandSpec.create();
    spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false);
    spec.addOption(OptionSpec.builder("-x").required(true).description("required").build());
    spec.addPositional(PositionalParamSpec.builder().arity("1").paramLabel("POSITIONAL").description("positional").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [OPTIONS] POSITIONAL%n" +
            "!     POSITIONAL   positional%n" +
            "! -x               required%n");
    assertEquals(expected, actual);
}
 
Example 20
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException {
    CommandSpec spec = CommandSpec.create();
    spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false);
    spec.addOption(OptionSpec.builder("-x").required(true).description("required").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [OPTIONS]%n" +
            "! -x     required%n");
    assertEquals(expected, actual);
}