ParsedOptionDescription.java revision cff0d218a8d061b3568a6197b8d456424d9c9c4a
1// Copyright 2017 The Bazel Authors. All rights reserved.
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
15package com.google.devtools.common.options;
16
17import com.google.common.collect.ImmutableList;
18import javax.annotation.Nullable;
19
20/**
21 * The value of an option with additional metadata describing its origin.
22 *
23 * <p>This class represents an option as the parser received it, which is distinct from the final
24 * value of an option, as these values may be overridden or combined in some way.
25 *
26 * <p>The origin includes the form it had when parsed, its priority, a message about where it came
27 * from, and whether it was set explicitly or expanded/implied by other flags.
28 */
29public final class ParsedOptionDescription {
30  private final OptionDefinition optionDefinition;
31  private final String commandLineForm;
32  @Nullable private final String unconvertedValue;
33  private final OptionPriority priority;
34  @Nullable private final String source;
35
36  // Whether this flag was explicitly given, as opposed to having been added by an expansion flag
37  // or an implicit dependency. Notice that this does NOT mean it was explicitly given by the
38  // user, for that to be true, it needs the right combination of explicit & priority.
39  private final boolean explicit;
40
41  public ParsedOptionDescription(
42      OptionDefinition optionDefinition,
43      String commandLineForm,
44      @Nullable String unconvertedValue,
45      OptionPriority priority,
46      @Nullable String source,
47      boolean explicit) {
48    this.optionDefinition = optionDefinition;
49    this.commandLineForm = commandLineForm;
50    this.unconvertedValue = unconvertedValue;
51    this.priority = priority;
52    this.source = source;
53    this.explicit = explicit;
54  }
55
56  public OptionDefinition getOptionDefinition() {
57    return optionDefinition;
58  }
59
60  public String getCommandLineForm() {
61    return commandLineForm;
62  }
63
64  public boolean isBooleanOption() {
65    return optionDefinition.getType().equals(boolean.class);
66  }
67
68  private OptionDocumentationCategory documentationCategory() {
69    return optionDefinition.getDocumentationCategory();
70  }
71
72  private ImmutableList<OptionMetadataTag> metadataTags() {
73    return ImmutableList.copyOf(optionDefinition.getOptionMetadataTags());
74  }
75
76  public boolean isDocumented() {
77    return documentationCategory() != OptionDocumentationCategory.UNDOCUMENTED && !isHidden();
78  }
79
80  public boolean isHidden() {
81    ImmutableList<OptionMetadataTag> tags = metadataTags();
82    return tags.contains(OptionMetadataTag.HIDDEN) || tags.contains(OptionMetadataTag.INTERNAL);
83  }
84
85  boolean isExpansion() {
86    return optionDefinition.isExpansionOption();
87  }
88
89  boolean isImplicitRequirement() {
90    return optionDefinition.getImplicitRequirements().length > 0;
91  }
92
93  public String getUnconvertedValue() {
94    return unconvertedValue;
95  }
96
97  OptionPriority getPriority() {
98    return priority;
99  }
100
101  public String getSource() {
102    return source;
103  }
104
105  public boolean isExplicit() {
106    return explicit;
107  }
108
109  public Object getConvertedValue() throws OptionsParsingException {
110    Converter<?> converter = optionDefinition.getConverter();
111    try {
112      return converter.convert(unconvertedValue);
113    } catch (OptionsParsingException e) {
114      // The converter doesn't know the option name, so we supply it here by re-throwing:
115      throw new OptionsParsingException(
116          String.format("While parsing option %s: %s", commandLineForm, e.getMessage()), e);
117    }
118  }
119
120  @Override
121  public String toString() {
122    StringBuilder result = new StringBuilder();
123    result.append("option '").append(optionDefinition.getOptionName()).append("' ");
124    result.append("set to '").append(unconvertedValue).append("' ");
125    result.append("with priority ").append(priority);
126    if (source != null) {
127      result.append(" and source '").append(source).append("'");
128    }
129    return result.toString();
130  }
131}
132