src/main/java/com/google/devtools/build/lib/runtime/StarlarkOptionsParser.java
// require multiple rounds of parsing to fit starlark-defined options into native option format.
require multiple rounds of parsing to fit starlark-defined options into native option format.
require multiple rounds of parsing to fit starlark - defined options into native option format .
@VisibleForTesting
  public void parse(ExtendedEventHandler eventHandler) throws OptionsParsingException {
    ImmutableList.Builder<String> residue = new ImmutableList.Builder<>();
    Map<String, Pair<String, Target>> unparsedOptions =
        Maps.newHashMapWithExpectedSize(nativeOptionsParser.getResidue().size());
    for (String arg : nativeOptionsParser.getPreDoubleDashResidue()) {
      if (!arg.startsWith("--")) {
        residue.add(arg);
        continue;
      }
      parseArg(arg, unparsedOptions, eventHandler);
    }
    List<String> postDoubleDashResidue = nativeOptionsParser.getPostDoubleDashResidue();
    residue.addAll(postDoubleDashResidue);
    nativeOptionsParser.setResidue(residue.build(), postDoubleDashResidue);
    if (unparsedOptions.isEmpty()) {
      return;
    }
    ImmutableMap.Builder<String, Object> parsedOptions = new ImmutableMap.Builder<>();
    for (Map.Entry<String, Pair<String, Target>> option : unparsedOptions.entrySet()) {
      String loadedFlag = option.getKey();
      String unparsedValue = option.getValue().first;
      Target buildSettingTarget = option.getValue().second;
      BuildSetting buildSetting =
          buildSettingTarget.getAssociatedRule().getRuleClassObject().getBuildSetting();
      if (!buildSetting.isFlag()) {
        throw new OptionsParsingException(
            String.format("Unrecognized option: %s=%s", loadedFlag, unparsedValue));
      }
      Type<?> type = buildSetting.getType();
      Converter<?> converter = BUILD_SETTING_CONVERTERS.get(type);
      Object value;
      try {
        value = converter.convert(unparsedValue);
      } catch (OptionsParsingException e) {
        throw new OptionsParsingException(
            String.format(
                "While parsing option %s=%s: '%s' is not a %s",
                loadedFlag, unparsedValue, unparsedValue, type),
            e);
      }
      if (!value.equals(
          buildSettingTarget
              .getAssociatedRule()
              .getAttr(STARLARK_BUILD_SETTING_DEFAULT_ATTR_NAME))) {
        parsedOptions.put(loadedFlag, value);
      }
    }
    nativeOptionsParser.setStarlarkOptions(parsedOptions.build());
  }
@ VisibleForTesting public void parse ( ExtendedEventHandler eventHandler ) throws OptionsParsingException { ImmutableList . Builder < String > residue = new ImmutableList . Builder < > ( ) ; Map < String , Pair < String , Target > > unparsedOptions = Maps . newHashMapWithExpectedSize ( nativeOptionsParser . getResidue ( ) . size ( ) ) ; for ( String arg : nativeOptionsParser . getPreDoubleDashResidue ( ) ) { if ( ! arg . startsWith ( "--" ) ) { residue . add ( arg ) ; continue ; } parseArg ( arg , unparsedOptions , eventHandler ) ; } List < String > postDoubleDashResidue = nativeOptionsParser . getPostDoubleDashResidue ( ) ; residue . addAll ( postDoubleDashResidue ) ; nativeOptionsParser . setResidue ( residue . build ( ) , postDoubleDashResidue ) ; if ( unparsedOptions . isEmpty ( ) ) { return ; } ImmutableMap . Builder < String , Object > parsedOptions = new ImmutableMap . Builder < > ( ) ; for ( Map . Entry < String , Pair < String , Target > > option : unparsedOptions . entrySet ( ) ) { String loadedFlag = option . getKey ( ) ; String unparsedValue = option . getValue ( ) . first ; Target buildSettingTarget = option . getValue ( ) . second ; BuildSetting buildSetting = buildSettingTarget . getAssociatedRule ( ) . getRuleClassObject ( ) . getBuildSetting ( ) ; if ( ! buildSetting . isFlag ( ) ) { throw new OptionsParsingException ( String . format ( "Unrecognized option: %s=%s" , loadedFlag , unparsedValue ) ) ; } Type < ? > type = buildSetting . getType ( ) ; Converter < ? > converter = BUILD_SETTING_CONVERTERS . get ( type ) ; Object value ; try { value = converter . convert ( unparsedValue ) ; } catch ( OptionsParsingException e ) { throw new OptionsParsingException ( String . format ( "While parsing option %s=%s: '%s' is not a %s" , loadedFlag , unparsedValue , unparsedValue , type ) , e ) ; } if ( ! value . equals ( buildSettingTarget . getAssociatedRule ( ) . getAttr ( STARLARK_BUILD_SETTING_DEFAULT_ATTR_NAME ) ) ) { parsedOptions . put ( loadedFlag , value ) ; } } nativeOptionsParser . setStarlarkOptions ( parsedOptions . build ( ) ) ; }
require multiple rounds of parsing to fit starlark-defined options into native option format.
require multiple rounds of parsing to fit starlark - defined options into native option format .
// Map of <option name (label), <unparsed option value, loaded option>>. // sort the old residue into starlark flags and legitimate residue // TODO(bazel-team): support single dash options? // Do not recognize internal options, which are treated as if they did not exist.