1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.output;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * Takes a command line, like given to the {@link net.sf.statcvs.Main#main} method,
30 * and turns it into a {@link ConfigurationOptions} object.
31 *
32 * @author Richard Cyganiak <rcyg@gmx.de>
33 * @version $Id: CommandLineParser.java,v 1.27 2008/06/25 20:46:43 benoitx Exp $
34 */
35 public class CommandLineParser {
36
37 private final String[] argsArray;
38 private final List args = new ArrayList();
39 private int argCount = 0;
40 private boolean givenCss = false;
41
42 /**
43 * Constructor for CommandLineParser
44 *
45 * @param args the command line parameters
46 */
47 public CommandLineParser(final String[] args) {
48 argsArray = args;
49 }
50
51 /**
52 * Parses the command line and sets the options (as static
53 * fields in {@link ConfigurationOptions}).
54 *
55 * @throws ConfigurationException if errors are present on the command line
56 */
57 public void parse() throws ConfigurationException {
58 for (int i = 0; i < argsArray.length; i++) {
59 args.add(argsArray[i]);
60 }
61 while (!args.isEmpty()) {
62 final String currentArg = popNextArg();
63 if (currentArg.startsWith("-")) {
64 parseSwitch(currentArg.substring(1));
65 } else {
66 parseArgument(currentArg);
67 }
68 }
69 checkForRequiredArgs();
70 }
71
72 protected String popNextArg() {
73 return (String) args.remove(0);
74 }
75
76 protected void parseSwitch(final String switchName) throws ConfigurationException {
77 final String s = switchName.toLowerCase();
78 if (s.equals("css")) {
79 if (args.isEmpty()) {
80 throw new ConfigurationException("Missing argument for -css");
81 }
82 ConfigurationOptions.setCssFile(popNextArg());
83 givenCss = true;
84 } else if (s.equals("output-dir")) {
85 if (args.isEmpty()) {
86 throw new ConfigurationException("Missing argument for -output-dir");
87 }
88 ConfigurationOptions.setOutputDir(popNextArg());
89 } else if (s.equals("verbose")) {
90 ConfigurationOptions.setVerboseLogging();
91 } else if (s.equals("debug")) {
92 ConfigurationOptions.setDebugLogging();
93 } else if (s.equals("notes")) {
94 if (args.isEmpty()) {
95 throw new ConfigurationException("Missing argument for -notes");
96 }
97 ConfigurationOptions.setNotesFile(popNextArg());
98 } else if (s.equals("viewcvs")) {
99 if (args.isEmpty()) {
100 throw new ConfigurationException("Missing argument for -viewcvs");
101 }
102 ConfigurationOptions.setViewCvsURL(popNextArg());
103 } else if (s.equals("viewvc")) {
104 if (args.isEmpty()) {
105 throw new ConfigurationException("Missing argument for -viewvc");
106 }
107 ConfigurationOptions.setViewVcURL(popNextArg());
108 } else if (s.equals("trac")) {
109 if (args.isEmpty()) {
110 throw new ConfigurationException("Missing argument for -trac");
111 }
112 ConfigurationOptions.setViewTracURL(popNextArg());
113 } else if (s.equals("cvsweb")) {
114 if (args.isEmpty()) {
115 throw new ConfigurationException("Missing argument for -cvsweb");
116 }
117 ConfigurationOptions.setCvswebURL(popNextArg());
118 } else if (s.equals("chora")) {
119 if (args.isEmpty()) {
120 throw new ConfigurationException("Missing argument for -chora");
121 }
122 ConfigurationOptions.setChoraURL(popNextArg());
123 } else if (s.equals("jcvsweb")) {
124 if (args.isEmpty()) {
125 throw new ConfigurationException("Missing argument for -jcvsweb");
126 }
127 ConfigurationOptions.setJCVSWebURL(popNextArg());
128 } else if (s.equals("include")) {
129 if (args.isEmpty()) {
130 throw new ConfigurationException("Missing argument for -include");
131 }
132 ConfigurationOptions.setIncludePattern(popNextArg());
133 } else if (s.equals("exclude")) {
134 if (args.isEmpty()) {
135 throw new ConfigurationException("Missing argument for -exclude");
136 }
137 ConfigurationOptions.setExcludePattern(popNextArg());
138 } else if (s.equals("title")) {
139 if (args.isEmpty()) {
140 throw new ConfigurationException("Missing argument for -title");
141 }
142 ConfigurationOptions.setProjectName(popNextArg());
143 } else if (s.equals("tags")) {
144 if (args.isEmpty()) {
145 throw new ConfigurationException("Missing argument for -tags");
146 }
147 ConfigurationOptions.setSymbolicNamesPattern(popNextArg());
148 } else if (s.equals("bugzilla")) {
149 if (args.isEmpty()) {
150 throw new ConfigurationException("Missing argument for -bugzilla");
151 }
152 ConfigurationOptions.setBugzillaUrl(popNextArg());
153 } else if (s.equals("mantis")) {
154 if (args.isEmpty()) {
155 throw new ConfigurationException("Missing argument for -mantis");
156 }
157 ConfigurationOptions.setMantisUrl(popNextArg());
158 } else if (s.equals("config-file")) {
159 if (args.isEmpty()) {
160 throw new ConfigurationException("Missing argument for -config-file");
161 }
162 ConfigurationOptions.setConfigFile(popNextArg());
163 } else if (s.equals("charset")) {
164 if (isArgsEmpty()) {
165 throw new ConfigurationException("Missing argument for -charset");
166 }
167 ConfigurationOptions.setCharSet(popNextArg());
168 } else if (s.equals("xdoc")) {
169 ConfigurationOptions.setOutputFormat("xdoc");
170 if (!this.givenCss) {
171
172 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
173 }
174 } else if (s.equals("xml")) {
175
176
177
178
179 ConfigurationOptions.setOutputFormat("xml");
180 } else if (s.equals("format")) {
181 if (args.isEmpty()) {
182 throw new ConfigurationException("Missing argument for -format");
183 }
184 final String format = popNextArg();
185 if ("xdoc".equals(format) && !givenCss) {
186
187 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
188 }
189 ConfigurationOptions.setOutputFormat(format);
190 } else if (s.equals("no-developer")) {
191 if (args.isEmpty()) {
192 throw new ConfigurationException("Missing argument for -no-developer");
193 }
194 ConfigurationOptions.addNonDeveloperLogin(popNextArg());
195 } else if (!doChildrenSwitch(s)) {
196 throw new ConfigurationException("Unrecognized option -" + s);
197 }
198 }
199
200 /**
201 * Give the argument to children classes that may have a way to recognise it.
202 * @param s
203 * @return
204 */
205 protected boolean doChildrenSwitch(final String s) throws ConfigurationException {
206 return false;
207 }
208
209 private void parseArgument(final String arg) throws ConfigurationException {
210 argCount++;
211 switch (argCount) {
212 case 1:
213 ConfigurationOptions.setLogFileName(arg);
214 break;
215 case 2:
216 ConfigurationOptions.setCheckedOutDirectory(arg);
217 break;
218 default:
219 throw new ConfigurationException("Too many arguments");
220 }
221 }
222
223 protected void checkForRequiredArgs() throws ConfigurationException {
224 switch (argCount) {
225 case 0:
226 throw new ConfigurationException("Not enough arguments - <logfile> is missing");
227 case 1:
228 throw new ConfigurationException("Not enough arguments - <directory> is missing");
229 }
230 }
231
232 protected int getArgCount() {
233 return argCount;
234 }
235
236 protected boolean isArgsEmpty() {
237 return args.isEmpty();
238 }
239 }