/** Adds the specified option spec to the list of configured arguments to expect. * The option's {@linkplain OptionSpec#description()} may now return Strings from this * CommandSpec's {@linkplain UsageMessageSpec#messages() messages}. * The option parameter's {@linkplain OptionSpec#defaultValueString()} may * now return Strings from this CommandSpec's {@link CommandSpec#defaultValueProvider()} IDefaultValueProvider}. * @param option the option spec to add * @return this CommandSpec for method chaining * @throws DuplicateOptionAnnotationsException if any of the names of the specified option is the same as the name of another option */ public CommandSpec addOption(OptionSpec option) { args.add(option); options.add(option); for (String name : option.names()) { // cannot be null or empty OptionSpec existing = optionsByNameMap.put(name, option); if (existing != null && !existing.equals(option)) { throw DuplicateOptionAnnotationsException.create(name, option, existing); } if (name.length() == 2 && name.startsWith("-")) { posixOptionsByKeyMap.put(name.charAt(1), option); } } if (option.required()) { requiredArgs.add(option); } option.messages(usageMessage().messages()); option.commandSpec = this; return this; } /** Adds the specified positional parameter spec to the list of configured arguments to expect.
if (shortestName.length() == 2 && shortestName.startsWith("-")) { booleanOptions.add(option); if (option.required()) { clusteredRequired.append(shortestName.substring(1)); } else { Text name = colorScheme.optionText(option.shortestName()); Text param = parameterLabelRenderer().renderParameterLabel(option, colorScheme.ansi(), colorScheme.optionParamStyles); if (option.required()) { // e.g., -x=VAL optionText = optionText.concat(" ").concat(name).concat(param).concat(""); if (option.isMultiValue()) { // e.g., -x=VAL [-x=VAL]...
public Text[][] render(OptionSpec option, IParamLabelRenderer paramLabelRenderer, ColorScheme scheme) { String[] names = ShortestFirst.sort(option.names()); int shortOptionCount = names[0].length() == 2 ? 1 : 0; String shortOption = shortOptionCount > 0 ? names[0] : ""; sep = shortOptionCount > 0 && names.length > 1 ? "," : ""; String longOption = join(names, shortOptionCount, names.length - shortOptionCount, ", "); Text longOptionText = createLongOptionText(option, paramLabelRenderer, scheme, longOption); String requiredOption = option.required() ? requiredMarker : ""; return renderDescriptionLines(option, scheme, requiredOption, shortOption, longOptionText); }
if (shortestName.length() == 2 && shortestName.startsWith("-")) { booleanOptions.add(option); if (option.required()) { clusteredRequired.append(shortestName.substring(1)); } else { Text name = colorScheme.optionText(option.shortestName()); Text param = parameterLabelRenderer().renderParameterLabel(option, colorScheme.ansi(), colorScheme.optionParamStyles); if (option.required()) { // e.g., -x=VAL optionText = optionText.concat(" ").concat(name).concat(param).concat(""); if (option.isMultiValue()) { // e.g., -x=VAL [-x=VAL]...
/** Adds the specified option spec to the list of configured arguments to expect. * The option's {@linkplain OptionSpec#description()} may now return Strings from this * CommandSpec's {@linkplain UsageMessageSpec#messages() messages}. * The option parameter's {@linkplain OptionSpec#defaultValueString()} may * now return Strings from this CommandSpec's {@link CommandSpec#defaultValueProvider()} IDefaultValueProvider}. * @param option the option spec to add * @return this CommandSpec for method chaining * @throws DuplicateOptionAnnotationsException if any of the names of the specified option is the same as the name of another option */ public CommandSpec addOption(OptionSpec option) { args.add(option); options.add(option); for (String name : option.names()) { // cannot be null or empty OptionSpec existing = optionsByNameMap.put(name, option); if (existing != null && !existing.equals(option)) { throw DuplicateOptionAnnotationsException.create(name, option, existing); } if (name.length() == 2 && name.startsWith("-")) { posixOptionsByKeyMap.put(name.charAt(1), option); } } if (option.required()) { requiredArgs.add(option); } option.messages(usageMessage().messages()); option.commandSpec = this; return this; } /** Adds the specified positional parameter spec to the list of configured arguments to expect.
@Test public void testOptionDefaultRequiredIsFalse() { assertFalse(OptionSpec.builder("-x").build().required()); }
public Text[][] render(OptionSpec option, IParamLabelRenderer paramLabelRenderer, ColorScheme scheme) { String[] names = ShortestFirst.sort(option.names()); int shortOptionCount = names[0].length() == 2 ? 1 : 0; String shortOption = shortOptionCount > 0 ? names[0] : ""; sep = shortOptionCount > 0 && names.length > 1 ? "," : ""; String longOption = join(names, shortOptionCount, names.length - shortOptionCount, ", "); Text longOptionText = createLongOptionText(option, paramLabelRenderer, scheme, longOption); String requiredOption = option.required() ? requiredMarker : ""; return renderDescriptionLines(option, scheme, requiredOption, shortOption, longOptionText); }
@SuppressWarnings("deprecation") private void printOptionAnnotation(PrintWriter pw, OptionSpec spec, String indent) { pw.printf("%s@%s", indent, importer.getImportedName(Option.class.getCanonicalName())); indent = String.format(",%n%s ", indent); String sep = "("; sep = appendStringArray(pw, sep, indent, "names = %s", spec.names(), EMPTY_ARRAY); sep = append(pw, sep, indent, "required = %s", spec.required(), false); sep = append(pw, sep, indent, "help = %s", spec.help(), false); sep = append(pw, sep, indent, "usageHelp = %s", spec.usageHelp(), false); sep = append(pw, sep, indent, "versionHelp = %s", spec.versionHelp(), false); 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 testOptionDefaultRequiredIsFalse() { assertFalse(OptionSpec.builder("-x").build().required()); }