Search in sources :

Example 1 with UnparsedOptionValueDescription

use of com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription in project bazel by bazelbuild.

the class OptionsParserImpl method asCanonicalizedList.

/**
   * Implements {@link OptionsParser#canonicalize}.
   */
List<String> asCanonicalizedList() {
    List<UnparsedOptionValueDescription> 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<UnparsedOptionValueDescription>() {

        @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<String> result = Lists.newArrayList();
    for (UnparsedOptionValueDescription value : processed) {
        // Ignore expansion options.
        if (value.isExpansion()) {
            continue;
        }
        result.add("--" + value.getName() + "=" + value.getUnparsedValue());
    }
    return result;
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription)

Example 2 with UnparsedOptionValueDescription

use of com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription in project bazel by bazelbuild.

the class OptionsParserTest method asListOfUnparsedOptions.

@Test
public void asListOfUnparsedOptions() throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(IntrospectionExample.class);
    parser.parse(OptionPriority.COMMAND_LINE, "source", Arrays.asList("--alpha=one", "--gamma=two", "--echo=three"));
    List<UnparsedOptionValueDescription> result = parser.asListOfUnparsedOptions();
    assertNotNull(result);
    assertEquals(3, result.size());
    assertEquals("alpha", result.get(0).getName());
    assertEquals(true, result.get(0).isDocumented());
    assertEquals(false, result.get(0).isHidden());
    assertEquals("one", result.get(0).getUnparsedValue());
    assertEquals("source", result.get(0).getSource());
    assertEquals(OptionPriority.COMMAND_LINE, result.get(0).getPriority());
    assertEquals("gamma", result.get(1).getName());
    assertEquals(false, result.get(1).isDocumented());
    assertEquals(false, result.get(1).isHidden());
    assertEquals("two", result.get(1).getUnparsedValue());
    assertEquals("source", result.get(1).getSource());
    assertEquals(OptionPriority.COMMAND_LINE, result.get(1).getPriority());
    assertEquals("echo", result.get(2).getName());
    assertEquals(false, result.get(2).isDocumented());
    assertEquals(true, result.get(2).isHidden());
    assertEquals("three", result.get(2).getUnparsedValue());
    assertEquals("source", result.get(2).getSource());
    assertEquals(OptionPriority.COMMAND_LINE, result.get(2).getPriority());
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionsParser.newOptionsParser(com.google.devtools.common.options.OptionsParser.newOptionsParser) Test(org.junit.Test)

Example 3 with UnparsedOptionValueDescription

use of com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription in project bazel by bazelbuild.

the class OptionsParserTest method asListOfExplicitOptions.

@Test
public void asListOfExplicitOptions() throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(IntrospectionExample.class);
    parser.parse(OptionPriority.COMMAND_LINE, "source", Arrays.asList("--alpha=one", "--gamma=two"));
    List<UnparsedOptionValueDescription> result = parser.asListOfExplicitOptions();
    assertNotNull(result);
    assertEquals(2, result.size());
    assertEquals("alpha", result.get(0).getName());
    assertEquals(true, result.get(0).isDocumented());
    assertEquals("one", result.get(0).getUnparsedValue());
    assertEquals("source", result.get(0).getSource());
    assertEquals(OptionPriority.COMMAND_LINE, result.get(0).getPriority());
    assertEquals("gamma", result.get(1).getName());
    assertEquals(false, result.get(1).isDocumented());
    assertEquals("two", result.get(1).getUnparsedValue());
    assertEquals("source", result.get(1).getSource());
    assertEquals(OptionPriority.COMMAND_LINE, result.get(1).getPriority());
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionsParser.newOptionsParser(com.google.devtools.common.options.OptionsParser.newOptionsParser) Test(org.junit.Test)

Example 4 with UnparsedOptionValueDescription

use of com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription in project bazel by bazelbuild.

the class DashModule method gotOptions.

@Subscribe
public void gotOptions(GotOptionsEvent event) {
    BuildData.Builder builder = BuildData.newBuilder();
    BuildData.CommandLine.Builder cmdLineBuilder = BuildData.CommandLine.newBuilder();
    for (UnparsedOptionValueDescription option : event.getStartupOptions().asListOfUnparsedOptions()) {
        cmdLineBuilder.addStartupOptions(getOption(option));
    }
    for (UnparsedOptionValueDescription option : event.getOptions().asListOfUnparsedOptions()) {
        if (option.getName().equals("client_env")) {
            String[] env = option.getUnparsedValue().split("=");
            if (env.length == 1) {
                builder.addClientEnv(EnvironmentVar.newBuilder().setName(env[0]).setValue("true").build());
            } else if (env.length == 2) {
                builder.addClientEnv(EnvironmentVar.newBuilder().setName(env[0]).setValue(env[1]).build());
            }
        } else {
            cmdLineBuilder.addOptions(getOption(option));
        }
    }
    for (String residue : event.getOptions().getResidue()) {
        cmdLineBuilder.addResidue(residue);
    }
    builder.setCommandLine(cmdLineBuilder.build());
    // This can be called before handleOptions, so the BuildData is stored until we know if it
    // should be sent somewhere.
    optionsBuildData = builder.build();
}
Also used : BuildData(com.google.devtools.build.lib.bazel.dash.DashProtos.BuildData) UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) ByteString(com.google.protobuf.ByteString) Subscribe(com.google.common.eventbus.Subscribe)

