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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#add() . 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: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPositionalClearCustomSetterBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    final List<Object> values = new ArrayList<Object>();
    ISetter setter = new ISetter() {
        public <T> T set(T value) {
            values.add(value);
            return null;
        }
    };
    cmd.add(PositionalParamSpec.builder().type(String.class).setter(setter).build());

    CommandLine cl = new CommandLine(cmd);
    assertTrue(values.isEmpty());
    cl.parseArgs("1");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("1", values.get(1));

    values.clear();
    cl.parseArgs("2");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("2", values.get(1));
}
 
Example 2
Source File: ModelPositionalParamSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterHasCommand() {
    class App {
        @Parameters(index="0") int x;
    }

    CommandSpec cmd = new CommandLine(new App()).getCommandSpec();
    CommandSpec cmd2 = new CommandLine(new App()).getCommandSpec();
    PositionalParamSpec param = cmd.positionalParameters().get(0);
    assertEquals(cmd, param.command());
    PositionalParamSpec param1 = PositionalParamSpec.builder().index("1").build();
    assertEquals(null, param1.command());
    cmd.add(param1);
    assertEquals(cmd, param1.command());
    cmd2.add(param1);
    assertEquals(cmd2, param1.command());
    assertEquals(param1, cmd.positionalParameters().get(1));
}
 
Example 3
Source File: ModelOptionSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testOptionHasCommand() {
    class App {
        @Option(names = "-x") int x;
    }

    CommandSpec cmd = new CommandLine(new App()).getCommandSpec();
    CommandSpec cmd2 = new CommandLine(new App()).getCommandSpec();
    OptionSpec optx = cmd.findOption('x');
    assertEquals(cmd, optx.command());
    OptionSpec opty = OptionSpec.builder("-y").arity("1").build();
    assertEquals(null, opty.command());
    cmd.add(opty);
    assertEquals(cmd, opty.command());
    cmd2.add(opty);
    assertEquals(cmd2, opty.command());
    assertEquals(opty, cmd.findOption('y'));
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveArg() {
    class MyApp {
        // hide this option on all OS's other than Darwin (OSX)
        @Option(names = {"-o", "--open"},
                description = "On OSX open the finder to the data dir that was downloaded.")
        boolean open = false;
    }
    MyApp myApp = new MyApp();
    CommandLine cmd = new CommandLine(myApp);

    String expectBefore = String.format("" +
            "Usage: <main class> [-o]%n" +
            "  -o, --open   On OSX open the finder to the data dir that was downloaded.%n");
    assertEquals(expectBefore, cmd.getUsageMessage(Ansi.OFF));

    // replace the old "--open" option with a different one that is hidden
    CommandSpec spec = cmd.getCommandSpec();
    OptionSpec old = spec.findOption("--open");
    OptionSpec newOpenOption = OptionSpec.builder(old).hidden(true).build();
    spec.remove(old);
    spec.add(newOpenOption);

    String expectAfter = String.format("" +
            "Usage: <main class>%n");
    assertEquals(expectAfter, cmd.getUsageMessage(Ansi.OFF));
    assertFalse(myApp.open);
    cmd.parseArgs("--open");
    assertTrue(myApp.open);
}
 
Example 5
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddOptionBeforeSub() {
    OptionSpec optA = OptionSpec.builder("-a").scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    spec.add(optA);
    assertFalse(optA.inherited());

    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    assertNotNull(spec.findOption("-a"));
    assertNotNull(sub.findOption("-a"));

    assertFalse(spec.findOption("-a").inherited());
    assertTrue(sub.findOption("-a").inherited());
}
 
Example 6
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddOptionAfterSub() {
    OptionSpec optA = OptionSpec.builder("-a").scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    spec.add(optA);
    assertFalse(optA.inherited());

    assertNotNull(spec.findOption("-a"));
    assertNotNull(sub.findOption("-a"));

    assertFalse(spec.findOption("-a").inherited());
    assertTrue(sub.findOption("-a").inherited());
}
 
Example 7
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddPositionalParamBeforeSub() {
    PositionalParamSpec positional = PositionalParamSpec.builder().scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    spec.add(positional);
    assertFalse(positional.inherited());

    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    assertFalse(spec.positionalParameters().isEmpty());
    assertFalse(sub.positionalParameters().isEmpty());

    assertFalse(spec.positionalParameters().get(0).inherited());
    assertTrue(sub.positionalParameters().get(0).inherited());
}
 
Example 8
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddPositionalParamAfterSub() {
    PositionalParamSpec positional = PositionalParamSpec.builder().scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    spec.add(positional);
    assertFalse(positional.inherited());

    assertFalse(spec.positionalParameters().isEmpty());
    assertFalse(sub.positionalParameters().isEmpty());

    assertFalse(spec.positionalParameters().get(0).inherited());
    assertTrue(sub.positionalParameters().get(0).inherited());
}
 
Example 9
Source File: CompletionCandidatesTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompletionCandidatesValues_forOptionSpec() {
    CommandSpec spec = CommandSpec.create();
    spec.add(OptionSpec.builder("-x").completionCandidates(Arrays.asList("x", "y", "z")).build());
    assertEquals(Arrays.asList("x", "y", "z"), extract(spec.findOption("x").completionCandidates()));
}
 
Example 10
Source File: CompletionCandidatesTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompletionCandidatesValues_forPositionalParamSpec() {
    CommandSpec spec = CommandSpec.create();
    spec.add(PositionalParamSpec.builder().completionCandidates(Arrays.asList("x", "y", "z")).build());
    assertEquals(Arrays.asList("x", "y", "z"), extract(spec.positionalParameters().get(0).completionCandidates()));
}