Skip to content

Commit 298804b

Browse files
committed
Applying Henning's patch to make the Parser more extendable - CLI-142
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@654429 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f40737 commit 298804b

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

src/java/org/apache/commons/cli/Parser.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,28 @@
3333
public abstract class Parser implements CommandLineParser {
3434

3535
/** commandline instance */
36-
private CommandLine cmd;
36+
protected CommandLine cmd;
3737

3838
/** current Options */
3939
private Options options;
4040

4141
/** list of required options strings */
4242
private List requiredOptions;
4343

44+
protected void setOptions(final Options options) {
45+
this.options = options;
46+
this.requiredOptions = options.getRequiredOptions();
47+
}
48+
49+
protected Options getOptions() {
50+
return options;
51+
}
52+
53+
protected List getRequiredOptions() {
54+
return requiredOptions;
55+
}
56+
57+
4458
/**
4559
* <p>Subclasses must implement this method to reduce
4660
* the <code>arguments</code> that have been passed to the parse
@@ -131,16 +145,15 @@ public CommandLine parse(Options options, String[] arguments,
131145
Properties properties, boolean stopAtNonOption)
132146
throws ParseException
133147
{
134-
// initialise members
135-
this.options = options;
136-
137148
// clear out the data in options in case it's been used before (CLI-71)
138149
for (Iterator it = options.helpOptions().iterator(); it.hasNext();) {
139150
Option opt = (Option) it.next();
140151
opt.clearValues();
141152
}
142153

143-
requiredOptions = options.getRequiredOptions();
154+
// initialise members
155+
setOptions(options);
156+
144157
cmd = new CommandLine();
145158

146159
boolean eatTheRest = false;
@@ -150,7 +163,7 @@ public CommandLine parse(Options options, String[] arguments,
150163
arguments = new String[0];
151164
}
152165

153-
List tokenList = Arrays.asList(flatten(this.options,
166+
List tokenList = Arrays.asList(flatten(getOptions(),
154167
arguments,
155168
stopAtNonOption));
156169

@@ -183,7 +196,7 @@ else if ("-".equals(t))
183196
// the value is an option
184197
else if (t.startsWith("-"))
185198
{
186-
if (stopAtNonOption && !options.hasOption(t))
199+
if (stopAtNonOption && !getOptions().hasOption(t))
187200
{
188201
eatTheRest = true;
189202
cmd.addArg(t);
@@ -233,7 +246,7 @@ else if (t.startsWith("-"))
233246
*
234247
* @param properties The value properties to be processed.
235248
*/
236-
private void processProperties(Properties properties)
249+
protected void processProperties(Properties properties)
237250
{
238251
if (properties == null)
239252
{
@@ -246,7 +259,7 @@ private void processProperties(Properties properties)
246259

247260
if (!cmd.hasOption(option))
248261
{
249-
Option opt = options.getOption(option);
262+
Option opt = getOptions().getOption(option);
250263

251264
// get the value from the properties instance
252265
String value = properties.getProperty(option);
@@ -287,16 +300,16 @@ else if (!("yes".equalsIgnoreCase(value)
287300
* @throws MissingOptionException if any of the required Options
288301
* are not present.
289302
*/
290-
private void checkRequiredOptions()
303+
protected void checkRequiredOptions()
291304
throws MissingOptionException
292305
{
293306
// if there are required options that have not been
294307
// processsed
295-
if (requiredOptions.size() > 0)
308+
if (getRequiredOptions().size() > 0)
296309
{
297-
Iterator iter = requiredOptions.iterator();
310+
Iterator iter = getRequiredOptions().iterator();
298311
StringBuffer buff = new StringBuffer("Missing required option");
299-
buff.append(requiredOptions.size() == 1 ? "" : "s");
312+
buff.append(getRequiredOptions().size() == 1 ? "" : "s");
300313
buff.append(": ");
301314

302315

@@ -331,7 +344,7 @@ public void processArgs(Option opt, ListIterator iter)
331344
String str = (String) iter.next();
332345

333346
// found an Option, not an argument
334-
if (options.hasOption(str) && str.startsWith("-"))
347+
if (getOptions().hasOption(str) && str.startsWith("-"))
335348
{
336349
iter.previous();
337350
break;
@@ -368,10 +381,10 @@ public void processArgs(Option opt, ListIterator iter)
368381
* @throws ParseException if <code>arg</code> does not
369382
* represent an Option
370383
*/
371-
private void processOption(String arg, ListIterator iter)
384+
protected void processOption(String arg, ListIterator iter)
372385
throws ParseException
373386
{
374-
boolean hasOption = options.hasOption(arg);
387+
boolean hasOption = getOptions().hasOption(arg);
375388

376389
// if there is no option throw an UnrecognisedOptionException
377390
if (!hasOption)
@@ -381,24 +394,24 @@ private void processOption(String arg, ListIterator iter)
381394
}
382395

383396
// get the option represented by arg
384-
final Option opt = options.getOption(arg);
397+
final Option opt = getOptions().getOption(arg);
385398

386399
// if the option is a required option remove the option from
387400
// the requiredOptions list
388401
if (opt.isRequired())
389402
{
390-
requiredOptions.remove(opt.getKey());
403+
getRequiredOptions().remove(opt.getKey());
391404
}
392405

393406
// if the option is in an OptionGroup make that option the selected
394407
// option of the group
395-
if (options.getOptionGroup(opt) != null)
408+
if (getOptions().getOptionGroup(opt) != null)
396409
{
397-
OptionGroup group = options.getOptionGroup(opt);
410+
OptionGroup group = getOptions().getOptionGroup(opt);
398411

399412
if (group.isRequired())
400413
{
401-
requiredOptions.remove(group);
414+
getRequiredOptions().remove(group);
402415
}
403416

404417
group.setSelected(opt);

0 commit comments

Comments
 (0)