blob: 17bf7a9a489147cf5cb67c095ebf511191b3ac69 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Googler173ee822015-05-19 09:46:33 +00002//
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
15package com.google.devtools.common.options;
16
17import com.google.devtools.common.options.Converters.BooleanConverter;
18
19/**
ahumeskybe31bb82018-07-26 13:37:45 -070020 * 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.
Googler173ee822015-05-19 09:46:33 +000022 *
ahumeskybe31bb82018-07-26 13:37:45 -070023 * <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.
Googler173ee822015-05-19 09:46:33 +000028 */
ahumeskybe31bb82018-07-26 13:37:45 -070029public abstract class BoolOrEnumConverter<T extends Enum<T>> extends EnumConverter<T> {
Googler173ee822015-05-19 09:46:33 +000030 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 Adams6f096662016-06-27 15:51:23 +000051 @Override
Googler173ee822015-05-19 09:46:33 +000052 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) {
ahumeskybe31bb82018-07-26 13:37:45 -070061 // TODO(b/111883901): Rethrowing the exception from the enum converter does not report the
62 // allowable boolean values.
Googler173ee822015-05-19 09:46:33 +000063 throw eEnum;
64 }
65 }
66 }
67}