Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.common.options; |
| 16 | |
| 17 | import com.google.devtools.common.options.Converters.BooleanConverter; |
| 18 | |
| 19 | /** |
ahumesky | be31bb8 | 2018-07-26 13:37:45 -0700 | [diff] [blame] | 20 | * Converter that can convert both the standard set of boolean string values and enumerations. If |
| 21 | * there is an overlap in values, those from the underlying enumeration will be taken. |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 22 | * |
ahumesky | be31bb8 | 2018-07-26 13:37:45 -0700 | [diff] [blame] | 23 | * <p>Note that for the flag to take one of its enum values on the command line, it must be of the |
| 24 | * form "--flag=value". That is, "--flag value" and "-f value" (if the flag has a short-form of "f") |
| 25 | * will result in "value" being left as residue on the command line. This maintains compatibility |
| 26 | * with boolean flags where "--flag true" and "-f true" also leave "true" as residue on the command |
| 27 | * line. |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 28 | */ |
ahumesky | be31bb8 | 2018-07-26 13:37:45 -0700 | [diff] [blame] | 29 | public abstract class BoolOrEnumConverter<T extends Enum<T>> extends EnumConverter<T> { |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 30 | private T falseValue; |
| 31 | private T trueValue; |
| 32 | |
| 33 | /** |
| 34 | * You *must* implement a zero-argument constructor that delegates |
| 35 | * to this constructor, passing in the appropriate parameters. This |
| 36 | * comes from the base {@link EnumConverter} class. |
| 37 | * |
| 38 | * @param enumType The type of your enumeration; usually a class literal |
| 39 | * like MyEnum.class |
| 40 | * @param typeName The intuitive name of your enumeration, for example, the |
| 41 | * type name for CompilationMode might be "compilation mode". |
| 42 | * @param trueValue The enumeration value to associate with {@code true}. |
| 43 | * @param falseValue The enumeration value to associate with {@code false}. |
| 44 | */ |
| 45 | protected BoolOrEnumConverter(Class<T> enumType, String typeName, T trueValue, T falseValue) { |
| 46 | super(enumType, typeName); |
| 47 | this.trueValue = trueValue; |
| 48 | this.falseValue = falseValue; |
| 49 | } |
| 50 | |
Ulf Adams | 6f09666 | 2016-06-27 15:51:23 +0000 | [diff] [blame] | 51 | @Override |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 52 | public T convert(String input) throws OptionsParsingException { |
| 53 | try { |
| 54 | return super.convert(input); |
| 55 | } catch (OptionsParsingException eEnum) { |
| 56 | try { |
| 57 | BooleanConverter booleanConverter = new BooleanConverter(); |
| 58 | boolean value = booleanConverter.convert(input); |
| 59 | return value ? trueValue : falseValue; |
| 60 | } catch (OptionsParsingException eBoolean) { |
ahumesky | be31bb8 | 2018-07-26 13:37:45 -0700 | [diff] [blame] | 61 | // TODO(b/111883901): Rethrowing the exception from the enum converter does not report the |
| 62 | // allowable boolean values. |
Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 63 | throw eEnum; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |