// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.common.options;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.devtools.common.options.OptionsParser.OptionDescription;
import com.google.devtools.common.options.OptionsParser.OptionUsageRestrictions;
import com.google.devtools.common.options.OptionsParser.OptionValueDescription;
import com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* The implementation of the options parser. This is intentionally package
* private for full flexibility. Use {@link OptionsParser} or {@link Options}
* if you're a consumer.
*/
class OptionsParserImpl {
private final OptionsData optionsData;
/**
* We store the results of parsing the arguments in here. It'll look like
*
*
*
* This map is modified by repeated calls to {@link #parse(OptionPriority,Function,List)}.
*/
private final Map parsedValues = new HashMap<>();
/**
* We store the pre-parsed, explicit options for each priority in here.
* We use partially preparsed options, which can be different from the original
* representation, e.g. "--nofoo" becomes "--foo=0".
*/
private final List unparsedValues = new ArrayList<>();
/**
* Unparsed values for use with the canonicalize command are stored separately from
* unparsedValues so that invocation policy can modify the values for canonicalization (e.g.
* override user-specified values with default values) without corrupting the data used to
* represent the user's original invocation for {@link #asListOfExplicitOptions()} and
* {@link #asListOfUnparsedOptions()}. A LinkedHashMultimap is used so that canonicalization
* happens in the correct order and multiple values can be stored for flags that allow multiple
* values.
*/
private final Multimap canonicalizeValues
= LinkedHashMultimap.create();
private final List warnings = new ArrayList<>();
private boolean allowSingleDashLongOptions = false;
private ArgsPreProcessor argsPreProcessor =
new ArgsPreProcessor() {
@Override
public List preProcess(List args) throws OptionsParsingException {
return args;
}
};
/**
* Create a new parser object
*/
OptionsParserImpl(OptionsData optionsData) {
this.optionsData = optionsData;
}
OptionsData getOptionsData() {
return optionsData;
}
/**
* Indicates whether or not the parser will allow long options with a
* single-dash, instead of the usual double-dash, too, eg. -example instead of just --example.
*/
void setAllowSingleDashLongOptions(boolean allowSingleDashLongOptions) {
this.allowSingleDashLongOptions = allowSingleDashLongOptions;
}
/** Sets the ArgsPreProcessor for manipulations of the options before parsing. */
void setArgsPreProcessor(ArgsPreProcessor preProcessor) {
this.argsPreProcessor = Preconditions.checkNotNull(preProcessor);
}
/**
* Implements {@link OptionsParser#asListOfUnparsedOptions()}.
*/
List asListOfUnparsedOptions() {
List result = Lists.newArrayList(unparsedValues);
// It is vital that this sort is stable so that options on the same priority are not reordered.
Collections.sort(result, new Comparator() {
@Override
public int compare(UnparsedOptionValueDescription o1,
UnparsedOptionValueDescription o2) {
return o1.getPriority().compareTo(o2.getPriority());
}
});
return result;
}
/**
* Implements {@link OptionsParser#asListOfExplicitOptions()}.
*/
List asListOfExplicitOptions() {
List result = Lists.newArrayList(Iterables.filter(
unparsedValues,
new Predicate() {
@Override
public boolean apply(UnparsedOptionValueDescription input) {
return input.isExplicit();
}
}));
// It is vital that this sort is stable so that options on the same priority are not reordered.
Collections.sort(result, new Comparator() {
@Override
public int compare(UnparsedOptionValueDescription o1,
UnparsedOptionValueDescription o2) {
return o1.getPriority().compareTo(o2.getPriority());
}
});
return result;
}
/**
* Implements {@link OptionsParser#canonicalize}.
*/
List asCanonicalizedList() {
List processed = Lists.newArrayList(
canonicalizeValues.values());
// Sort implicit requirement options to the end, keeping their existing order, and sort the
// other options alphabetically.
Collections.sort(processed, new Comparator() {
@Override
public int compare(UnparsedOptionValueDescription o1, UnparsedOptionValueDescription o2) {
if (o1.isImplicitRequirement()) {
return o2.isImplicitRequirement() ? 0 : 1;
}
if (o2.isImplicitRequirement()) {
return -1;
}
return o1.getName().compareTo(o2.getName());
}
});
List result = new ArrayList<>();
for (UnparsedOptionValueDescription value : processed) {
// Ignore expansion options.
if (value.isExpansion()) {
continue;
}
result.add("--" + value.getName() + "=" + value.getUnparsedValue());
}
return result;
}
/**
* Implements {@link OptionsParser#asListOfEffectiveOptions()}.
*/
List asListOfEffectiveOptions() {
List result = new ArrayList<>();
for (Map.Entry mapEntry : optionsData.getAllNamedFields()) {
String fieldName = mapEntry.getKey();
Field field = mapEntry.getValue();
OptionValueDescription entry = parsedValues.get(field);
if (entry == null) {
Object value = optionsData.getDefaultValue(field);
result.add(
new OptionValueDescription(
fieldName,
/* originalValueString */null,
value,
OptionPriority.DEFAULT,
/* source */ null,
/* implicitDependant */ null,
/* expandedFrom */ null,
false));
} else {
result.add(entry);
}
}
return result;
}
private void maybeAddDeprecationWarning(Field field) {
Option option = field.getAnnotation(Option.class);
// Continue to support the old behavior for @Deprecated options.
String warning = option.deprecationWarning();
if (!warning.isEmpty() || (field.getAnnotation(Deprecated.class) != null)) {
addDeprecationWarning(option.name(), warning);
}
}
private void addDeprecationWarning(String optionName, String warning) {
warnings.add("Option '" + optionName + "' is deprecated"
+ (warning.isEmpty() ? "" : ": " + warning));
}
// Warnings should not end with a '.' because the internal reporter adds one automatically.
private void setValue(Field field, String name, Object value,
OptionPriority priority, String source, String implicitDependant, String expandedFrom) {
OptionValueDescription entry = parsedValues.get(field);
if (entry != null) {
// Override existing option if the new value has higher or equal priority.
if (priority.compareTo(entry.getPriority()) >= 0) {
// Output warnings:
if ((implicitDependant != null) && (entry.getImplicitDependant() != null)) {
if (!implicitDependant.equals(entry.getImplicitDependant())) {
warnings.add(
"Option '"
+ name
+ "' is implicitly defined by both option '"
+ entry.getImplicitDependant()
+ "' and option '"
+ implicitDependant
+ "'");
}
} else if ((implicitDependant != null) && priority.equals(entry.getPriority())) {
warnings.add(
"Option '"
+ name
+ "' is implicitly defined by option '"
+ implicitDependant
+ "'; the implicitly set value overrides the previous one");
} else if (entry.getImplicitDependant() != null) {
warnings.add(
"A new value for option '"
+ name
+ "' overrides a previous implicit setting of that option by option '"
+ entry.getImplicitDependant()
+ "'");
} else if ((priority == entry.getPriority())
&& ((entry.getExpansionParent() == null) && (expandedFrom != null))) {
// Create a warning if an expansion option overrides an explicit option:
warnings.add("The option '" + expandedFrom + "' was expanded and now overrides a "
+ "previous explicitly specified option '" + name + "'");
} else if ((entry.getExpansionParent() != null) && (expandedFrom != null)) {
warnings.add(
"The option '"
+ name
+ "' was expanded to from both options '"
+ entry.getExpansionParent()
+ "' and '"
+ expandedFrom
+ "'");
}
// Record the new value:
parsedValues.put(
field,
new OptionValueDescription(
name, null, value, priority, source, implicitDependant, expandedFrom, false));
}
} else {
parsedValues.put(
field,
new OptionValueDescription(
name, null, value, priority, source, implicitDependant, expandedFrom, false));
maybeAddDeprecationWarning(field);
}
}
private void addListValue(Field field, String originalName, Object value, OptionPriority priority,
String source, String implicitDependant, String expandedFrom) {
OptionValueDescription entry = parsedValues.get(field);
if (entry == null) {
entry =
new OptionValueDescription(
originalName,
/* originalValueString */ null,
ArrayListMultimap.create(),
priority,
source,
implicitDependant,
expandedFrom,
true);
parsedValues.put(field, entry);
maybeAddDeprecationWarning(field);
}
entry.addValue(priority, value);
}
OptionValueDescription clearValue(String optionName)
throws OptionsParsingException {
Field field = optionsData.getFieldFromName(optionName);
if (field == null) {
throw new IllegalArgumentException("No such option '" + optionName + "'");
}
// Actually remove the value from various lists tracking effective options.
canonicalizeValues.removeAll(field);
return parsedValues.remove(field);
}
OptionValueDescription getOptionValueDescription(String name) {
Field field = optionsData.getFieldFromName(name);
if (field == null) {
throw new IllegalArgumentException("No such option '" + name + "'");
}
return parsedValues.get(field);
}
OptionDescription getOptionDescription(String name) throws OptionsParsingException {
Field field = optionsData.getFieldFromName(name);
if (field == null) {
return null;
}
Option optionAnnotation = field.getAnnotation(Option.class);
return new OptionDescription(
name,
optionsData.getDefaultValue(field),
optionsData.getConverter(field),
optionsData.getAllowMultiple(field),
getExpansionDescriptions(
optionsData.getEvaluatedExpansion(field),
/* expandedFrom */ name,
/* implicitDependant */ null),
getExpansionDescriptions(
optionAnnotation.implicitRequirements(),
/* expandedFrom */ null,
/* implicitDependant */ name));
}
/**
* @return A list of the descriptions corresponding to the list of unparsed flags passed in.
* These descriptions are are divorced from the command line - there is no correct priority or
* source for these, as they are not actually set values. The value itself is also a string, no
* conversion has taken place.
*/
private ImmutableList getExpansionDescriptions(
String[] optionStrings, String expandedFrom, String implicitDependant)
throws OptionsParsingException {
ImmutableList.Builder builder = ImmutableList.builder();
ImmutableList options = ImmutableList.copyOf(optionStrings);
Iterator optionsIterator = options.iterator();
while (optionsIterator.hasNext()) {
String unparsedFlagExpression = optionsIterator.next();
ParseOptionResult parseResult = parseOption(unparsedFlagExpression, optionsIterator);
builder.add(new OptionValueDescription(
parseResult.option.name(),
parseResult.value,
/* value */ null,
/* priority */ null,
/* source */null,
implicitDependant,
expandedFrom,
optionsData.getAllowMultiple(parseResult.field)));
}
return builder.build();
}
boolean containsExplicitOption(String name) {
Field field = optionsData.getFieldFromName(name);
if (field == null) {
throw new IllegalArgumentException("No such option '" + name + "'");
}
return parsedValues.get(field) != null;
}
/**
* Parses the args, and returns what it doesn't parse. May be called multiple
* times, and may be called recursively. In each call, there may be no
* duplicates, but separate calls may contain intersecting sets of options; in
* that case, the arg seen last takes precedence.
*/
List parse(OptionPriority priority, Function super String, String> sourceFunction,
List args) throws OptionsParsingException {
return parse(priority, sourceFunction, null, null, args);
}
/**
* Parses the args, and returns what it doesn't parse. May be called multiple
* times, and may be called recursively. Calls may contain intersecting sets
* of options; in that case, the arg seen last takes precedence.
*
*
The method uses the invariant that if an option has neither an implicit
* dependent nor an expanded from value, then it must have been explicitly
* set.
*/
private List parse(
OptionPriority priority,
Function super String, String> sourceFunction,
String implicitDependent,
String expandedFrom,
List args) throws OptionsParsingException {
List unparsedArgs = new ArrayList<>();
LinkedHashMap> implicitRequirements = new LinkedHashMap<>();
Iterator argsIterator = argsPreProcessor.preProcess(args).iterator();
while (argsIterator.hasNext()) {
String arg = argsIterator.next();
if (!arg.startsWith("-")) {
unparsedArgs.add(arg);
continue; // not an option arg
}
if (arg.equals("--")) { // "--" means all remaining args aren't options
Iterators.addAll(unparsedArgs, argsIterator);
break;
}
ParseOptionResult parseOptionResult = parseOption(arg, argsIterator);
Field field = parseOptionResult.field;
Option option = parseOptionResult.option;
String value = parseOptionResult.value;
final String originalName = option.name();
if (option.wrapperOption()) {
if (value.startsWith("-")) {
List unparsed = parse(
priority,
Functions.constant("Unwrapped from wrapper option --" + originalName),
null, // implicitDependent
null, // expandedFrom
ImmutableList.of(value));
if (!unparsed.isEmpty()) {
throw new OptionsParsingException(
"Unparsed options remain after unwrapping "
+ arg
+ ": "
+ Joiner.on(' ').join(unparsed));
}
// Don't process implicitRequirements or expansions for wrapper options. In particular,
// don't record this option in unparsedValues, so that only the wrapped option shows
// up in canonicalized options.
continue;
} else {
throw new OptionsParsingException("Invalid --" + originalName + " value format. "
+ "You may have meant --" + originalName + "=--" + value);
}
}
if (implicitDependent == null) {
// Log explicit options and expanded options in the order they are parsed (can be sorted
// later). Also remember whether they were expanded or not. This information is needed to
// correctly canonicalize flags.
UnparsedOptionValueDescription unparsedOptionValueDescription =
new UnparsedOptionValueDescription(
originalName,
field,
value,
priority,
sourceFunction.apply(originalName),
expandedFrom == null);
unparsedValues.add(unparsedOptionValueDescription);
if (option.allowMultiple()) {
canonicalizeValues.put(field, unparsedOptionValueDescription);
} else {
canonicalizeValues.replaceValues(field, ImmutableList.of(unparsedOptionValueDescription));
}
}
// Handle expansion options.
String[] expansion = optionsData.getEvaluatedExpansion(field);
if (expansion.length > 0) {
Function