Skip to content

Commit 7fb448f

Browse files
author
Robert James Oxspring
committed
Implemented definition time defaults:
Option - added defaults(WriteableCommandLine) WriteableCommandLine - added setDefaultValues(Option,List) WriteableCommandLine - added setDefaultSwitch(Option,Boolean) Argument - add defaults(WriteableCommandLine,Option) CommandLineDefaultsTest - test the defaults in combination Parser - now applys the defaults before processing Implemented new methods git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130090 13f79535-47bb-0310-9956-ffa450edef68
1 parent fd3362f commit 7fb448f

13 files changed

Lines changed: 392 additions & 48 deletions

File tree

src/java/org/apache/commons/cli2/Argument.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ void processValues(
4949
final ListIterator args,
5050
final Option option)
5151
throws OptionException;
52+
53+
/**
54+
* Adds defaults to a CommandLine.
55+
*
56+
* @param commandLine
57+
* The CommandLine object to store defaults in.
58+
* @param option
59+
* The Option to store the defaults against.
60+
*/
61+
void defaultValues(final WriteableCommandLine commandLine, final Option option);
5262

5363
/**
5464
* Performs any necessary validation on the values added to the

src/java/org/apache/commons/cli2/Option.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ void process(
4747
final WriteableCommandLine commandLine,
4848
final ListIterator args)
4949
throws OptionException;
50+
51+
/**
52+
* Adds defaults to a CommandLine.
53+
*
54+
* Any defaults for this option are applied as well as the defaults for
55+
* any contained options
56+
*
57+
* @param commandLine
58+
* The CommandLine object to store defaults in
59+
*/
60+
void defaults(final WriteableCommandLine commandLine);
5061

5162
/**
5263
* Indicates whether this Option will be able to process the particular

src/java/org/apache/commons/cli2/WriteableCommandLine.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.commons.cli2;
1717

18+
import java.util.List;
19+
1820
/**
1921
* A CommandLine that detected values and options can be written to.
2022
*/
@@ -32,6 +34,13 @@ public interface WriteableCommandLine extends CommandLine {
3234
* @param value the value to add
3335
*/
3436
void addValue(final Option option, final Object value);
37+
38+
/**
39+
* Sets the default values for an Option in the CommandLine
40+
* @param option the Option to add to
41+
* @param defaultValues the defaults for the option
42+
*/
43+
void setDefaultValues(final Option option, final List defaultValues);
3544

3645
/**
3746
* Adds a switch value to an Option in the CommandLine.
@@ -41,6 +50,13 @@ public interface WriteableCommandLine extends CommandLine {
4150
*/
4251
void addSwitch(final Option option, final boolean value) throws IllegalStateException;
4352

53+
/**
54+
* Sets the default state for a Switch in the CommandLine.
55+
* @param option the Option to add to
56+
* @param defaultSwitch the defaults state for ths switch
57+
*/
58+
void setDefaultSwitch(final Option option, final Boolean defaultSwitch);
59+
4460
/**
4561
* Adds a property value to a name in the CommandLine.
4662
* Replaces any existing value for the property.

src/java/org/apache/commons/cli2/builder/SwitchBuilder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class SwitchBuilder {
3737
private Argument argument;
3838
private Group children;
3939
private int id;
40+
private Boolean switchDefault;
4041

4142
/**
4243
* Creates a new SwitchBuilder using defaults.
@@ -86,7 +87,8 @@ public Switch create() {
8687
required,
8788
argument,
8889
children,
89-
id);
90+
id,
91+
switchDefault);
9092

9193
reset();
9294

@@ -104,6 +106,7 @@ public void reset() {
104106
argument = null;
105107
children = null;
106108
id = 0;
109+
switchDefault = null;
107110
}
108111

109112
/**
@@ -173,4 +176,15 @@ public final SwitchBuilder withId(final int newId) {
173176
this.id = newId;
174177
return this;
175178
}
179+
180+
/**
181+
* Sets the default state for this switch
182+
*
183+
* @param switchDefault the default state
184+
* @return this SwitchBuilder
185+
*/
186+
public final SwitchBuilder withSwitchDefault(final Boolean switchDefault) {
187+
this.switchDefault = switchDefault;
188+
return this;
189+
}
176190
}

