1 | /* |
2 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Sun designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Sun in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
23 | * have any questions. |
24 | */ |
25 | |
26 | package com.sun.tools.javac.main; |
27 | |
28 | import com.sun.tools.javac.util.Log; |
29 | import com.sun.tools.javac.util.Options; |
30 | import java.io.PrintWriter; |
31 | |
32 | /** |
33 | * TODO: describe com.sun.tools.javac.main.JavacOption |
34 | * |
35 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
36 | * If you write code that depends on this, you do so at your own |
37 | * risk. This code and its internal interfaces are subject to change |
38 | * or deletion without notice.</b></p> |
39 | */ |
40 | public interface JavacOption { |
41 | |
42 | OptionKind getKind(); |
43 | |
44 | /** Does this option take a (separate) operand? */ |
45 | boolean hasArg(); |
46 | |
47 | /** Does argument string match option pattern? |
48 | * @param arg The command line argument string. |
49 | */ |
50 | boolean matches(String arg); |
51 | |
52 | /** Process the option (with arg). Return true if error detected. |
53 | */ |
54 | boolean process(Options options, String option, String arg); |
55 | |
56 | /** Process the option (without arg). Return true if error detected. |
57 | */ |
58 | boolean process(Options options, String option); |
59 | |
60 | OptionName getName(); |
61 | |
62 | enum OptionKind { |
63 | NORMAL, |
64 | EXTENDED, |
65 | HIDDEN, |
66 | } |
67 | |
68 | /** This class represents an option recognized by the main program |
69 | */ |
70 | static class Option implements JavacOption { |
71 | |
72 | /** Option string. |
73 | */ |
74 | OptionName name; |
75 | |
76 | /** Documentation key for arguments. |
77 | */ |
78 | String argsNameKey; |
79 | |
80 | /** Documentation key for description. |
81 | */ |
82 | String descrKey; |
83 | |
84 | /** Suffix option (-foo=bar or -foo:bar) |
85 | */ |
86 | boolean hasSuffix; |
87 | |
88 | Option(OptionName name, String argsNameKey, String descrKey) { |
89 | this.name = name; |
90 | this.argsNameKey = argsNameKey; |
91 | this.descrKey = descrKey; |
92 | char lastChar = name.optionName.charAt(name.optionName.length()-1); |
93 | hasSuffix = lastChar == ':' || lastChar == '='; |
94 | } |
95 | Option(OptionName name, String descrKey) { |
96 | this(name, null, descrKey); |
97 | } |
98 | |
99 | public String toString() { |
100 | return name.optionName; |
101 | } |
102 | |
103 | /** Does this option take a (separate) operand? |
104 | */ |
105 | public boolean hasArg() { |
106 | return argsNameKey != null && !hasSuffix; |
107 | } |
108 | |
109 | /** Does argument string match option pattern? |
110 | * @param arg The command line argument string. |
111 | */ |
112 | public boolean matches(String arg) { |
113 | return hasSuffix ? arg.startsWith(name.optionName) : arg.equals(name.optionName); |
114 | } |
115 | |
116 | /** Print a line of documentation describing this option, if standard. |
117 | */ |
118 | void help(PrintWriter out) { |
119 | String s = " " + helpSynopsis(); |
120 | out.print(s); |
121 | for (int j = s.length(); j < 29; j++) out.print(" "); |
122 | Log.printLines(out, Main.getLocalizedString(descrKey)); |
123 | } |
124 | String helpSynopsis() { |
125 | return name + |
126 | (argsNameKey == null ? "" : |
127 | ((hasSuffix ? "" : " ") + |
128 | Main.getLocalizedString(argsNameKey))); |
129 | } |
130 | |
131 | /** Print a line of documentation describing this option, if non-standard. |
132 | */ |
133 | void xhelp(PrintWriter out) {} |
134 | |
135 | /** Process the option (with arg). Return true if error detected. |
136 | */ |
137 | public boolean process(Options options, String option, String arg) { |
138 | if (options != null) |
139 | options.put(option, arg); |
140 | return false; |
141 | } |
142 | |
143 | /** Process the option (without arg). Return true if error detected. |
144 | */ |
145 | public boolean process(Options options, String option) { |
146 | if (hasSuffix) |
147 | return process(options, name.optionName, option.substring(name.optionName.length())); |
148 | else |
149 | return process(options, option, option); |
150 | } |
151 | |
152 | public OptionKind getKind() { return OptionKind.NORMAL; } |
153 | |
154 | public OptionName getName() { return name; } |
155 | }; |
156 | |
157 | /** A nonstandard or extended (-X) option |
158 | */ |
159 | static class XOption extends Option { |
160 | XOption(OptionName name, String argsNameKey, String descrKey) { |
161 | super(name, argsNameKey, descrKey); |
162 | } |
163 | XOption(OptionName name, String descrKey) { |
164 | this(name, null, descrKey); |
165 | } |
166 | void help(PrintWriter out) {} |
167 | void xhelp(PrintWriter out) { super.help(out); } |
168 | public OptionKind getKind() { return OptionKind.EXTENDED; } |
169 | }; |
170 | |
171 | /** A hidden (implementor) option |
172 | */ |
173 | static class HiddenOption extends Option { |
174 | HiddenOption(OptionName name) { |
175 | super(name, null, null); |
176 | } |
177 | HiddenOption(OptionName name, String argsNameKey) { |
178 | super(name, argsNameKey, null); |
179 | } |
180 | void help(PrintWriter out) {} |
181 | void xhelp(PrintWriter out) {} |
182 | public OptionKind getKind() { return OptionKind.HIDDEN; } |
183 | }; |
184 | |
185 | } |