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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#argGroups() . 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: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testReflection() {
    class All {
        @Option(names = "-x", required = true) int x;
        @Option(names = "-y", required = true) int y;
    }
    class App {
        @ArgGroup All all;
    }
    CommandLine cmd = new CommandLine(new App(), new InnerClassFactory(this));
    CommandSpec spec = cmd.getCommandSpec();
    List<ArgGroupSpec> groups = spec.argGroups();
    assertEquals(1, groups.size());

    ArgGroupSpec group = groups.get(0);
    assertNotNull(group);

    List<ArgSpec> options = new ArrayList<ArgSpec>(group.args());
    assertEquals(2, options.size());
    assertEquals("-x", ((OptionSpec) options.get(0)).shortestName());
    assertEquals("-y", ((OptionSpec) options.get(1)).shortestName());

    assertNotNull(options.get(0).group());
    assertSame(group, options.get(0).group());

    assertNotNull(options.get(1).group());
    assertSame(group, options.get(1).group());
}
 
Example 2
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammatic() {
    CommandSpec spec = CommandSpec.create();

    ArgGroupSpec exclusiveSub = ArgGroupSpec.builder()
            .addArg(OptionSpec.builder("-x").required(true).build())
            .addArg(OptionSpec.builder("-y").required(true).build()).build();
    ArgGroupSpec cooccur = ArgGroupSpec.builder()
            .addArg(OptionSpec.builder("-z").required(true).build())
            .addSubgroup(exclusiveSub).build();
    spec.addArgGroup(cooccur);
    assertNull(cooccur.parentGroup());
    assertEquals(1, cooccur.subgroups().size());
    assertSame(exclusiveSub, cooccur.subgroups().get(0));
    assertEquals(1, cooccur.args().size());

    assertNotNull(exclusiveSub.parentGroup());
    assertSame(cooccur, exclusiveSub.parentGroup());
    assertEquals(0, exclusiveSub.subgroups().size());
    assertEquals(2, exclusiveSub.args().size());

    ArgGroupSpec exclusive = ArgGroupSpec.builder()
            .addArg(OptionSpec.builder("-a").required(true).build())
            .addArg(OptionSpec.builder("-b").required(true).build()).build();
    spec.addArgGroup(exclusive);
    assertNull(exclusive.parentGroup());
    assertTrue(exclusive.subgroups().isEmpty());
    assertEquals(2, exclusive.args().size());

    List<ArgGroupSpec> groups = spec.argGroups();
    assertEquals(2, groups.size());
    assertSame(cooccur, groups.get(0));
    assertSame(exclusive, groups.get(1));
}
 
Example 3
Source File: ReflectionConfigGenerator.java    From picocli with Apache License 2.0 4 votes vote down vote up
void visitCommandSpec(CommandSpec spec) throws Exception {
    Object userObject = spec.userObject();
    if (userObject != null) {
        if (userObject instanceof Method) {
            Method method = (Method) spec.userObject();
            ReflectedClass cls = getOrCreateClass(method.getDeclaringClass());
            cls.addMethod(method);
        } else if (userObject instanceof Element) {
            visitElement((Element) userObject);
        } else if (Proxy.isProxyClass(spec.userObject().getClass())) {
            // do nothing: requires DynamicProxyConfigGenerator
        } else {
            visitAnnotatedFields(spec.userObject().getClass());
        }
    }
    visitObjectType(spec.versionProvider());
    visitObjectType(spec.defaultValueProvider());

    for (UnmatchedArgsBinding binding : spec.unmatchedArgsBindings()) {
        visitGetter(binding.getter());
        visitSetter(binding.setter());
    }
    for (IAnnotatedElement specElement : spec.specElements()) {
        visitGetter(specElement.getter());
        visitSetter(specElement.setter());
    }
    for (IAnnotatedElement parentCommandElement : spec.parentCommandElements()) {
        visitGetter(parentCommandElement.getter());
        visitSetter(parentCommandElement.setter());
    }
    for (OptionSpec option : spec.options()) {
        visitArgSpec(option);
    }
    for (PositionalParamSpec positional : spec.positionalParameters()) {
        visitArgSpec(positional);
    }
    for (ArgGroupSpec group : spec.argGroups()) {
        visitGroupSpec(group);
    }
    for (Map.Entry<String, CommandSpec> entry : spec.mixins().entrySet()) {
        CommandSpec mixin = entry.getValue();
        visitCommandSpec(mixin);

        String name = entry.getKey();
        IAnnotatedElement annotatedElement = spec.mixinAnnotatedElements().get(name);
        if (annotatedElement != null) {
            visitGetter(annotatedElement.getter());
        }
    }
    for (CommandLine sub : spec.subcommands().values()) {
        visitCommandSpec(sub.getCommandSpec());
    }
}