src/java/org/apache/commons/cli2/commandline/Parser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ public class Parser {
5656
public CommandLine parse(final String[] arguments) throws OptionException {
5757

5858
final List argumentList = new LinkedList(Arrays.asList(arguments));
59-
final ListIterator iterator = argumentList.listIterator();
6059
final WriteableCommandLine commandLine =
6160
new WriteableCommandLineImpl(group, new ArrayList());
61+
62+
// pick up any defaults from the model
63+
group.defaults(commandLine);
6264

65+
final ListIterator iterator = argumentList.listIterator();
6366
while (group.canProcess(iterator)) {
6467
group.process(commandLine, iterator);
6568
}

src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020
import java.util.HashMap;
21-
import java.util.HashSet;
2221
import java.util.Iterator;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.Properties;
2625
import java.util.Set;
2726

2827
import org.apache.commons.cli2.Argument;
29-
import org.apache.commons.cli2.CommandLine;
3028
import org.apache.commons.cli2.Option;
3129
import org.apache.commons.cli2.WriteableCommandLine;
3230

@@ -41,12 +39,11 @@ public class WriteableCommandLineImpl extends CommandLineImpl implements Writeab
4139
private final Map nameToOption = new HashMap();
4240
private final Map values = new HashMap();
4341
private final Map switches = new HashMap();
44-
private final Map defaults = new HashMap();
42+
private final Map defaultValues = new HashMap();
43+
private final Map defaultSwitches = new HashMap();
4544
private final List normalised;
4645
private final Set prefixes;
4746

48-
private CommandLine defaultCommandLine = null;
49-
5047
/**
5148
* Creates a new WriteableCommandLineImpl rooted on the specified Option, to
5249
* hold the parsed arguments.
@@ -91,38 +88,26 @@ public void addSwitch(final Option option, final boolean value) {
9188

9289
public boolean hasOption(final Option option) {
9390
final boolean present = options.contains(option);
94-
if (!present && defaultCommandLine != null) {
95-
return defaultCommandLine.hasOption(option);
96-
}
97-
else {
98-
return present;
99-
}
91+
return present;
10092
}
10193

10294
public Option getOption(final String trigger) {
10395
return (Option)nameToOption.get(trigger);
10496
}
10597

106-
//TODO Document the order of values and defaults
10798
public List getValues(final Option option, final List defaultValues) {
10899

109100
// First grab the command line values
110101
List valueList = (List)values.get(option);
111102

112-
// Secondly try alternate CommandLines
113-
if ((valueList == null || valueList.isEmpty())
114-
&& defaultCommandLine != null) {
115-
valueList = defaultCommandLine.getValues(option, null);
116-
}
117-
118-
// Thirdly try the defaults supplied to the method
103+
// Secondly try the defaults supplied to the method
119104
if (valueList == null || valueList.isEmpty()) {
120105
valueList = defaultValues;
121106
}
122107

123-
// Fourthly try the option's default values
108+
// Thirdly try the option's default values
124109
if (valueList == null || valueList.isEmpty()) {
125-
valueList = (List)this.defaults.get(option);
110+
valueList = (List)this.defaultValues.get(option);
126111
}
127112

128113
// Finally use an empty list
@@ -137,18 +122,15 @@ public Boolean getSwitch(final Option option, final Boolean defaultValue) {
137122
// First grab the command line values
138123
Boolean bool = (Boolean)switches.get(option);
139124

140-
// Secondly try alternate CommandLines
141-
if (bool == null && defaultCommandLine != null) {
142-
bool = defaultCommandLine.getSwitch(option);
143-
}
144-
145-
// Thirdly try the defaults supplied to the method
125+
// Secondly try the defaults supplied to the method
146126
if (bool == null) {
147127
bool = defaultValue;
148128
}
149129

150-
// Fourthly try the option's default values
151-
//????
130+
// Thirdly try the option's default values
131+
if (bool == null) {
132+
bool = (Boolean)this.defaultSwitches.get(option);
133+
}
152134

153135
return bool;
154136
}
@@ -162,15 +144,7 @@ public String getProperty(final String property, final String defaultValue) {
162144
}
163145

164146
public Set getProperties() {
165-
if (defaultCommandLine == null) {
166-
return Collections.unmodifiableSet(properties.keySet());
167-
}
168-
else {
169-
final Set props = new HashSet();
170-
props.addAll(properties.keySet());
171-
props.addAll(defaultCommandLine.getProperties());
172-
return Collections.unmodifiableSet(props);
173-
}
147+
return Collections.unmodifiableSet(properties.keySet());
174148
}
175149

176150
public boolean looksLikeOption(final String trigger) {
@@ -212,4 +186,22 @@ public List getOptions() {
212186
public Set getOptionTriggers() {
213187
return Collections.unmodifiableSet(nameToOption.keySet());
214188
}
189+
190+
public void setDefaultValues(final Option option, final List defaults) {
191+
if (defaults==null) {
192+
defaultValues.remove(option);
193+
}
194+
else {
195+
defaultValues.put(option, defaults);
196+
}
197+
}
198+
199+
public void setDefaultSwitch(final Option option, final Boolean defaultSwitch) {
200+
if (defaultSwitch==null) {
201+
defaultSwitches.remove(defaultSwitch);
202+
}
203+
else {
204+
defaultSwitches.put(option, defaultSwitch);
205+
}
206+
}
215207
}

src/java/org/apache/commons/cli2/option/ArgumentImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public ArgumentImpl(
103103
final char subsequentSeparator,
104104
final Validator validator,
105105
final String consumeRemaining,
106-
final List defaultValues,
106+
final List valueDefaults,
107107
final int id) {
108108

109109
super(id,false);
@@ -117,20 +117,20 @@ public ArgumentImpl(
117117
this.subsequentSplit = subsequentSeparator != NUL;
118118
this.validator = validator;
119119
this.consumeRemaining = consumeRemaining;
120-
this.defaultValues = defaultValues;
120+
this.defaultValues = valueDefaults;
121121

122122
if (minimum > maximum) {
123123
throw new IllegalArgumentException(
124124
resources.getMessage("cli.error.minimum.exceeds.maximum"));
125125
}
126126

127-
if (defaultValues != null) {
128-
if (defaultValues.size() < minimum) {
127+
if (valueDefaults != null) {
128+
if (valueDefaults.size() < minimum) {
129129
throw new IllegalArgumentException(
130130
resources.getMessage("cli.error.too.few.defaults"));
131131
}
132132

133-
if (defaultValues.size() > maximum) {
133+
if (valueDefaults.size() > maximum) {
134134
throw new IllegalArgumentException(
135135
resources.getMessage("cli.error.too.many.defaults"));
136136
}
@@ -367,4 +367,13 @@ public String stripBoundaryQuotes(String token) {
367367
public boolean isRequired() {
368368
return getMinimum()>0;
369369
}
370+
371+
public void defaults(final WriteableCommandLine commandLine) {
372+
super.defaults(commandLine);
373+
defaultValues(commandLine,this);
374+
}
375+
376+
public void defaultValues(final WriteableCommandLine commandLine, final Option option) {
377+
commandLine.setDefaultValues(option, defaultValues);
378+
}
370379
}

src/java/org/apache/commons/cli2/option/GroupImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ public int getMaximum() {
444444
public boolean isRequired() {
445445
return getMinimum()>0;
446446
}
447+
448+
public void defaults(final WriteableCommandLine commandLine) {
449+
super.defaults(commandLine);
450+
451+
for (final Iterator i = options.iterator(); i.hasNext();) {
452+
final Option option = (Option) i.next();
453+
option.defaults(commandLine);
454+
}
455+
456+
for (final Iterator i = anonymous.iterator(); i.hasNext();) {
457+
final Option option = (Option) i.next();
458+
option.defaults(commandLine);
459+
}
460+
}
447461
}
448462

449463
class ReverseStringComparator implements Comparator {

src/java/org/apache/commons/cli2/option/OptionImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.commons.cli2.DisplaySetting;
2121
import org.apache.commons.cli2.Option;
22+
import org.apache.commons.cli2.WriteableCommandLine;
2223

2324
/**
2425
* A base implementation of Option providing limited ground work for further
@@ -99,4 +100,8 @@ public Option findOption(String trigger) {
99100
public boolean isRequired() {
100101
return required;
101102
}
103+
104+
public void defaults(final WriteableCommandLine commandLine) {
105+
// nothing to do normally
106+
}
102107
}

src/java/org/apache/commons/cli2/option/ParentImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,16 @@ public Option findOption(final String trigger) {
252252
return found;
253253
}
254254
}
255+
256+
public void defaults(final WriteableCommandLine commandLine) {
257+
super.defaults(commandLine);
258+
259+
if(argument!=null) {
260+
argument.defaultValues(commandLine,this);
261+
}
262+
263+
if(children!=null) {
264+
children.defaults(commandLine);
265+
}
266+
}
255267
}

0 commit comments

Comments
 (0)