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.26 2008/04/17 15:02:01 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("xdoc")) {
164 ConfigurationOptions.setOutputFormat("xdoc");
165 if (!this.givenCss) {
166
167 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
168 }
169 } else if (s.equals("xml")) {
170
171
172
173
174 ConfigurationOptions.setOutputFormat("xml");
175 } else if (s.equals("format")) {
176 if (args.isEmpty()) {
177 throw new ConfigurationException("Missing argument for -format");
178 }
179 final String format = popNextArg();
180 if ("xdoc".equals(format) && !givenCss) {
181
182 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
183 }
184 ConfigurationOptions.setOutputFormat(format);
185 } else if (s.equals("no-developer")) {
186 if (args.isEmpty()) {
187 throw new ConfigurationException("Missing argument for -no-developer");
188 }
189 ConfigurationOptions.addNonDeveloperLogin(popNextArg());
190 } else if (!doChildrenSwitch(s)) {
191 throw new ConfigurationException("Unrecognized option -" + s);
192 }
193 }
194
195 /**
196 * Give the argument to children classes that may have a way to recognise it.
197 * @param s
198 * @return
199 */
200 protected boolean doChildrenSwitch(final String s) throws ConfigurationException {
201 return false;
202 }
203
204 private void parseArgument(final String arg) throws ConfigurationException {
205 argCount++;
206 switch (argCount) {
207 case 1:
208 ConfigurationOptions.setLogFileName(arg);
209 break;
210 case 2:
211 ConfigurationOptions.setCheckedOutDirectory(arg);
212 break;
213 default:
214 throw new ConfigurationException("Too many arguments");
215 }
216 }
217
218 protected void checkForRequiredArgs() throws ConfigurationException {
219 switch (argCount) {
220 case 0:
221 throw new ConfigurationException("Not enough arguments - <logfile> is missing");
222 case 1:
223 throw new ConfigurationException("Not enough arguments - <directory> is missing");
224 }
225 }
226
227 protected int getArgCount() {
228 return argCount;
229 }
230
231 protected boolean isArgsEmpty() {
232 return args.isEmpty();
233 }
234 }