From 54ef45a56a126b7493d3e0c272b0b6217b35ac98 Mon Sep 17 00:00:00 2001 From: Luis Fernando Pino Duque Date: Wed, 24 Feb 2016 13:43:37 +0000 Subject: Refactoring of the OptionsParser implementation to allow overriding the value of an option with null. Currently it returns the original default value if the new value is null. -- MOS_MIGRATED_REVID=115442253 GitOrigin-RevId: dafdc2854bc74bb8bf09abe9932160dd4a5bb1fb Change-Id: I152c25605889e0b5862231324f466991708580d1 --- .../devtools/common/options/OptionsParserImpl.java | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/java/com/google/devtools/common/options/OptionsParserImpl.java b/java/com/google/devtools/common/options/OptionsParserImpl.java index 48dac23..96fb496 100644 --- a/java/com/google/devtools/common/options/OptionsParserImpl.java +++ b/java/com/google/devtools/common/options/OptionsParserImpl.java @@ -244,7 +244,7 @@ class OptionsParserImpl { = LinkedHashMultimap.create(); private final List warnings = Lists.newArrayList(); - + private boolean allowSingleDashLongOptions = false; /** @@ -261,7 +261,7 @@ class OptionsParserImpl { void setAllowSingleDashLongOptions(boolean allowSingleDashLongOptions) { this.allowSingleDashLongOptions = allowSingleDashLongOptions; } - + /** * The implementation of {@link OptionsBase#asMap}. */ @@ -468,11 +468,6 @@ class OptionsParserImpl { } } - private Object getValue(Field field) { - ParsedOptionEntry entry = parsedValues.get(field); - return entry == null ? null : entry.getValue(); - } - OptionValueDescription getOptionValueDescription(String name) { Field field = optionsData.getFieldFromName(name); if (field == null) { @@ -659,7 +654,7 @@ class OptionsParserImpl { sourceFunction.apply(originalName), expandedFrom == null); unparsedValues.add(unparsedOptionValueDescription); - if (option.allowMultiple()) { + if (option.allowMultiple()) { canonicalizeValues.put(field, unparsedOptionValueDescription); } else { canonicalizeValues.replaceValues(field, ImmutableList.of(unparsedOptionValueDescription)); @@ -754,9 +749,12 @@ class OptionsParserImpl { // Set the fields for (Field field : optionsData.getFieldsForClass(optionsClass)) { - Object value = getValue(field); - if (value == null) { + Object value; + ParsedOptionEntry entry = parsedValues.get(field); + if (entry == null) { value = optionsData.getDefaultValue(field); + } else { + value = entry.getValue(); } try { field.set(optionsInstance, value); @@ -814,4 +812,4 @@ class OptionsParserImpl { throw new AssertionError(e); } } -} +} \ No newline at end of file -- cgit v1.2.3