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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#positionalParameters() . 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: CommandMethodTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotateMethod_unannotatedPositional_indexByParameterOrder() throws Exception {
    Method m = CommandLine.getCommandMethods(UnannotatedPositional.class, "x").get(0);
    CommandLine cmd = new CommandLine(m);
    CommandSpec spec = cmd.getCommandSpec();
    List<Model.PositionalParamSpec> positionals = spec.positionalParameters();
    String[] labels = { "<arg0>", "<arg1>", "<arg2>", "<arg3>", "<arg4>"};
    assertEquals(positionals.size(), labels.length);

    String[] ranges = { "0", "1", "2", "3..*", "4..*" };

    for (int i = 0; i < positionals.size(); i++) {
        Model.PositionalParamSpec positional = positionals.get(i);
        assertEquals(positional.paramLabel() + " at index " + i, CommandLine.Range.valueOf(ranges[i]), positional.index());
        assertEquals(labels[i], positional.paramLabel());
    }
}
 
Example 2
Source File: CommandMethodTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotateMethod_unannotatedPositionalMixedWithOptions_indexByParameterOrder() throws Exception {
    Method m = CommandLine.getCommandMethods(PositionalsMixedWithOptions.class, "mixed").get(0);
    CommandLine cmd = new CommandLine(m);
    CommandSpec spec = cmd.getCommandSpec();
    List<Model.PositionalParamSpec> positionals = spec.positionalParameters();
    String[] labels = { "<arg0>", "<arg3>", "<arg4>"};
    assertEquals(positionals.size(), labels.length);

    String[] ranges = { "0", "1..*", "2..*" };

    for (int i = 0; i < positionals.size(); i++) {
        Model.PositionalParamSpec positional = positionals.get(i);
        assertEquals(positional.paramLabel() + " at index " + i, CommandLine.Range.valueOf(ranges[i]), positional.index());
        assertEquals(labels[i], positional.paramLabel());
    }

    assertEquals(2, spec.options().size());
    assertEquals(int.class, spec.findOption("-b").type());
    assertEquals(String.class, spec.findOption("-c").type());
}
 
Example 3
Source File: AnnotatedCommandSourceGenerator.java    From picocli with Apache License 2.0 5 votes vote down vote up
private static boolean isMixedIn(PositionalParamSpec positional, CommandSpec spec) {
    for (CommandSpec mixin : spec.mixins().values()) {
        for (PositionalParamSpec mixedIn : mixin.positionalParameters()) {
            if (mixedIn.equals(positional)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: ManPageGenerator.java    From picocli with Apache License 2.0 5 votes vote down vote up
static void genPositionalArgs(PrintWriter pw, CommandSpec spec) {
    List<PositionalParamSpec> positionals = new ArrayList<PositionalParamSpec>(spec.positionalParameters());
    // remove hidden params
    for (Iterator<PositionalParamSpec> iter = positionals.iterator(); iter.hasNext();) {
        if (iter.next().hidden()) { iter.remove(); }
    }
    // positional parameters that are part of a group
    // are shown in the custom option section for that group
    List<ArgGroupSpec> groups = optionListGroups(spec);
    for (ArgGroupSpec group : groups) { positionals.removeAll(group.positionalParameters()); }

    if (positionals.isEmpty() && !spec.usageMessage().showAtFileInUsageHelp()) {
        return;
    }
    pw.printf("// tag::picocli-generated-man-section-arguments[]%n");
    pw.printf("== Arguments%n");

    IParameterRenderer parameterRenderer = spec.commandLine().getHelp().createDefaultParameterRenderer();
    IParamLabelRenderer paramLabelRenderer = spec.commandLine().getHelp().createDefaultParamLabelRenderer();

    if (spec.usageMessage().showAtFileInUsageHelp()) {
        CommandLine cmd = new CommandLine(spec).setColorScheme(COLOR_SCHEME);
        CommandLine.Help help = cmd.getHelp();
        writePositional(pw, help.AT_FILE_POSITIONAL_PARAM, parameterRenderer, paramLabelRenderer);
    }

    for (PositionalParamSpec positional : positionals) {
        writePositional(pw, positional, parameterRenderer, paramLabelRenderer);
    }
    pw.println();
    pw.printf("// end::picocli-generated-man-section-arguments[]%n");
    pw.println();
}
 
Example 5
Source File: AutoComplete.java    From picocli with Apache License 2.0 5 votes vote down vote up
private static CommandSpec findCommandFor(PositionalParamSpec positional, CommandSpec commandSpec) {
    for (PositionalParamSpec defined : commandSpec.positionalParameters()) {
        if (defined == positional) { return commandSpec; }
    }
    for (CommandLine sub : commandSpec.subcommands().values()) {
        CommandSpec result = findCommandFor(positional, sub.getCommandSpec());
        if (result != null) { return result; }
    }
    return null;
}
 
Example 6
Source File: AutoComplete.java    From picocli with Apache License 2.0 5 votes vote down vote up
private static void addCandidatesForArgsFollowing(CommandSpec commandSpec, List<CharSequence> candidates) {
    if (commandSpec == null) { return; }
    for (Map.Entry<String, CommandLine> entry : commandSpec.subcommands().entrySet()) {
        if (entry.getValue().getCommandSpec().usageMessage().hidden()) { continue; } // #887 skip hidden subcommands
        candidates.add(entry.getKey());
        candidates.addAll(Arrays.asList(entry.getValue().getCommandSpec().aliases()));
    }
    candidates.addAll(commandSpec.optionsMap().keySet());
    for (PositionalParamSpec positional : commandSpec.positionalParameters()) {
        if (positional.hidden()) { continue; } // #887 skip hidden subcommands
        addCandidatesForArgsFollowing(positional, candidates);
    }
}
 
Example 7
Source File: AutoComplete.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
private static CommandSpec findCommandFor(PositionalParamSpec positional, CommandSpec commandSpec) {
    for (PositionalParamSpec defined : commandSpec.positionalParameters()) {
        if (defined == positional) { return commandSpec; }
    }
    for (CommandLine sub : commandSpec.subcommands().values()) {
        CommandSpec result = findCommandFor(positional, sub.getCommandSpec());
        if (result != null) { return result; }
    }
    return null;
}
 
Example 8
Source File: AutoComplete.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
private static void addCandidatesForArgsFollowing(CommandSpec commandSpec, List<CharSequence> candidates) {
    if (commandSpec == null) { return; }
    for (Map.Entry<String, CommandLine> entry : commandSpec.subcommands().entrySet()) {
        if (entry.getValue().getCommandSpec().usageMessage().hidden()) { continue; } // #887 skip hidden subcommands
        candidates.add(entry.getKey());
        candidates.addAll(Arrays.asList(entry.getValue().getCommandSpec().aliases()));
    }
    candidates.addAll(commandSpec.optionsMap().keySet());
    for (PositionalParamSpec positional : commandSpec.positionalParameters()) {
        if (positional.hidden()) { continue; } // #887 skip hidden subcommands
        addCandidatesForArgsFollowing(positional, candidates);
    }
}
 
Example 9
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());
    }
}