diff options
author | ccalvarin <ccalvarin@google.com> | 2017-03-30 17:34:04 +0000 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2017-03-31 17:09:28 +0200 |
commit | 706bafe7aa17ed6ef1187986af3ba749559fb232 (patch) | |
tree | 50df197ccb2d36492af7f8cf490316083f7611f9 /src/main/java/com/google/devtools/common/options/OptionsParser.java | |
parent | 29bd56e3cd3eadd63abcc833c3075ecedfd2e9dc (diff) |
Expand Invocation FlagPolicies on expansion flags.
For SetValue and UseDefault policies on expansion flags or flags with implicitRequirements, expand the policy into policy for each of its sub-flags. For SetValue, this addresses the issue with policies on expansion flags with overridable=true not actually letting user flags overrride the expansion. For UseDefault, this formalizes the behavior where UseDefault will wipe all user-provided flags that expand from a banned expansion flag, and will allow later work to guarantee that a later policy can override the expansion policy's subflags.
Since expansion flags do not have value, break if the invocation policy uses AllowValue or DisallowValue on an expansion flag.
PiperOrigin-RevId: 151718539
Diffstat (limited to 'src/main/java/com/google/devtools/common/options/OptionsParser.java')
-rw-r--r-- | src/main/java/com/google/devtools/common/options/OptionsParser.java | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/src/main/java/com/google/devtools/common/options/OptionsParser.java b/src/main/java/com/google/devtools/common/options/OptionsParser.java index 1c4b2780a0..a16f0e7efa 100644 --- a/src/main/java/com/google/devtools/common/options/OptionsParser.java +++ b/src/main/java/com/google/devtools/common/options/OptionsParser.java @@ -32,6 +32,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; /** * A parser for options. Typical use case in a main method: @@ -199,16 +200,28 @@ public class OptionsParser implements OptionsProvider { public static final class OptionDescription { private final String name; + + // For valued flags private final Object defaultValue; private final Converter<?> converter; private final boolean allowMultiple; - public OptionDescription(String name, Object defaultValue, Converter<?> converter, - boolean allowMultiple) { + private final ImmutableList<OptionValueDescription> expansions; + private final ImmutableList<OptionValueDescription> implicitRequirements; + + OptionDescription( + String name, + Object defaultValue, + Converter<?> converter, + boolean allowMultiple, + ImmutableList<OptionValueDescription> expansions, + ImmutableList<OptionValueDescription> implicitRequirements) { this.name = name; this.defaultValue = defaultValue; this.converter = converter; this.allowMultiple = allowMultiple; + this.expansions = expansions; + this.implicitRequirements = implicitRequirements; } public String getName() { @@ -226,6 +239,14 @@ public class OptionsParser implements OptionsProvider { public boolean getAllowMultiple() { return allowMultiple; } + + public ImmutableList<OptionValueDescription> getImplicitRequirements() { + return implicitRequirements; + } + + public ImmutableList<OptionValueDescription> getExpansions() { + return expansions; + } } /** @@ -235,22 +256,25 @@ public class OptionsParser implements OptionsProvider { */ public static class OptionValueDescription { private final String name; - private final Object value; - private final OptionPriority priority; - private final String source; - private final String implicitDependant; - private final String expandedFrom; + @Nullable private final String originalValueString; + @Nullable private final Object value; + @Nullable private final OptionPriority priority; + @Nullable private final String source; + @Nullable private final String implicitDependant; + @Nullable private final String expandedFrom; private final boolean allowMultiple; public OptionValueDescription( String name, - Object value, - OptionPriority priority, - String source, - String implicitDependant, - String expandedFrom, + @Nullable String originalValueString, + @Nullable Object value, + @Nullable OptionPriority priority, + @Nullable String source, + @Nullable String implicitDependant, + @Nullable String expandedFrom, boolean allowMultiple) { this.name = name; + this.originalValueString = originalValueString; this.value = value; this.priority = priority; this.source = source; @@ -263,6 +287,10 @@ public class OptionsParser implements OptionsProvider { return name; } + public String getOriginalValueString() { + return originalValueString; + } + // Need to suppress unchecked warnings, because the "multiple occurrence" // options use unchecked ListMultimaps due to limitations of Java generics. @SuppressWarnings({"unchecked", "rawtypes"}) @@ -285,6 +313,7 @@ public class OptionsParser implements OptionsProvider { } return value; } + /** * @return the priority of the thing that set this value for this flag */ @@ -585,10 +614,10 @@ public class OptionsParser implements OptionsProvider { /** * Returns a description of the option. * - * @return The {@link OptionValueDescription} for the option, or null if there is no option by - * the given name. + * @return The {@link OptionDescription} for the option, or null if there is no option by the + * given name. */ - public OptionDescription getOptionDescription(String name) { + public OptionDescription getOptionDescription(String name) throws OptionsParsingException { return impl.getOptionDescription(name); } @@ -623,7 +652,7 @@ public class OptionsParser implements OptionsProvider { * {@code parse(OptionPriority.COMMAND_LINE, null, Arrays.asList(args))}. */ public void parse(String... args) throws OptionsParsingException { - parse(OptionPriority.COMMAND_LINE, (String) null, Arrays.asList(args)); + parse(OptionPriority.COMMAND_LINE, null, Arrays.asList(args)); } /** @@ -631,7 +660,7 @@ public class OptionsParser implements OptionsProvider { * {@code parse(OptionPriority.COMMAND_LINE, null, args)}. */ public void parse(List<String> args) throws OptionsParsingException { - parse(OptionPriority.COMMAND_LINE, (String) null, args); + parse(OptionPriority.COMMAND_LINE, null, args); } /** |