Example 5 with UnparsedOptionValueDescription

use of com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription in project bazel by bazelbuild.

the class OptionsParserImpl method parse.

/**
   * 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.
   *
   * <p>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<String> parse(OptionPriority priority, Function<? super String, String> sourceFunction, String implicitDependent, String expandedFrom, List<String> args) throws OptionsParsingException {
    List<String> unparsedArgs = Lists.newArrayList();
    LinkedHashMap<String, List<String>> implicitRequirements = Maps.newLinkedHashMap();
    Iterator<String> argsIterator = argsPreProcessor.preProcess(args).iterator();
    while (argsIterator.hasNext()) {
        String arg = argsIterator.next();
        if (!arg.startsWith("-")) {
            unparsedArgs.add(arg);
            // not an option arg
            continue;
        }
        if (arg.equals("--")) {
            // "--" means all remaining args aren't options
            Iterators.addAll(unparsedArgs, argsIterator);
            break;
        }
        ParseOptionResult optionParseResult = parseOption(arg, argsIterator);
        Field field = optionParseResult.field;
        Option option = optionParseResult.option;
        String value = optionParseResult.value;
        final String originalName = option.name();
        if (option.wrapperOption()) {
            if (value.startsWith("-")) {
                List<String> unparsed = parse(priority, Functions.constant("Unwrapped from wrapper option --" + originalName), // implicitDependent
                null, // expandedFrom
                null, ImmutableList.of(value));
                if (!unparsed.isEmpty()) {
                    throw new OptionsParsingException("Unparsed options remain after unwrapping " + arg + ": " + Joiner.on(' ').join(unparsed));
                }
                // 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.
        if (option.expansion().length > 0) {
            Function<Object, String> expansionSourceFunction = Functions.constant("expanded from option --" + originalName + " from " + sourceFunction.apply(originalName));
            maybeAddDeprecationWarning(field);
            List<String> unparsed = parse(priority, expansionSourceFunction, null, originalName, ImmutableList.copyOf(option.expansion()));
            if (!unparsed.isEmpty()) {
                // expansion for the current option.
                throw new AssertionError("Unparsed options remain after parsing expansion of " + arg + ": " + Joiner.on(' ').join(unparsed));
            }
        } else {
            Converter<?> converter = optionsData.getConverter(field);
            Object convertedValue;
            try {
                convertedValue = converter.convert(value);
            } catch (OptionsParsingException e) {
                // re-throwing:
                throw new OptionsParsingException("While parsing option " + arg + ": " + e.getMessage(), e);
            }
            // parse(); latest wins:
            if (!option.allowMultiple()) {
                setValue(field, originalName, convertedValue, priority, sourceFunction.apply(originalName), implicitDependent, expandedFrom);
            } else {
                // But if it's a multiple-use option, then just accumulate the
                // values, in the order in which they were seen.
                // Note: The type of the list member is not known; Java introspection
                // only makes it available in String form via the signature string
                // for the field declaration.
                addListValue(field, convertedValue, priority, sourceFunction.apply(originalName), implicitDependent, expandedFrom);
            }
        }
        // Collect any implicit requirements.
        if (option.implicitRequirements().length > 0) {
            implicitRequirements.put(option.name(), Arrays.asList(option.implicitRequirements()));
        }
    }
    // TODO(bazel-team): this should happen when the option is encountered.
    if (!implicitRequirements.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : implicitRequirements.entrySet()) {
            Function<Object, String> requirementSourceFunction = Functions.constant("implicit requirement of option --" + entry.getKey() + " from " + sourceFunction.apply(entry.getKey()));
            List<String> unparsed = parse(priority, requirementSourceFunction, entry.getKey(), null, entry.getValue());
            if (!unparsed.isEmpty()) {
                // implicit requirements for the option(s).
                throw new AssertionError("Unparsed options remain after parsing implicit options: " + Joiner.on(' ').join(unparsed));
            }
        }
    }
    return unparsedArgs;
}
Also used : Field(java.lang.reflect.Field) UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

UnparsedOptionValueDescription (com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription)6 OptionsParser.newOptionsParser (com.google.devtools.common.options.OptionsParser.newOptionsParser)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 Subscribe (com.google.common.eventbus.Subscribe)1 BuildData (com.google.devtools.build.lib.bazel.dash.DashProtos.BuildData)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 ByteString (com.google.protobuf.ByteString)1 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1