src/share/classes/com/sun/tools/javac/main/JavacOption.java

changeset 11
b66d15dfd001
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
equal deleted inserted replaced
10:508c01999047 11:b66d15dfd001
26 package com.sun.tools.javac.main; 26 package com.sun.tools.javac.main;
27 27
28 import com.sun.tools.javac.util.Log; 28 import com.sun.tools.javac.util.Log;
29 import com.sun.tools.javac.util.Options; 29 import com.sun.tools.javac.util.Options;
30 import java.io.PrintWriter; 30 import java.io.PrintWriter;
31 import java.util.Arrays;
32 import java.util.Collection;
31 33
32 /** 34 /**
33 * TODO: describe com.sun.tools.javac.main.JavacOption 35 * TODO: describe com.sun.tools.javac.main.JavacOption
34 * 36 *
35 * <p><b>This is NOT part of any API supported by Sun Microsystems. 37 * <p><b>This is NOT part of any API supported by Sun Microsystems.
39 */ 41 */
40 public interface JavacOption { 42 public interface JavacOption {
41 43
42 OptionKind getKind(); 44 OptionKind getKind();
43 45
44 /** Does this option take a (separate) operand? */ 46 /** Does this option take a (separate) operand?
47 * @return true if this option takes a separate operand
48 */
45 boolean hasArg(); 49 boolean hasArg();
46 50
47 /** Does argument string match option pattern? 51 /** Does argument string match option pattern?
48 * @param arg The command line argument string. 52 * @param arg the command line argument string
53 * @return true if {@code arg} matches this option
49 */ 54 */
50 boolean matches(String arg); 55 boolean matches(String arg);
51 56
52 /** Process the option (with arg). Return true if error detected. 57 /** Process an option with an argument.
58 * @param options the accumulated set of analyzed options
59 * @param option the option to be processed
60 * @param arg the arg for the option to be processed
61 * @return true if an error was detected
53 */ 62 */
54 boolean process(Options options, String option, String arg); 63 boolean process(Options options, String option, String arg);
55 64
56 /** Process the option (without arg). Return true if error detected. 65 /** Process the option with no argument.
66 * @param options the accumulated set of analyzed options
67 * @param option the option to be processed
68 * @return true if an error was detected
57 */ 69 */
58 boolean process(Options options, String option); 70 boolean process(Options options, String option);
59 71
60 OptionName getName(); 72 OptionName getName();
61 73
63 NORMAL, 75 NORMAL,
64 EXTENDED, 76 EXTENDED,
65 HIDDEN, 77 HIDDEN,
66 } 78 }
67 79
80 enum ChoiceKind {
81 ONEOF,
82 ANYOF
83 }
84
68 /** This class represents an option recognized by the main program 85 /** This class represents an option recognized by the main program
69 */ 86 */
70 static class Option implements JavacOption { 87 static class Option implements JavacOption {
71 88
72 /** Option string. 89 /** Option string.
82 String descrKey; 99 String descrKey;
83 100
84 /** Suffix option (-foo=bar or -foo:bar) 101 /** Suffix option (-foo=bar or -foo:bar)
85 */ 102 */
86 boolean hasSuffix; 103 boolean hasSuffix;
104
105 /** The kind of choices for this option, if any.
106 */
107 ChoiceKind choiceKind;
108
109 /** The choices for this option, if any.
110 */
111 Collection<String> choices;
87 112
88 Option(OptionName name, String argsNameKey, String descrKey) { 113 Option(OptionName name, String argsNameKey, String descrKey) {
89 this.name = name; 114 this.name = name;
90 this.argsNameKey = argsNameKey; 115 this.argsNameKey = argsNameKey;
91 this.descrKey = descrKey; 116 this.descrKey = descrKey;
92 char lastChar = name.optionName.charAt(name.optionName.length()-1); 117 char lastChar = name.optionName.charAt(name.optionName.length()-1);
93 hasSuffix = lastChar == ':' || lastChar == '='; 118 hasSuffix = lastChar == ':' || lastChar == '=';
94 } 119 }
120
95 Option(OptionName name, String descrKey) { 121 Option(OptionName name, String descrKey) {
96 this(name, null, descrKey); 122 this(name, null, descrKey);
97 } 123 }
98 124
125 Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) {
126 this(name, descrKey, choiceKind, Arrays.asList(choices));
127 }
128
129 Option(OptionName name, String descrKey, ChoiceKind choiceKind, Collection<String> choices) {
130 this(name, null, descrKey);
131 if (choiceKind == null || choices == null)
132 throw new NullPointerException();
133 this.choiceKind = choiceKind;
134 this.choices = choices;
135 }
136
137 @Override
99 public String toString() { 138 public String toString() {
100 return name.optionName; 139 return name.optionName;
101 } 140 }
102 141
103 /** Does this option take a (separate) operand?
104 */
105 public boolean hasArg() { 142 public boolean hasArg() {
106 return argsNameKey != null && !hasSuffix; 143 return argsNameKey != null && !hasSuffix;
107 } 144 }
108 145
109 /** Does argument string match option pattern? 146 public boolean matches(String option) {
110 * @param arg The command line argument string. 147 if (!hasSuffix)
111 */ 148 return option.equals(name.optionName);
112 public boolean matches(String arg) { 149
113 return hasSuffix ? arg.startsWith(name.optionName) : arg.equals(name.optionName); 150 if (!option.startsWith(name.optionName))
151 return false;
152
153 if (choices != null) {
154 String arg = option.substring(name.optionName.length());
155 if (choiceKind == ChoiceKind.ONEOF)
156 return choices.contains(arg);
157 else {
158 for (String a: arg.split(",+")) {
159 if (!choices.contains(a))
160 return false;
161 }
162 }
163 }
164
165 return true;
114 } 166 }
115 167
116 /** Print a line of documentation describing this option, if standard. 168 /** Print a line of documentation describing this option, if standard.
169 * @param out the stream to which to write the documentation
117 */ 170 */
118 void help(PrintWriter out) { 171 void help(PrintWriter out) {
119 String s = " " + helpSynopsis(); 172 String s = " " + helpSynopsis();
120 out.print(s); 173 out.print(s);
121 for (int j = s.length(); j < 29; j++) out.print(" "); 174 for (int j = Math.min(s.length(), 28); j < 29; j++) out.print(" ");
122 Log.printLines(out, Main.getLocalizedString(descrKey)); 175 Log.printLines(out, Main.getLocalizedString(descrKey));
123 } 176 }
177
124 String helpSynopsis() { 178 String helpSynopsis() {
125 return name + 179 StringBuilder sb = new StringBuilder();
126 (argsNameKey == null ? "" : 180 sb.append(name);
127 ((hasSuffix ? "" : " ") + 181 if (argsNameKey == null) {
128 Main.getLocalizedString(argsNameKey))); 182 if (choices != null) {
183 String sep = "{";
184 for (String c: choices) {
185 sb.append(sep);
186 sb.append(c);
187 sep = ",";
188 }
189 sb.append("}");
190 }
191 } else {
192 if (!hasSuffix)
193 sb.append(" ");
194 sb.append(Main.getLocalizedString(argsNameKey));
195 }
196
197 return sb.toString();
129 } 198 }
130 199
131 /** Print a line of documentation describing this option, if non-standard. 200 /** Print a line of documentation describing this option, if non-standard.
201 * @param out the stream to which to write the documentation
132 */ 202 */
133 void xhelp(PrintWriter out) {} 203 void xhelp(PrintWriter out) {}
134 204
135 /** Process the option (with arg). Return true if error detected. 205 /** Process the option (with arg). Return true if error detected.
136 */ 206 */
137 public boolean process(Options options, String option, String arg) { 207 public boolean process(Options options, String option, String arg) {
138 if (options != null) 208 if (options != null) {
209 if (choices != null) {
210 if (choiceKind == ChoiceKind.ONEOF) {
211 // some clients like to see just one of option+choice set
212 for (String c: choices)
213 options.remove(option + c);
214 String opt = option + arg;
215 options.put(opt, opt);
216 // some clients like to see option (without trailing ":")
217 // set to arg
218 String nm = option.substring(0, option.length() - 1);
219 options.put(nm, arg);
220 } else {
221 // set option+word for each word in arg
222 for (String a: arg.split(",+")) {
223 String opt = option + a;
224 options.put(opt, opt);
225 }
226 }
227 }
139 options.put(option, arg); 228 options.put(option, arg);
229 }
140 return false; 230 return false;
141 } 231 }
142 232
143 /** Process the option (without arg). Return true if error detected. 233 /** Process the option (without arg). Return true if error detected.
144 */ 234 */
161 super(name, argsNameKey, descrKey); 251 super(name, argsNameKey, descrKey);
162 } 252 }
163 XOption(OptionName name, String descrKey) { 253 XOption(OptionName name, String descrKey) {
164 this(name, null, descrKey); 254 this(name, null, descrKey);
165 } 255 }
256 XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) {
257 super(name, descrKey, kind, choices);
258 }
259 XOption(OptionName name, String descrKey, ChoiceKind kind, Collection<String> choices) {
260 super(name, descrKey, kind, choices);
261 }
262 @Override
166 void help(PrintWriter out) {} 263 void help(PrintWriter out) {}
264 @Override
167 void xhelp(PrintWriter out) { super.help(out); } 265 void xhelp(PrintWriter out) { super.help(out); }
266 @Override
168 public OptionKind getKind() { return OptionKind.EXTENDED; } 267 public OptionKind getKind() { return OptionKind.EXTENDED; }
169 }; 268 };
170 269
171 /** A hidden (implementor) option 270 /** A hidden (implementor) option
172 */ 271 */
175 super(name, null, null); 274 super(name, null, null);
176 } 275 }
177 HiddenOption(OptionName name, String argsNameKey) { 276 HiddenOption(OptionName name, String argsNameKey) {
178 super(name, argsNameKey, null); 277 super(name, argsNameKey, null);
179 } 278 }
279 @Override
180 void help(PrintWriter out) {} 280 void help(PrintWriter out) {}
281 @Override
181 void xhelp(PrintWriter out) {} 282 void xhelp(PrintWriter out) {}
283 @Override
182 public OptionKind getKind() { return OptionKind.HIDDEN; } 284 public OptionKind getKind() { return OptionKind.HIDDEN; }
183 }; 285 };
184 286
185 } 287 }

mercurial