Search in sources :

Example 6 with OptionValueDescription

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

the class OptionsParserTest method getOptionValueDescriptionWithValue.

@Test
public void getOptionValueDescriptionWithValue() throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(NullTestOptions.class);
    parser.parse(OptionPriority.COMMAND_LINE, "my description", Arrays.asList("--simple=abc"));
    OptionValueDescription result = parser.getOptionValueDescription("simple");
    assertNotNull(result);
    assertEquals("simple", result.getName());
    assertEquals("abc", result.getValue());
    assertEquals(OptionPriority.COMMAND_LINE, result.getPriority());
    assertEquals("my description", result.getSource());
    assertNull(result.getImplicitDependant());
    assertFalse(result.isImplicitDependency());
    assertNull(result.getExpansionParent());
    assertFalse(result.isExpansion());
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription) OptionsParser.newOptionsParser(com.google.devtools.common.options.OptionsParser.newOptionsParser) Test(org.junit.Test)

Example 7 with OptionValueDescription

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

the class OptionsParserImpl method getParsedOptions.

/**
   * Gets the result of parsing the options.
   */
<O extends OptionsBase> O getParsedOptions(Class<O> optionsClass) {
    // Create the instance:
    O optionsInstance;
    try {
        Constructor<O> constructor = optionsData.getConstructor(optionsClass);
        if (constructor == null) {
            return null;
        }
        optionsInstance = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    // Set the fields
    for (Field field : optionsData.getFieldsForClass(optionsClass)) {
        Object value;
        OptionValueDescription entry = parsedValues.get(field);
        if (entry == null) {
            value = optionsData.getDefaultValue(field);
        } else {
            value = entry.getValue();
        }
        try {
            field.set(optionsInstance, value);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    return optionsInstance;
}
Also used : Field(java.lang.reflect.Field) UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription)

Example 8 with OptionValueDescription

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

the class OptionsParserImpl method addListValue.

private void addListValue(Field field, Object value, OptionPriority priority, String source, String implicitDependant, String expandedFrom) {
    OptionValueDescription entry = parsedValues.get(field);
    if (entry == null) {
        entry = new OptionValueDescription(field.getName(), ArrayListMultimap.create(), priority, source, implicitDependant, expandedFrom, true);
        parsedValues.put(field, entry);
        maybeAddDeprecationWarning(field);
    }
    entry.addValue(priority, value);
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription)

Example 9 with OptionValueDescription

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

the class InvocationPolicyEnforcer method enforce.

/**
   * Applies this OptionsPolicyEnforcer's policy to the given OptionsParser.
   *
   * @param parser The OptionsParser to enforce policy on.
   * @param command The current blaze command, for flag policies that apply to only specific
   *     commands. Such policies will be enforced only if they contain this command or a command
   *     they inherit from
   * @throws OptionsParsingException if any flag policy is invalid.
   */
public void enforce(OptionsParser parser, @Nullable String command) throws OptionsParsingException {
    if (invocationPolicy == null || invocationPolicy.getFlagPoliciesCount() == 0) {
        return;
    }
    ImmutableSet<String> commandAndParentCommands = command == null ? ImmutableSet.<String>of() : CommandNameCache.CommandNameCacheInstance.INSTANCE.get(command);
    for (FlagPolicy flagPolicy : invocationPolicy.getFlagPoliciesList()) {
        String flagName = flagPolicy.getFlagName();
        // then the policy applies to all commands.
        if (!flagPolicy.getCommandsList().isEmpty() && !commandAndParentCommands.isEmpty()) {
            boolean flagApplies = false;
            for (String policyCommand : flagPolicy.getCommandsList()) {
                if (commandAndParentCommands.contains(policyCommand)) {
                    flagApplies = true;
                    break;
                }
            }
            if (!flagApplies) {
                continue;
            }
        }
        OptionValueDescription valueDescription;
        try {
            valueDescription = parser.getOptionValueDescription(flagName);
        } catch (IllegalArgumentException e) {
            // This flag doesn't exist. We are deliberately lenient if the flag policy has a flag
            // we don't know about. This is for better future proofing so that as new flags are added,
            // new policies can use the new flags without worrying about older versions of Bazel.
            log.info(String.format("Flag '%s' specified by invocation policy does not exist", flagName));
            continue;
        }
        OptionDescription optionDescription = parser.getOptionDescription(flagName);
        // getOptionDescription() will return null if the option does not exist, however
        // getOptionValueDescription() above would have thrown an IllegalArgumentException if that
        // were the case.
        Verify.verifyNotNull(optionDescription);
        switch(flagPolicy.getOperationCase()) {
            case SET_VALUE:
                applySetValueOperation(parser, flagPolicy, flagName, valueDescription, optionDescription);
                break;
            case USE_DEFAULT:
                applyUseDefaultOperation(parser, "UseDefault", flagName);
                break;
            case ALLOW_VALUES:
                AllowValues allowValues = flagPolicy.getAllowValues();
                FilterValueOperation.ALLOW_VALUE_OPERATION.apply(parser, allowValues.getAllowedValuesList(), allowValues.hasNewValue() ? allowValues.getNewValue() : null, allowValues.hasUseDefault(), flagName, valueDescription, optionDescription);
                break;
            case DISALLOW_VALUES:
                DisallowValues disallowValues = flagPolicy.getDisallowValues();
                FilterValueOperation.DISALLOW_VALUE_OPERATION.apply(parser, disallowValues.getDisallowedValuesList(), disallowValues.hasNewValue() ? disallowValues.getNewValue() : null, disallowValues.hasUseDefault(), flagName, valueDescription, optionDescription);
                break;
            case OPERATION_NOT_SET:
                throw new OptionsParsingException(String.format("Flag policy for flag '%s' does not " + "have an operation", flagName));
            default:
                log.warning(String.format("Unknown operation '%s' from invocation policy for flag '%s'", flagPolicy.getOperationCase(), flagName));
                break;
        }
    }
}
Also used : OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription) OptionDescription(com.google.devtools.common.options.OptionsParser.OptionDescription) FlagPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy) DisallowValues(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues) AllowValues(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException)

Aggregations

OptionValueDescription (com.google.devtools.common.options.OptionsParser.OptionValueDescription)9 UnparsedOptionValueDescription (com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription)7 OptionsParser.newOptionsParser (com.google.devtools.common.options.OptionsParser.newOptionsParser)2 Field (java.lang.reflect.Field)2 Test (org.junit.Test)2 AllowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues)1 DisallowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues)1 FlagPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy)1 OptionDescription (com.google.devtools.common.options.OptionsParser.OptionDescription)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1