roxspring 2004/09/06 15:57:44
Modified: cli/src/java/org/apache/commons/cli2 Argument.java
Option.java WriteableCommandLine.java
cli/src/java/org/apache/commons/cli2/option GroupImpl.java
Switch.java ParentImpl.java OptionImpl.java
ArgumentImpl.java
cli/src/java/org/apache/commons/cli2/commandline Parser.java
WriteableCommandLineImpl.java
cli/src/test/org/apache/commons/cli2/option SwitchTest.java
cli/src/java/org/apache/commons/cli2/builder
SwitchBuilder.java
Log:
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
Revision Changes Path
1.3 +10 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/Argument.java
Index: Argument.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/Argument.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Argument.java 22 Apr 2004 23:00:06 -0000 1.2
+++ Argument.java 6 Sep 2004 22:57:43 -0000 1.3
@@ -49,6 +49,16 @@
final ListIterator args,
final Option option)
throws OptionException;
+
+ /**
+ * Adds defaults to a CommandLine.
+ *
+ * @param commandLine
+ * The CommandLine object to store defaults in.
+ * @param option
+ * The Option to store the defaults against.
+ */
+ void defaultValues(final WriteableCommandLine commandLine, final Option option);
/**
* Performs any necessary validation on the values added to the
1.3 +11 -0 jakarta-commons/cli/src/java/org/apache/commons/cli2/Option.java
Index: Option.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/Option.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Option.java 22 Apr 2004 23:00:06 -0000 1.2
+++ Option.java 6 Sep 2004 22:57:43 -0000 1.3
@@ -47,6 +47,17 @@
final WriteableCommandLine commandLine,
final ListIterator args)
throws OptionException;
+
+ /**
+ * Adds defaults to a CommandLine.
+ *
+ * Any defaults for this option are applied as well as the defaults for
+ * any contained options
+ *
+ * @param commandLine
+ * The CommandLine object to store defaults in
+ */
+ void defaults(final WriteableCommandLine commandLine);
/**
* Indicates whether this Option will be able to process the particular
1.3 +16 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/WriteableCommandLine.java
Index: WriteableCommandLine.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/WriteableCommandLine.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WriteableCommandLine.java 22 Apr 2004 23:00:06 -0000 1.2
+++ WriteableCommandLine.java 6 Sep 2004 22:57:43 -0000 1.3
@@ -15,6 +15,8 @@
*/
package org.apache.commons.cli2;
+import java.util.List;
+
/**
* A CommandLine that detected values and options can be written to.
*/
@@ -32,6 +34,13 @@
* @param value the value to add
*/
void addValue(final Option option, final Object value);
+
+ /**
+ * Sets the default values for an Option in the CommandLine
+ * @param option the Option to add to
+ * @param defaultValues the defaults for the option
+ */
+ void setDefaultValues(final Option option, final List defaultValues);
/**
* Adds a switch value to an Option in the CommandLine.
@@ -40,6 +49,13 @@
* @throws IllegalStateException if the switch has already been added
*/
void addSwitch(final Option option, final boolean value) throws
IllegalStateException;
+
+ /**
+ * Sets the default state for a Switch in the CommandLine.
+ * @param option the Option to add to
+ * @param defaultSwitch the defaults state for ths switch
+ */
+ void setDefaultSwitch(final Option option, final Boolean defaultSwitch);
/**
* Adds a property value to a name in the CommandLine.
1.4 +14 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/GroupImpl.java
Index: GroupImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/GroupImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- GroupImpl.java 17 Aug 2004 13:51:36 -0000 1.3
+++ GroupImpl.java 6 Sep 2004 22:57:44 -0000 1.4
@@ -444,6 +444,20 @@
public boolean isRequired() {
return getMinimum()>0;
}
+
+ public void defaults(final WriteableCommandLine commandLine) {
+ super.defaults(commandLine);
+
+ for (final Iterator i = options.iterator(); i.hasNext();) {
+ final Option option = (Option) i.next();
+ option.defaults(commandLine);
+ }
+
+ for (final Iterator i = anonymous.iterator(); i.hasNext();) {
+ final Option option = (Option) i.next();
+ option.defaults(commandLine);
+ }
+ }
}
class ReverseStringComparator implements Comparator {
1.3 +9 -2
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java
Index: Switch.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Switch.java 22 Apr 2004 23:00:07 -0000 1.2
+++ Switch.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -52,6 +52,7 @@
private final String preferredName;
private final Set aliases;
private final Set prefixes;
+ private final Boolean defaultSwitch;
/**
* Creates a new Switch with the specified parameters
@@ -74,7 +75,8 @@
final boolean required,
final Argument argument,
final Group children,
- final int id) {
+ final int id,
+ final Boolean switchDefault) {
super(argument, children, description, id, required);
if (enabledPrefix == null) {
@@ -122,7 +124,8 @@
newPrefixes.add(enabledPrefix);
newPrefixes.add(disabledPrefix);
this.prefixes = Collections.unmodifiableSet(newPrefixes);
-
+
+ this.defaultSwitch = switchDefault;
}
public void processParent(
@@ -230,5 +233,9 @@
public String getPreferredName() {
return enabledPrefix + preferredName;
+ }
+
+ public void defaults(final WriteableCommandLine commandLine) {
+ commandLine.setDefaultSwitch(this, defaultSwitch);
}
}
1.3 +12 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/ParentImpl.java
Index: ParentImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/ParentImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ParentImpl.java 22 Apr 2004 23:00:07 -0000 1.2
+++ ParentImpl.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -252,4 +252,16 @@
return found;
}
}
+
+ public void defaults(final WriteableCommandLine commandLine) {
+ super.defaults(commandLine);
+
+ if(argument!=null) {
+ argument.defaultValues(commandLine,this);
+ }
+
+ if(children!=null) {
+ children.defaults(commandLine);
+ }
+ }
}
1.3 +5 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/OptionImpl.java
Index: OptionImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/OptionImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- OptionImpl.java 22 Apr 2004 23:00:07 -0000 1.2
+++ OptionImpl.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -19,6 +19,7 @@
import org.apache.commons.cli2.DisplaySetting;
import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.WriteableCommandLine;
/**
* A base implementation of Option providing limited ground work for further
@@ -98,5 +99,9 @@
public boolean isRequired() {
return required;
+ }
+
+ public void defaults(final WriteableCommandLine commandLine) {
+ // nothing to do normally
}
}
1.3 +14 -5
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
Index: ArgumentImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/ArgumentImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ArgumentImpl.java 22 Apr 2004 23:00:07 -0000 1.2
+++ ArgumentImpl.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -103,7 +103,7 @@
final char subsequentSeparator,
final Validator validator,
final String consumeRemaining,
- final List defaultValues,
+ final List valueDefaults,
final int id) {
super(id,false);
@@ -117,20 +117,20 @@
this.subsequentSplit = subsequentSeparator != NUL;
this.validator = validator;
this.consumeRemaining = consumeRemaining;
- this.defaultValues = defaultValues;
+ this.defaultValues = valueDefaults;
if (minimum > maximum) {
throw new IllegalArgumentException(
resources.getMessage("cli.error.minimum.exceeds.maximum"));
}
- if (defaultValues != null) {
- if (defaultValues.size() < minimum) {
+ if (valueDefaults != null) {
+ if (valueDefaults.size() < minimum) {
throw new IllegalArgumentException(
resources.getMessage("cli.error.too.few.defaults"));
}
- if (defaultValues.size() > maximum) {
+ if (valueDefaults.size() > maximum) {
throw new IllegalArgumentException(
resources.getMessage("cli.error.too.many.defaults"));
}
@@ -366,5 +366,14 @@
public boolean isRequired() {
return getMinimum()>0;
+ }
+
+ public void defaults(final WriteableCommandLine commandLine) {
+ super.defaults(commandLine);
+ defaultValues(commandLine,this);
+ }
+
+ public void defaultValues(final WriteableCommandLine commandLine, final Option
option) {
+ commandLine.setDefaultValues(option, defaultValues);
}
}
1.3 +4 -1
jakarta-commons/cli/src/java/org/apache/commons/cli2/commandline/Parser.java
Index: Parser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/commandline/Parser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Parser.java 22 Apr 2004 23:00:15 -0000 1.2
+++ Parser.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -56,10 +56,13 @@
public CommandLine parse(final String[] arguments) throws OptionException {
final List argumentList = new LinkedList(Arrays.asList(arguments));
- final ListIterator iterator = argumentList.listIterator();
final WriteableCommandLine commandLine =
new WriteableCommandLineImpl(group, new ArrayList());
+
+ // pick up any defaults from the model
+ group.defaults(commandLine);
+ final ListIterator iterator = argumentList.listIterator();
while (group.canProcess(iterator)) {
group.process(commandLine, iterator);
}
1.3 +30 -38
jakarta-commons/cli/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
Index: WriteableCommandLineImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WriteableCommandLineImpl.java 22 Apr 2004 23:00:15 -0000 1.2
+++ WriteableCommandLineImpl.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -26,7 +25,6 @@
import java.util.Set;
import org.apache.commons.cli2.Argument;
-import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.WriteableCommandLine;
@@ -41,12 +39,11 @@
private final Map nameToOption = new HashMap();
private final Map values = new HashMap();
private final Map switches = new HashMap();
- private final Map defaults = new HashMap();
+ private final Map defaultValues = new HashMap();
+ private final Map defaultSwitches = new HashMap();
private final List normalised;
private final Set prefixes;
- private CommandLine defaultCommandLine = null;
-
/**
* Creates a new WriteableCommandLineImpl rooted on the specified Option, to
* hold the parsed arguments.
@@ -91,38 +88,26 @@
public boolean hasOption(final Option option) {
final boolean present = options.contains(option);
- if (!present && defaultCommandLine != null) {
- return defaultCommandLine.hasOption(option);
- }
- else {
- return present;
- }
+ return present;
}
public Option getOption(final String trigger) {
return (Option)nameToOption.get(trigger);
}
- //TODO Document the order of values and defaults
public List getValues(final Option option, final List defaultValues) {
// First grab the command line values
List valueList = (List)values.get(option);
- // Secondly try alternate CommandLines
- if ((valueList == null || valueList.isEmpty())
- && defaultCommandLine != null) {
- valueList = defaultCommandLine.getValues(option, null);
- }
-
- // Thirdly try the defaults supplied to the method
+ // Secondly try the defaults supplied to the method
if (valueList == null || valueList.isEmpty()) {
valueList = defaultValues;
}
- // Fourthly try the option's default values
+ // Thirdly try the option's default values
if (valueList == null || valueList.isEmpty()) {
- valueList = (List)this.defaults.get(option);
+ valueList = (List)this.defaultValues.get(option);
}
// Finally use an empty list
@@ -137,18 +122,15 @@
// First grab the command line values
Boolean bool = (Boolean)switches.get(option);
- // Secondly try alternate CommandLines
- if (bool == null && defaultCommandLine != null) {
- bool = defaultCommandLine.getSwitch(option);
- }
-
- // Thirdly try the defaults supplied to the method
+ // Secondly try the defaults supplied to the method
if (bool == null) {
bool = defaultValue;
}
- // Fourthly try the option's default values
- //????
+ // Thirdly try the option's default values
+ if (bool == null) {
+ bool = (Boolean)this.defaultSwitches.get(option);
+ }
return bool;
}
@@ -162,15 +144,7 @@
}
public Set getProperties() {
- if (defaultCommandLine == null) {
- return Collections.unmodifiableSet(properties.keySet());
- }
- else {
- final Set props = new HashSet();
- props.addAll(properties.keySet());
- props.addAll(defaultCommandLine.getProperties());
- return Collections.unmodifiableSet(props);
- }
+ return Collections.unmodifiableSet(properties.keySet());
}
public boolean looksLikeOption(final String trigger) {
@@ -211,5 +185,23 @@
public Set getOptionTriggers() {
return Collections.unmodifiableSet(nameToOption.keySet());
+ }
+
+ public void setDefaultValues(final Option option, final List defaults) {
+ if (defaults==null) {
+ defaultValues.remove(option);
+ }
+ else {
+ defaultValues.put(option, defaults);
+ }
+ }
+
+ public void setDefaultSwitch(final Option option, final Boolean defaultSwitch) {
+ if (defaultSwitch==null) {
+ defaultSwitches.remove(defaultSwitch);
+ }
+ else {
+ defaultSwitches.put(option, defaultSwitch);
+ }
}
}
1.3 +2 -1
jakarta-commons/cli/src/test/org/apache/commons/cli2/option/SwitchTest.java
Index: SwitchTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/option/SwitchTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SwitchTest.java 22 Apr 2004 23:00:14 -0000 1.2
+++ SwitchTest.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -47,7 +47,8 @@
true,
null,
null,
- 'd');
+ 'd',
+ null);
}
/*
1.3 +15 -1
jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java
Index: SwitchBuilder.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SwitchBuilder.java 22 Apr 2004 23:00:15 -0000 1.2
+++ SwitchBuilder.java 6 Sep 2004 22:57:44 -0000 1.3
@@ -37,6 +37,7 @@
private Argument argument;
private Group children;
private int id;
+ private Boolean switchDefault;
/**
* Creates a new SwitchBuilder using defaults.
@@ -86,7 +87,8 @@
required,
argument,
children,
- id);
+ id,
+ switchDefault);
reset();
@@ -104,6 +106,7 @@
argument = null;
children = null;
id = 0;
+ switchDefault = null;
}
/**
@@ -171,6 +174,17 @@
*/
public final SwitchBuilder withId(final int newId) {
this.id = newId;
+ return this;
+ }
+
+ /**
+ * Sets the default state for this switch
+ *
+ * @param switchDefault the default state
+ * @return this SwitchBuilder
+ */
+ public final SwitchBuilder withSwitchDefault(final Boolean switchDefault) {
+ this.switchDefault = switchDefault;
return this;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]