KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > commandline > CommandLine


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.commandline;
34
35 import java.util.*;
36
37 /**
38  * Command-line parser.
39  */

40 public class CommandLine implements Visitable {
41     private static final boolean DEFAULT_STRICT = true;
42     private static final ParameterStrategy DEFAULT_PARAMETER_STRATEGY = new AnyParameterStrategy();
43
44     private boolean strict;
45     private ParameterStrategy parameterStrategy;
46
47     private List parameters = new LinkedList();
48     private Map map = new TreeMap();
49
50     public CommandLine() {
51         this(DEFAULT_STRICT, DEFAULT_PARAMETER_STRATEGY);
52     }
53
54     public CommandLine(boolean strict) {
55         this(strict, DEFAULT_PARAMETER_STRATEGY);
56     }
57
58     public CommandLine(ParameterStrategy parameterStrategy) {
59         this(DEFAULT_STRICT, parameterStrategy);
60     }
61
62     public CommandLine(boolean strict, ParameterStrategy parameterStrategy) {
63         setStrict(strict);
64         setParameterStrategy(parameterStrategy);
65     }
66
67     public boolean isStrict() {
68         return strict;
69     }
70
71     public void setStrict(boolean strict) {
72         this.strict = strict;
73     }
74
75     public ParameterStrategy getParameterStrategy() {
76         return parameterStrategy;
77     }
78
79     public void setParameterStrategy(ParameterStrategy parameterStrategy) {
80         this.parameterStrategy = parameterStrategy;
81     }
82
83     public void addSwitch(String JavaDoc name, CommandLineSwitch cls) {
84         map.put(name, cls);
85     }
86
87     public void addToggleSwitch(String JavaDoc name) {
88         addSwitch(name, new ToggleSwitch());
89     }
90
91     public void addToggleSwitch(String JavaDoc name, boolean defaultValue) {
92         addSwitch(name, new ToggleSwitch(defaultValue));
93     }
94
95     public void addSingleValueSwitch(String JavaDoc name) {
96         addSwitch(name, new SingleValueSwitch());
97     }
98
99     public void addSingleValueSwitch(String JavaDoc name, boolean mandatory) {
100         addSwitch(name, new SingleValueSwitch(mandatory));
101     }
102
103     public void addSingleValueSwitch(String JavaDoc name, String JavaDoc defaultValue) {
104         addSwitch(name, new SingleValueSwitch(defaultValue));
105     }
106
107     public void addSingleValueSwitch(String JavaDoc name, String JavaDoc defaultValue, boolean mandatory) {
108         addSwitch(name, new SingleValueSwitch(defaultValue, mandatory));
109     }
110
111     public void addOptionalValueSwitch(String JavaDoc name) {
112         addSwitch(name, new OptionalValueSwitch());
113     }
114
115     public void addOptionalValueSwitch(String JavaDoc name, boolean mandatory) {
116         addSwitch(name, new OptionalValueSwitch(mandatory));
117     }
118
119     public void addOptionalValueSwitch(String JavaDoc name, String JavaDoc defaultValue) {
120         addSwitch(name, new OptionalValueSwitch(defaultValue));
121     }
122
123     public void addOptionalValueSwitch(String JavaDoc name, String JavaDoc defaultValue, boolean mandatory) {
124         addSwitch(name, new OptionalValueSwitch(defaultValue, mandatory));
125     }
126
127     public void addMultipleValuesSwitch(String JavaDoc name) {
128         map.put(name, new MultipleValuesSwitch());
129     }
130
131     public void addMultipleValuesSwitch(String JavaDoc name, boolean mandatory) {
132         map.put(name, new MultipleValuesSwitch(mandatory));
133     }
134
135     public void addMultipleValuesSwitch(String JavaDoc name, String JavaDoc defaultValue) {
136         map.put(name, new MultipleValuesSwitch(defaultValue));
137     }
138
139     public void addMultipleValuesSwitch(String JavaDoc name, String JavaDoc defaultValue, boolean mandatory) {
140         map.put(name, new MultipleValuesSwitch(defaultValue, mandatory));
141     }
142
143     public CommandLineSwitch getSwitch(String JavaDoc name) {
144         return (CommandLineSwitch) map.get(name);
145     }
146
147     public boolean getToggleSwitch(String JavaDoc name) {
148         boolean result = false;
149
150         CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
151         if (cls != null) {
152             result = ((Boolean JavaDoc) cls.getValue()).booleanValue();
153         }
154
155         return result;
156     }
157
158     public String JavaDoc getSingleSwitch(String JavaDoc name) {
159         return getStringSwitch(name);
160     }
161
162     public String JavaDoc getOptionalSwitch(String JavaDoc name) {
163         return getStringSwitch(name);
164     }
165
166     public List getMultipleSwitch(String JavaDoc name) {
167         return getListSwitch(name);
168     }
169
170     private String JavaDoc getStringSwitch(String JavaDoc name) {
171         String JavaDoc result = null;
172
173         CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
174         if (cls != null) {
175             result = cls.getValue().toString();
176         }
177
178         return result;
179     }
180
181     private List getListSwitch(String JavaDoc name) {
182         List result = null;
183
184         CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
185         if (cls != null && cls.getValue() instanceof List) {
186             result = (List) cls.getValue();
187         }
188
189         return result;
190     }
191
192     public boolean isPresent(String JavaDoc name) {
193         boolean result = false;
194
195         CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
196         if (cls != null) {
197             result = cls.isPresent();
198         }
199
200         return result;
201     }
202
203     public Set getKnownSwitches() {
204         return map.keySet();
205     }
206
207     public Set getPresentSwitches() {
208         Set result = new TreeSet();
209
210         Iterator i = getKnownSwitches().iterator();
211         while (i.hasNext()) {
212             String JavaDoc name = (String JavaDoc) i.next();
213             CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
214
215             if (cls.isPresent()) {
216                 result.add(name);
217             }
218         }
219
220         return result;
221     }
222
223     public List getParameters() {
224         return parameters;
225     }
226
227     public void parse(String JavaDoc args[]) throws CommandLineException {
228         parameters = new LinkedList();
229
230         int i=0;
231         while (i < args.length) {
232             if (args[i].startsWith("-")) {
233                 String JavaDoc name = args[i].substring(1);
234                 String JavaDoc value = null;
235
236                 if (i+1 < args.length && !map.containsKey(args[i+1].substring(1))) {
237                     value = args[i+1];
238                 }
239
240                 CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
241
242                 if (cls == null) {
243                     if (isStrict()) {
244                         throw new CommandLineException("Unknown switch \"" + args[i] + "\"");
245                     } else {
246                         cls = new OptionalValueSwitch();
247                         map.put(name, cls);
248                     }
249                 }
250
251                 i += cls.parse(name, value);
252             } else if (parameterStrategy.accept(args[i])) {
253                 parameters.add(args[i]);
254                 i++;
255             } else {
256                 throw new CommandLineException("Invalid parameter \"" + args[i] + "\"");
257             }
258         }
259
260         // Checking that all manadatory switches are present
261
Iterator j = map.keySet().iterator();
262         while (j.hasNext()) {
263             String JavaDoc name = (String JavaDoc) j.next();
264             CommandLineSwitch cls = (CommandLineSwitch) map.get(name);
265
266             if (cls.isMandatory() && !cls.isPresent()) {
267                 throw new CommandLineException("Missing mandatory switch \"" + name + "\"");
268             }
269         }
270
271         // Checking that all mandatory parameters are present
272
if (!parameterStrategy.isSatisfied()) {
273             throw new CommandLineException("Missing mandatory parameters");
274         }
275     }
276
277     public void accept(Visitor visitor) {
278         visitor.visitCommandLine(this);
279     }
280 }
281
Popular Tags