Skip to content

Commit

Permalink
Include whether flags need values in info
Browse files Browse the repository at this point in the history
When parsing flags outside of Bazel, it can be useful to know whether the next token may be a value for a previous flag or whether the previous flag was complete because it didn't need a value.

Closes #19536.

PiperOrigin-RevId: 579324484
Change-Id: I47162f90dc7d5bbd770a95766e99fb06eaea52f1
  • Loading branch information
illicitonion authored and Copybara-Service committed Nov 3, 2023
1 parent 176239e commit 718f400
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ private static BazelFlagsProto.FlagInfo.Builder createFlagInfo(OptionDefinition
flagBuilder.setHasNegativeFlag(option.hasNegativeOption());
flagBuilder.setDocumentation(option.getHelpText());
flagBuilder.setAllowsMultiple(option.allowsMultiple());
flagBuilder.setRequiresValue(option.requiresValue());

List<String> optionEffectTags =
Arrays.stream(option.getOptionEffectTags())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ public boolean usesBooleanValueSyntax() {
|| getConverter() instanceof BoolOrEnumConverter;
}

/**
* Returns whether an option requires a value when instantiated, or instead can be present without
* an explicit value.
*/
public boolean requiresValue() {
return !isVoidField() && !usesBooleanValueSyntax();
}

/** Returns the evaluated default value for this option & memoizes the result. */
@Nullable
public Object getDefaultValue(@Nullable Object conversionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static void getUsageHtml(
.append(flagName)
.append("</a>");

if (optionDefinition.usesBooleanValueSyntax() || optionDefinition.isVoidField()) {
if (!optionDefinition.requiresValue()) {
// Nothing for boolean, tristate, boolean_or_enum, or void options.
} else if (!valueDescription.isEmpty()) {
usage.append("=").append(escaper.escape(valueDescription));
Expand Down
5 changes: 5 additions & 0 deletions src/main/protobuf/bazel_flags.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ message FlagInfo {
repeated string metadata_tags = 8;
// The documentation category assigned to this flag
optional string documentation_category = 9;
// Whether the flag requires a value.
// If false, value-less invocations are acceptable, e.g. --subcommands,
// but if true a value must be present for all instantiations of the flag,
// e.g. --jobs=100.
optional bool requires_value = 10;
}

message FlagCollection {
Expand Down

0 comments on commit 718f400

Please sign in to comment.