1 // Copyright 2014 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 package com.google.devtools.common.options; 15 16 import java.lang.annotation.ElementType; 17 import java.lang.annotation.Retention; 18 import java.lang.annotation.RetentionPolicy; 19 import java.lang.annotation.Target; 20 21 /** 22 * An interface for annotating fields in classes (derived from OptionsBase) 23 * that are options. 24 */ 25 @Target(ElementType.FIELD) 26 @Retention(RetentionPolicy.RUNTIME) 27 public @interface Option { 28 /** 29 * The name of the option ("--name"). 30 */ name()31 String name(); 32 33 /** 34 * The single-character abbreviation of the option ("-abbrev"). 35 */ abbrev()36 char abbrev() default '\0'; 37 38 /** 39 * A help string for the usage information. 40 */ help()41 String help() default ""; 42 43 /** 44 * A short text string to describe the type of the expected value. E.g., <code>regex</code>. This 45 * is ignored for boolean, tristate, boolean_or_enum, and void options. 46 */ valueHelp()47 String valueHelp() default ""; 48 49 /** 50 * The default value for the option. This method should only be invoked directly by the parser 51 * implementation. Any access to default values should go via the parser to allow for application 52 * specific defaults. 53 * 54 * <p>There are two reasons this is a string. Firstly, it ensures that explicitly specifying this 55 * option at its default value (as printed in the usage message) has the same behavior as not 56 * specifying the option at all; this would be very hard to achieve if the default value was an 57 * instance of type T, since we'd need to ensure that {@link #toString()} and {@link #converter} 58 * were dual to each other. The second reason is more mundane but also more restrictive: 59 * annotation values must be compile-time constants. 60 * 61 * <p>If an option's defaultValue() is the string "null", the option's converter will not be 62 * invoked to interpret it; a null reference will be used instead. (It would be nice if 63 * defaultValue could simply return null, but bizarrely, the Java Language Specification does not 64 * consider null to be a compile-time constant.) This special interpretation of the string "null" 65 * is only applicable when computing the default value; if specified on the command-line, this 66 * string will have its usual literal meaning. 67 * 68 * <p>The default value for flags that set allowMultiple is always the empty list and its default 69 * value is ignored. 70 */ defaultValue()71 String defaultValue(); 72 73 /** 74 * A string describing the category of options that this belongs to. {@link 75 * OptionsParser#describeOptions} prints options of the same category grouped together. 76 * 77 * <p>There are three special category values: 78 * 79 * <ul> 80 * <li>{@code "undocumented"}: options which are useful for (some subset of) users, but not 81 * meant to be publicly advertised. For example, experimental options which are only meant 82 * to be used by specific testers or team members, but which should otherwise be treated 83 * normally. These options will not be listed in the usage info displayed for the {@code 84 * --help} option. They are otherwise normal - {@link 85 * OptionsParser.UnparsedOptionValueDescription#isHidden()} returns {@code false} for them, 86 * and they can be parsed normally from the command line or RC files. 87 * <li>{@code "hidden"}: options which users should not pass or know about, but which are used 88 * by the program (e.g., communication between a command-line client and a backend server). 89 * Like {@code "undocumented"} options, these options will not be listed in the usage info 90 * displayed for the {@code --help} option. However, in addition to this, calling {@link 91 * OptionsParser.UnparsedOptionValueDescription#isHidden()} on these options will return 92 * {@code true} - for example, this can be checked to strip out such secret options when 93 * logging or otherwise reporting the command line to the user. This category does not 94 * affect the option in any other way; it can still be parsed normally from the command line 95 * or an RC file. 96 * <li>{@code "internal"}: options which are purely for internal use within the JVM, and should 97 * never be shown to the user, nor ever need to be parsed by the options parser. Like {@code 98 * "hidden"} options, these options will not be listed in the usage info displayed for the 99 * --help option, and are considered hidden by {@link 100 * OptionsParser.UnparsedOptionValueDescription#isHidden()}. Unlike those, this type of 101 * option cannot be parsed by any call to {@link OptionsParser#parse} - it will be treated 102 * as if it was not defined. 103 * </ul> 104 */ category()105 String category() default "misc"; 106 107 /** 108 * The converter that we'll use to convert the string representation of this option's value into 109 * an object or a simple type. The default is to use the builtin converters ({@link 110 * Converters#DEFAULT_CONVERTERS}). Custom converters must implement the {@link Converter} 111 * interface. 112 */ 113 @SuppressWarnings({"unchecked", "rawtypes"}) 114 // Can't figure out how to coerce Converter.class into Class<? extends Converter<?>> converter()115 Class<? extends Converter> converter() default Converter.class; 116 117 /** 118 * A flag indicating whether the option type should be allowed to occur multiple times in a single 119 * option list. 120 * 121 * <p>If the command can occur multiple times, then the attribute value <em>must</em> be a list 122 * type {@code List<T>}, and the result type of the converter for this option must either match 123 * the parameter {@code T} or {@code List<T>}. In the latter case the individual lists are 124 * concatenated to form the full options value. 125 * 126 * <p>The {@link #defaultValue()} field of the annotation is ignored for repeatable flags and the 127 * default value will be the empty list. 128 */ allowMultiple()129 boolean allowMultiple() default false; 130 131 /** 132 * If the option is actually an abbreviation for other options, this field will contain the 133 * strings to expand this option into. The original option is dropped and the replacement used in 134 * its stead. It is recommended that such an option be of type {@link Void}. 135 * 136 * <p>An expanded option overrides previously specified options of the same name, even if it is 137 * explicitly specified. This is the original behavior and can be surprising if the user is not 138 * aware of it, which has led to several requests to change this behavior. This was discussed in 139 * the blaze team and it was decided that it is not a strong enough case to change the behavior. 140 */ expansion()141 String[] expansion() default {}; 142 143 /** 144 * A mechanism for specifying an expansion that is a function of the parser's {@link 145 * IsolatedOptionsData}. This can be used to create an option that expands to different strings 146 * depending on what other options the parser knows about. 147 * 148 * <p>If provided (i.e. not {@link ExpansionFunction}{@code .class}), the {@code expansion} field 149 * must not be set. The mechanism of expansion is as if the {@code expansion} field were set to 150 * whatever the return value of this function is. 151 */ expansionFunction()152 Class<? extends ExpansionFunction> expansionFunction() default ExpansionFunction.class; 153 154 /** 155 * If the option requires that additional options be implicitly appended, this field will contain 156 * the additional options. Implicit dependencies are parsed at the end of each {@link 157 * OptionsParser#parse} invocation, and override options specified in the same call. However, they 158 * can be overridden by options specified in a later call or by options with a higher priority. 159 * 160 * @see OptionPriority 161 */ implicitRequirements()162 String[] implicitRequirements() default {}; 163 164 /** 165 * If this field is a non-empty string, the option is deprecated, and a deprecation warning is 166 * added to the list of warnings when such an option is used. 167 */ deprecationWarning()168 String deprecationWarning() default ""; 169 170 /** 171 * The old name for this option. If an option has a name "foo" and an old name "bar", --foo=baz 172 * and --bar=baz will be equivalent. If the old name is used, a warning will be printed indicating 173 * that the old name is deprecated and the new name should be used. 174 */ oldName()175 String oldName() default ""; 176 177 /** 178 * Indicates that this option is a wrapper for other options, and will be unwrapped when parsed. 179 * For example, if foo is a wrapper option, then "--foo=--bar=baz" will be parsed as the flag 180 * "--bar=baz" (rather than --foo taking the value "--bar=baz"). A wrapper option should have the 181 * type {@link Void} (if it is something other than Void, the parser will not assign a value to 182 * it). The {@link Option#implicitRequirements()}, {@link Option#expansion()}, {@link 183 * Option#converter()} attributes will not be processed. Wrapper options are implicitly repeatable 184 * (i.e., as though {@link Option#allowMultiple()} is true regardless of its value in the 185 * annotation). 186 * 187 * <p>Wrapper options are provided only for transitioning flags which appear as values to other 188 * flags, to top-level flags. Wrapper options should not be used in Invocation Policy, as 189 * expansion flags to other flags, or as implicit requirements to other flags. Use the inner flags 190 * instead. 191 */ wrapperOption()192 boolean wrapperOption() default false; 193 } 194