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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#addSubcommand() . 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: AbstractCommandSpecProcessor.java    From picocli with Apache License 2.0 6 votes vote down vote up
private void updateCommandFromMethodElement(ExecutableElement method, Context context, CommandSpec result, RoundEnvironment roundEnv) {
    debugMethod(method);
    result.withToString(method.getEnclosingElement().asType().toString() + "." + method.getSimpleName());

    updateCommandSpecFromCommandAnnotation(result, method, context, roundEnv);
    result.setAddMethodSubcommands(false); // must reset: true by default in the @Command annotation

    // set command name to method name, unless @Command#name is set
    if (result.name().equals(COMMAND_DEFAULT_NAME)) {
        result.name(method.getSimpleName().toString());
    }

    // add this commandSpec as a subcommand to its parent
    if (isSubcommand(method, roundEnv)) {
        CommandSpec commandSpec = buildCommand(method.getEnclosingElement(), context, roundEnv);
        commandSpec.addSubcommand(result.name(), result);
    }
    buildOptionsAndPositionalsFromMethodParameters(method, result, context);
}
 
Example 2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionHelp_helpCommand() {
    CommandSpec helpCommand = CommandSpec.create().helpCommand(true);
    assertTrue(helpCommand.helpCommand());

    CommandSpec parent = CommandSpec.create().addOption(OptionSpec.builder("-x").type(String.class).required(true).build());
    parent.addSubcommand("help", helpCommand);

    CommandLine commandLine = new CommandLine(parent);
    commandLine.parseArgs("help"); // no missing param exception

    try {
        commandLine.parseArgs();
    } catch (MissingParameterException ex) {
        assertEquals("Missing required option: '-x=PARAM'", ex.getMessage());
        assertEquals(1, ex.getMissing().size());
        assertSame(ex.getMissing().get(0).toString(), parent.posixOptionsMap().get('x'), ex.getMissing().get(0));
    }
}
 
Example 3
Source File: AbstractCommandSpecProcessor.java    From picocli with Apache License 2.0 5 votes vote down vote up
private void updateCommandSpecFromCommandAnnotation(CommandSpec result,
                                                    Element element,
                                                    Context context,
                                                    RoundEnvironment roundEnv) {
    Command cmd = element.getAnnotation(Command.class);
    if (cmd != null) {
        updateCommandAttributes(result, cmd);

        List<CommandSpec> subcommands = findSubcommands(element.getAnnotationMirrors(), context, roundEnv);
        for (CommandSpec sub : subcommands) {
            result.addSubcommand(sub.name(), sub);
        }
        if (cmd.mixinStandardHelpOptions()) {
            context.commandsRequestingStandardHelpOptions.add(result);
        }
    }
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_DisallowsDuplicateSubcommandNames() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);

    spec.addSubcommand("a", new CommandLine(sub));
    try {
        spec.addSubcommand("a", new CommandLine(sub));
    } catch (InitializationException ex) {
        assertEquals("Another subcommand named 'a' already exists for command '<main class>'", ex.getMessage());
    }
}
 
Example 5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_DisallowsDuplicateSubcommandAliases() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);

    spec.addSubcommand("a", new CommandLine(sub));

    CommandSpec sub2 = CommandSpec.wrapWithoutInspection(null);
    sub2.aliases("a");
    try {
        spec.addSubcommand("x", new CommandLine(sub2));
    } catch (InitializationException ex) {
        assertEquals("Alias 'a' for subcommand 'x' is already used by another subcommand of '<main class>'", ex.getMessage());
    }
}
 
Example 6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_SubcommandInheritsResourceBundle() {
    ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages");
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.resourceBundle(rb);
    assertSame(rb, spec.resourceBundle());

    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);
    spec.addSubcommand("a", new CommandLine(sub));

    assertSame(rb, sub.resourceBundle());
}
 
Example 7
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 8
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 9
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 10
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());
}