picocli.CommandLine.Spec Java Examples

The following examples show how to use picocli.CommandLine.Spec. 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 boolean isSubcommand(ExecutableElement method, RoundEnvironment roundEnv) {
    Element typeElement = method.getEnclosingElement();
    if (typeElement.getAnnotation(Command.class) != null && typeElement.getAnnotation(Command.class).addMethodSubcommands()) {
        return true;
    }
    if (typeElement.getAnnotation(Command.class) == null) {
        Set<Element> elements = new HashSet<Element>(typeElement.getEnclosedElements());

        // The class is a Command if it has any fields or methods annotated with the below:
        return roundEnv.getElementsAnnotatedWith(Option.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Parameters.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Mixin.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(ArgGroup.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Unmatched.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Spec.class).removeAll(elements);
    }
    return false;
}
 
Example #2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testInject_AnnotatedFieldInjectedForSubcommand() {
    class Injected {
        @Spec CommandSpec commandSpec;
        @Parameters String[] params;
    }
    Injected injected = new Injected();
    Injected sub = new Injected();

    assertNull(injected.commandSpec);
    assertNull(sub.commandSpec);

    CommandLine cmd = new CommandLine(injected);
    assertSame(cmd.getCommandSpec(), injected.commandSpec);

    CommandLine subcommand = new CommandLine(sub);
    assertSame(subcommand.getCommandSpec(), sub.commandSpec);
}
 
Example #3
Source File: AbstractCommandSpecProcessor.java    From picocli with Apache License 2.0 5 votes vote down vote up
private void buildSpecs(RoundEnvironment roundEnv, Context context) {
    logger.fine("Building specs...");
    Set<? extends Element> explicitSpecs = roundEnv.getElementsAnnotatedWith(Spec.class);
    for (Element element : explicitSpecs) {
        buildSpec(element, context);
    }
}
 
Example #4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInject_AnnotatedFieldInjected() {
    class Injected {
        @Spec CommandSpec commandSpec;
        @Parameters String[] params;
    }
    Injected injected = new Injected();
    assertNull(injected.commandSpec);

    CommandLine cmd = new CommandLine(injected);
    assertSame(cmd.getCommandSpec(), injected.commandSpec);
}
 
Example #5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInject_FieldMustBeCommandSpec() {
    class Injected {
        @Spec CommandLine commandLine;
        @Parameters String[] params;
    }
    Injected injected = new Injected();
    try {
        new CommandLine(injected);
        fail("Expect exception");
    } catch (InitializationException ex) {
        assertEquals("@picocli.CommandLine.Spec annotation is only supported on fields of type picocli.CommandLine$Model$CommandSpec", ex.getMessage());
    }
}
 
Example #6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpec_SpecElements() {
    class Positional {
        @CommandLine.Spec CommandSpec spec1;
        @CommandLine.Spec CommandSpec spec2;
        @Parameters(index = "0") String first;
        @Parameters(index = "1") String second;
    }
    CommandLine cmd = new CommandLine(new Positional());
    CommandSpec spec = cmd.getCommandSpec();
    List<IAnnotatedElement> specElements = spec.specElements();
    assertEquals(2, specElements.size());
    assertEquals("spec1", specElements.get(0).getName());
    assertEquals("spec2", specElements.get(1).getName());
}
 
Example #7
Source File: ExampleInterface.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Spec
CommandSpec spec();
 
Example #8
Source File: AbstractCommandSpecProcessor.java    From picocli with Apache License 2.0 4 votes vote down vote up
private void registerSubcommands(List<AnnotationValue> typeMirrors,
                                 List<CommandSpec> result,
                                 Context context,
                                 RoundEnvironment roundEnv) {

    for (AnnotationValue typeMirror : typeMirrors) {
        Element subcommandElement = processingEnv.getElementUtils().getTypeElement(
                typeMirror.getValue().toString().replace('$', '.'));
        logger.fine("Processing subcommand: " + subcommandElement);

        if (isValidSubcommandHasNameAttribute(subcommandElement)) {
            CommandSpec commandSpec = buildCommand(subcommandElement, context, roundEnv);
            result.add(commandSpec);

            List<? extends Element> enclosedElements = subcommandElement.getEnclosedElements();
            for (Element enclosed : enclosedElements) {
                if (enclosed.getAnnotation(Command.class) != null) {
                    buildCommand(enclosed, context, roundEnv);
                }
                if (enclosed.getAnnotation(ArgGroup.class) != null) {
                    buildArgGroup(enclosed, context);
                }
                if (enclosed.getAnnotation(Mixin.class) != null) {
                    buildMixin(enclosed, roundEnv, context);
                }
                if (enclosed.getAnnotation(Option.class) != null) {
                    buildOption(enclosed, context);
                }
                if (enclosed.getAnnotation(Parameters.class) != null) {
                    buildParameter(enclosed, context);
                }
                if (enclosed.getAnnotation(Unmatched.class) != null) {
                    buildUnmatched(enclosed, context);
                }
                if (enclosed.getAnnotation(Spec.class) != null) {
                    buildSpec(enclosed, context);
                }
                if (enclosed.getAnnotation(ParentCommand.class) != null) {
                    buildParentCommand(enclosed, context);
                }
            }
        }
    }
}
 
Example #9
Source File: ExampleInterface.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Spec
CommandSpec spec();