private void printPositional(PositionalParamSpec positional, PrintWriter pw, String indent) { pw.printf("%sindex: %s%n", indent + "- ", positional.index()); indent += " "; printArg(positional, pw, indent); } private void printArg(ArgSpec arg, PrintWriter pw, String indent) {
private static void validatePositionalParameters(List<PositionalParamSpec> positionalParametersFields) { int min = 0; for (PositionalParamSpec positional : positionalParametersFields) { Range index = positional.index(); if (index.min > min) { throw new ParameterIndexGapException("Command definition should have a positional parameter with index=" + min + ". Nearest positional parameter '" + positional.paramLabel() + "' has index=" + index.min); } min = Math.max(min, index.max); min = min == Integer.MAX_VALUE ? min : min + 1; } } @SuppressWarnings("unchecked") private static Stack<String> copy(Stack<String> stack) { return (Stack<String>) stack.clone(); }
int originalNowProcessingSize = parseResult.nowProcessing.size(); for (PositionalParamSpec positionalParam : commandSpec.positionalParameters()) { Range indexRange = positionalParam.index(); if (!indexRange.contains(position) || positionalParam.typedValueAtPosition.get(position) != null) { continue;
@Test public void testParamIndex() { CommandLine git = new CommandLine(new Git()); CommandLine clone = git.getSubcommands().get("clone"); Model.PositionalParamSpec repo = clone.getCommandSpec().positionalParameters().get(0); assertEquals(CommandLine.Range.valueOf("0"), repo.index()); }
/** Ensures all attributes of this {@code PositionalParamSpec} have a valid value; throws an {@link InitializationException} if this cannot be achieved. */ private PositionalParamSpec(Builder builder) { super(builder); index = builder.index == null ? Range.valueOf("*") : builder.index; capacity = builder.capacity == null ? Range.parameterCapacity(arity(), index) : builder.capacity; if (toString == null) { toString = "positional parameter[" + index() + "]"; } } /** Returns a new Builder initialized with the attributes from this {@code PositionalParamSpec}. Calling {@code build} immediately will return a copy of this {@code PositionalParamSpec}.
private static String describe(ArgSpec argSpec, String separator) { String prefix = (argSpec.isOption()) ? ((OptionSpec) argSpec).longestName() + separator : "params[" + ((PositionalParamSpec) argSpec).index() + "]" + separator; return prefix + argSpec.paramLabel(); } }
@Test public void testParamIndexAnnotatedAndUnAnnotated() { CommandLine git = new CommandLine(new AnnotatedParams()); CommandLine method = git.getSubcommands().get("method"); List<Model.PositionalParamSpec> positionals = method.getCommandSpec().positionalParameters(); for (int i = 0; i < positionals.size(); i++) { assertEquals(CommandLine.Range.valueOf("" + i), positionals.get(i).index()); } }
return false; Range indexRange = ((PositionalParamSpec) argSpec).index(); String sep = ""; String names = ": ";
private void printParametersAnnotation(PrintWriter pw, PositionalParamSpec spec, String indent) { pw.printf("%s@%s", indent, importer.getImportedName(Parameters.class.getCanonicalName())); indent = String.format(",%n%s ", indent); String sep = "("; sep = append(pw, sep, indent, "index = \"%s\"", spec.index().toString(), spec.index().isUnspecified()); sep = appendStringArray(pw, sep, indent, "description = %s", spec.description(), EMPTY_ARRAY); sep = append(pw, sep, indent, "arity = \"%s\"", spec.arity().toString(), spec.arity().isUnspecified() ? spec.arity().toString() : ""); sep = append(pw, sep, indent, "paramLabel = \"%s\"", spec.paramLabel(), "<" + argElementName(spec) + ">"); sep = append(pw, sep, indent, "hideParamSyntax = %s", spec.hideParamSyntax(), false); sep = appendTypeInfo(pw, sep, indent, spec.typeInfo()); sep = appendTypeConverter(pw, sep, indent, spec.converters()); sep = append(pw, sep, indent, "split = \"%s\"", spec.splitRegex(), ""); sep = append(pw, sep, indent, "hidden = %s", spec.hidden(), false); sep = append(pw, sep, indent, "defaultValue = \"%s\"", spec.defaultValue() == null ? "__no_default_value__" : spec.defaultValue(), "__no_default_value__"); sep = append(pw, sep, indent, "showDefaultValue = %s", spec.showDefaultValue(), CommandLine.Help.Visibility.ON_DEMAND); sep = appendCompletionCandidates(pw, sep, indent, spec); sep = append(pw, sep, indent, "interactive = %s", spec.interactive(), false); sep = append(pw, sep, indent, "descriptionKey = \"%s\"", spec.descriptionKey(), ""); if (!"(".equals(sep)) { pw.print(")"); } }
@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()); }
private Range index(ArgSpec arg) { return arg.isOption() ? OPTION_INDEX : ((PositionalParamSpec) arg).index(); } }
private String optionDescription(String prefix, ArgSpec argSpec, int index) { String desc = ""; if (argSpec.isOption()) { desc = prefix + "option '" + ((OptionSpec) argSpec).longestName() + "'"; if (index >= 0) { if (argSpec.arity().max > 1) { desc += " at index " + index; } desc += " (" + argSpec.paramLabel() + ")"; } } else { desc = prefix + "positional parameter at index " + ((PositionalParamSpec) argSpec).index() + " (" + argSpec.paramLabel() + ")"; } return desc; }
@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()); } }
/** Returns the description template of this positional parameter, before variables are {@linkplain Parameters#description() rendered}. * If a resource bundle has been {@linkplain ArgSpec#messages(Messages) set}, this method will first try to find a value in the resource bundle: * If the resource bundle has no entry for the {@code fully qualified commandName + "." + descriptionKey} or for the unqualified {@code descriptionKey}, * an attempt is made to find the positional parameter description using {@code paramLabel() + "[" + index() + "]"} as key, * first with the {@code fully qualified commandName + "."} prefix, then without. * @see Parameters#description() * @see CommandSpec#qualifiedName(String) * @since 3.6 */ @Override public String[] description() { if (messages() == null) { return super.description(); } String[] newValue = messages().getStringArray(descriptionKey(), null); if (newValue != null) { return newValue; } newValue = messages().getStringArray(paramLabel() + "[" + index() + "]", null); if (newValue != null) { return newValue; } return super.description(); }
@Test public void testPositionalDefaultIndexIsAll() { assertEquals(Range.valueOf("*"), PositionalParamSpec.builder().build().index()); }
@Test public void testParamIndexAnnotatedAndUnAnnotated() { CommandLine git = new CommandLine(new AnnotatedParams()); CommandLine method = git.getSubcommands().get("method"); List<Model.PositionalParamSpec> positionals = method.getCommandSpec().positionalParameters(); for (int i = 0; i < positionals.size(); i++) { assertEquals(CommandLine.Range.valueOf("" + i), positionals.get(i).index()); } }
private static String describe(ArgSpec argSpec, String separator) { String prefix = (argSpec.isOption()) ? ((OptionSpec) argSpec).longestName() + separator : "params[" + ((PositionalParamSpec) argSpec).index() + "]" + separator; return prefix + argSpec.paramLabel(); } }
/** Ensures all attributes of this {@code PositionalParamSpec} have a valid value; throws an {@link InitializationException} if this cannot be achieved. */ private PositionalParamSpec(Builder builder) { super(builder); index = builder.index == null ? Range.valueOf("*") : builder.index; capacity = builder.capacity == null ? Range.parameterCapacity(arity(), index) : builder.capacity; if (toString == null) { toString = "positional parameter[" + index() + "]"; } } public static Builder builder() { return new Builder(); }
@Test public void testParamIndex() { CommandLine git = new CommandLine(new Git()); CommandLine clone = git.getSubcommands().get("clone"); Model.PositionalParamSpec repo = clone.getCommandSpec().positionalParameters().get(0); assertEquals(CommandLine.Range.valueOf("0"), repo.index()); }