You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2003/10/21 22:23:21 UTC
cvs commit: jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2 ArgumentBuilder.java ArgumentImpl.java Argument.java
roxspring 2003/10/21 13:23:21
Modified: cli/src/java/org/apache/commons/cli2 ArgumentBuilder.java
ArgumentImpl.java Argument.java
Log:
Removed tabs and formatted inline with checkstyle settings
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS: If this change addresses a PR in the problem report tracking
CVS: database, then enter the PR number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system, such as NCSA,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to Apache by someone else; i.e.,
CVS: they sent us a patch or a new module, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.
Revision Changes Path
1.4 +177 -138 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java
Index: ArgumentBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ArgumentBuilder.java 21 Oct 2003 19:28:14 -0000 1.3
+++ ArgumentBuilder.java 21 Oct 2003 20:23:21 -0000 1.4
@@ -66,139 +66,178 @@
* Builds Argument instances.
* @author Rob Oxspring
*/
-public class ArgumentBuilder {
-
- public ArgumentBuilder() {
- reset();
- }
-
- /** The name of the argument. Used for display and lookups in CommandLine */
- private String name;
-
- /** A description of the argument. Used in the automated online help */
- private String description;
-
- /** The minimum number of values needed for to be valid */
- private int minimum;
-
- /** The maximum number of values needed for to be valid */
- private int maximum;
-
- /** The character used to separate the values from the option */
- private char initialSeparator;
-
- /** The character used to separate the values from each other */
- private char subsequentSeparator;
-
- /** The validator object that should be used to ensure the values are valid */
- private Validator validator;
-
- /**
- * Creates a new Argument instance using the options specified in this ArgumentBuilder.
- * @return A new Argument instance
- */
- public Argument create() {
- final Argument argument =
- new ArgumentImpl(
- name,
- description,
- minimum,
- maximum,
- initialSeparator,
- subsequentSeparator,
- validator);
-
- reset();
-
- return argument;
- }
-
- /**
- * Resets the ArgumentBuilder to the defaults for a new Argument.
- * The method should be called automatically at the end of a create() call.
- */
- public void reset() {
- name = "arg";
- description = null;
- minimum = 0;
- maximum = Integer.MAX_VALUE;
- initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
- subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
- }
-
- /**
- * Sets the name of the argument.
- * The name is used when displaying usage information and to allow lookups in the CommandLine object.
- * @see CommandLine#getValue(String)
- *
- * @param name the name of the argument
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withName(final String name) {
- this.name = name;
- return this;
- }
-
- /**
- * Sets the description of the argument.
- * The description is used when displaying online help.
- *
- * @param description a description of the argument.
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withDescription(final String description) {
- this.description = description;
- return this;
- }
-
- /**
- * Sets the minimum number of values needed for the argument to be valid.
- *
- * @param minimum the number of values needed
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withMinimum(final int minimum) {
- this.minimum = minimum;
- return this;
- }
-
- /**
- * Sets the maximum number of values allowed for the argument to be valid.
- *
- * @param maximum the number of values allowed
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withMaximum(final int maximum) {
- this.maximum = maximum;
- return this;
- }
-
- /**
- * Sets the character used to separate the values from the option.
- * When an argument is of the form -libs:dir1,dir2,dir3 the initialSeparator would be ':'.
- *
- * @param initialSeparator the character used to separate the values from the option
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withInitialSeparator(final char initialSeparator) {
- this.initialSeparator = initialSeparator;
- return this;
- }
-
- /**
- * Sets the character used to separate the values from each other.
- * When an argument is of the form -libs:dir1,dir2,dir3 the subsequentSeparator would be ','.
- *
- * @param subsequentSeparator the character used to separate the values from each other
- * @return this ArgumentBuilder
- */
- public ArgumentBuilder withSubsequentSeparator(final char subsequentSeparator) {
- this.subsequentSeparator = subsequentSeparator;
- return this;
- }
-
- public ArgumentBuilder withValidator(final Validator validator) {
- this.validator = validator;
- return this;
- }
+public class ArgumentBuilder
+{
+ /**
+ * Creates a new ArgumentBuilder instance
+ */
+ public ArgumentBuilder()
+ {
+ reset();
+ }
+
+ /**
+ * The name of the argument. Used for display and lookups in CommandLine
+ */
+ private String name;
+
+ /**
+ * A description of the argument. Used in the automated online help
+ */
+ private String description;
+
+ /**
+ * The minimum number of values needed for to be valid
+ */
+ private int minimum;
+
+ /**
+ * The maximum number of values needed for to be valid
+ */
+ private int maximum;
+
+ /**
+ * The character used to separate the values from the option
+ */
+ private char initialSeparator;
+
+ /**
+ * The character used to separate the values from each other
+ */
+ private char subsequentSeparator;
+
+ /**
+ * The validator object that should be used to ensure the values are valid
+ */
+ private Validator validator;
+
+ /**
+ * Creates a new Argument instance using the options specified in this
+ * ArgumentBuilder.
+ *
+ * @return A new Argument instance.
+ */
+ public final Argument create()
+ {
+ final Argument argument =
+ new ArgumentImpl(
+ name,
+ description,
+ minimum,
+ maximum,
+ initialSeparator,
+ subsequentSeparator,
+ validator);
+
+ reset();
+
+ return argument;
+ }
+
+ /**
+ * Resets the ArgumentBuilder to the defaults for a new Argument.
+ * The method should be called automatically at the end of a create()
+ * call.
+ */
+ public final void reset()
+ {
+ name = "arg";
+ description = null;
+ minimum = 0;
+ maximum = Integer.MAX_VALUE;
+ initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
+ subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
+ }
+
+ /**
+ * Sets the name of the argument.
+ * The name is used when displaying usage information and to allow
+ * lookups in the CommandLine object.
+ *
+ * @see CommandLine#getValue(String)
+ * @param name the name of the argument.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withName(final String name)
+ {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the description of the argument.
+ * The description is used when displaying online help.
+ *
+ * @param description a description of the argument.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withDescription(final String description)
+ {
+ this.description = description;
+ return this;
+ }
+
+ /**
+ * Sets the minimum number of values needed for the argument to be valid.
+ *
+ * @param minimum the number of values needed.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withMinimum(final int minimum)
+ {
+ this.minimum = minimum;
+ return this;
+ }
+
+ /**
+ * Sets the maximum number of values allowed for the argument to be valid.
+ *
+ * @param maximum the number of values allowed.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withMaximum(final int maximum)
+ {
+ this.maximum = maximum;
+ return this;
+ }
+
+ /**
+ * Sets the character used to separate the values from the option.
+ * When an argument is of the form -libs:dir1,dir2,dir3 the
+ * initialSeparator would be ':'.
+ *
+ * @param initialSeparator the character used to separate the values from
+ * the option.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withInitialSeparator(final char initialSeparator)
+ {
+ this.initialSeparator = initialSeparator;
+ return this;
+ }
+
+ /**
+ * Sets the character used to separate the values from each other.
+ * When an argument is of the form -libs:dir1,dir2,dir3 the subsequentSeparator would be ','.
+ *
+ * @param subsequentSeparator the character used to separate the values from each other.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withSubsequentSeparator(final char subsequentSeparator)
+ {
+ this.subsequentSeparator = subsequentSeparator;
+ return this;
+ }
+
+ /**
+ * Sets the validator instance used to perform validation on the Argument values.
+ *
+ * @param validator a Validator instance.
+ * @return this ArgumentBuilder.
+ */
+ public final ArgumentBuilder withValidator(final Validator validator)
+ {
+ this.validator = validator;
+ return this;
+ }
}
1.3 +380 -335 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java
Index: ArgumentImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ArgumentImpl.java 21 Oct 2003 19:28:14 -0000 1.2
+++ ArgumentImpl.java 21 Oct 2003 20:23:21 -0000 1.3
@@ -73,356 +73,401 @@
* An implementation of an Argument.
* @author Rob Oxspring
*/
-public class ArgumentImpl implements Argument {
+public class ArgumentImpl implements Argument
+{
- private final String name;
- private final String description;
- private final int minimum;
- private final int maximum;
- private final char initialSeparator;
- private final char subsequentSepatator;
- private final boolean initialSplit;
- private final boolean subsequentSplit;
- private final Validator validator;
+ private final String name;
+ private final String description;
+ private final int minimum;
+ private final int maximum;
+ private final char initialSeparator;
+ private final char subsequentSepatator;
+ private final boolean initialSplit;
+ private final boolean subsequentSplit;
+ private final Validator validator;
- private static final char NUL = '\0';
+ private static final char NUL = '\0';
public static final char DEFAULT_INITIAL_SEPARATOR = NUL;
public static final char DEFAULT_SUBSEQUENT_SEPARATOR = NUL;
+ /**
+ * Creates a new Argument instance.
+ * @param name The name of the argument
+ * @param description A description of the argument
+ * @param minimum The minimum number of values needed to be valid
+ * @param maximum The maximum number of values allowed to be valid
+ * @param initialSeparator The char separating option from value
+ * @param subsequentSeparator The char separating values from each other
+ */
+ public ArgumentImpl(
+ final String name,
+ final String description,
+ final int minimum,
+ final int maximum,
+ final char initialSeparator,
+ final char subsequentSeparator,
+ final Validator validator)
+ {
+
+ if (name == null)
+ {
+ this.name = "arg";
+ } else
+ {
+ this.name = name;
+ }
- /**
- * Creates a new Argument instance.
- * @param name The name of the argument
- * @param description A description of the argument
- * @param minimum The minimum number of values needed to be valid
- * @param maximum The maximum number of values allowed to be valid
- * @param initialSeparator The char separating option from value
- * @param subsequentSeparator The char separating values from each other
- */
- public ArgumentImpl(
- final String name,
- final String description,
- final int minimum,
- final int maximum,
- final char initialSeparator,
- final char subsequentSeparator,
- final Validator validator) {
-
- if (name == null) {
- this.name = "arg";
- } else {
- this.name = name;
- }
-
- this.description = description;
- this.minimum = minimum;
- this.maximum = maximum;
- this.initialSeparator = initialSeparator;
- this.subsequentSepatator = subsequentSeparator;
- this.initialSplit = initialSeparator != NUL;
- this.subsequentSplit = subsequentSeparator != NUL;
- this.validator = validator;
-
- if(minimum>maximum){
+ this.description = description;
+ this.minimum = minimum;
+ this.maximum = maximum;
+ this.initialSeparator = initialSeparator;
+ this.subsequentSepatator = subsequentSeparator;
+ this.initialSplit = initialSeparator != NUL;
+ this.subsequentSplit = subsequentSeparator != NUL;
+ this.validator = validator;
+
+ if (minimum > maximum)
+ {
throw new IllegalArgumentException("minimum must not exceed maximum");
}
-
- }
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Argument#processValues(org.apache.commons.cli2.CommandLine, java.util.ListIterator)
- */
- // public void processValues(
- // final CommandLine commandLine,
- // final ListIterator arguments)
- // throws OptionException {
- //
- // commandLine.addOption(this);
- // int argumentCount = commandLine.getValues(this).size();
- // while (arguments.hasNext() && argumentCount < maximum) {
- //
- // final String allValues = (String) arguments.next();
- //
- // if (commandLine.looksLikeOption(allValues)) {
- // arguments.previous();
- // break;
- // }
- //
- // final StringTokenizer values =
- // new StringTokenizer(
- // allValues,
- // String.valueOf(subsequentSepatator));
- //
- // while (values.hasMoreTokens() && argumentCount < maximum) {
- // ++argumentCount;
- // commandLine.addValue(this, values.nextToken());
- // }
- // if (values.hasMoreTokens()) {
- // throw new UnexpectedValueException(this, values.nextToken());
- // }
- // }
- //
- // if (argumentCount < minimum) {
- // throw new MissingValueException(this);
- // }
- // }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Argument#getName()
- */
- public String getPreferredName() {
- return name;
- }
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Argument#preProcess(java.util.ListIterator)
- */
- public void preProcess(final ListIterator args) { //throws OptionException {
-
- if (initialSplit) {
-
- final String argument = (String) args.next();
- final int initialIndex = argument.indexOf(initialSeparator);
- if (initialIndex > 0) {
- args.remove();
- args.add(argument.substring(0, initialIndex));
- args.add(argument.substring(initialIndex + 1));
- args.previous();
- }
- args.previous();
- }
-
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Argument#processValues(org.apache.commons.cli2.CommandLine, java.util.ListIterator, org.apache.commons.cli2.Option)
- */
- public void processValues(
- final CommandLine commandLine,
- final ListIterator args,
- final Option option)
- throws OptionException {
-
- //commandLine.addOption(option);
- int argumentCount;
- final List argumentValues = commandLine.getValues(option);
- if (argumentValues == null) {
- argumentCount = 0;
- } else {
- argumentCount = argumentValues.size();
- }
-
- String argument = "";
- while (args.hasNext() && argumentCount < maximum) {
- argument = (String) args.next();
-
- if (commandLine.looksLikeOption(argument)) {
- args.previous();
- break;
- }
-
- if (subsequentSplit) {
- args.remove();
- String remaining = argument;
- int subsequentIndex = remaining.indexOf(subsequentSepatator);
-
- while (subsequentIndex >= 0 && argumentCount < maximum) {
- ++argumentCount;
- final String head = remaining.substring(0, subsequentIndex);
- args.add(head);
- commandLine.addValue(option, head);
- remaining = remaining.substring(subsequentIndex + 1);
- subsequentIndex = remaining.indexOf(subsequentSepatator);
- }
- ++argumentCount;
- args.add(remaining);
- commandLine.addValue(option, remaining);
-
- if (argumentCount > maximum) {
- throw new UnexpectedValueException(option, remaining);
- }
- } else {
- ++argumentCount;
- commandLine.addValue(option, argument);
- }
- }
-
- if (argumentCount < minimum) {
- throw new MissingValueException(option);
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#canProcess(java.lang.String)
- */
- public boolean canProcess(String arg) {
- return true;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#prefixes()
- */
- public Set prefixes() {
- return Collections.EMPTY_SET;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine, java.util.ListIterator)
- */
- public void process(CommandLine commandLine, ListIterator args)
- throws OptionException {
-
- processValues(commandLine, args, this);
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#triggers()
- */
- public Set triggers() {
- return Collections.EMPTY_SET;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Argument#canPreProcess(java.lang.String)
- */
- public String canPreProcess(final String arg) {
-
- if (initialSplit) {
- final int initialIndex = arg.indexOf(initialSeparator);
- if (initialIndex > 0) {
- return arg.substring(0, initialIndex);
- }
- }
-
- return arg;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine)
- */
- public void validate(final CommandLine commandLine)
- throws OptionException {
- validate(commandLine, this);
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine,org.apache.commons.cli2.Option)
- */
- public void validate(final CommandLine commandLine, final Option option)
- throws OptionException {
-
- final List values = commandLine.getValues(option);
-
- if (values.size() < minimum) {
- throw new MissingValueException(option);
- }
-
- if (values.size() > maximum) {
- throw new UnexpectedValueException(
- option,
- (String) values.get(maximum));
- }
-
- if (validator != null) {
- try {
- validator.validate(values);
- } catch (InvalidArgumentException ive) {
- throw new UnexpectedValueException(option, ive.getMessage());
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#appendUsage(java.lang.StringBuffer, java.util.Set, java.util.Comparator)
- */
- public void appendUsage(
- final StringBuffer buffer,
- final Set helpSettings,
- final Comparator comp) {
-
- // do we display the outer optionality
- final boolean optional =
- helpSettings.contains(HelpSetting.DISPLAY_OPTIONAL);
-
- // allow numbering if multiple args
- final boolean numbered =
- maximum > 1
- && helpSettings.contains(HelpSetting.DISPLAY_ARGUMENT_NUMBERED);
-
- final boolean bracketed =
- helpSettings.contains(HelpSetting.DISPLAY_ARGUMENT_BRACKETED);
-
- // if infinite args are allowed then crop the list
- final int max = maximum == Integer.MAX_VALUE ? 2 : maximum;
-
- int i = 0;
- // for each argument
- while (i < max) {
- // if we're past the first add a space
- if (i > 0) {
- buffer.append(' ');
- }
- // if the next arg is optional
- if (i >= minimum && (optional || i > 0)) {
- buffer.append('[');
- }
- if (bracketed) {
- buffer.append('<');
- }
- // add name
- buffer.append(name);
- ++i;
- // if numbering
- if (numbered) {
- buffer.append(i);
- }
- if (bracketed) {
- buffer.append('>');
- }
- }
- // if infinite args are allowed
- if (maximum == Integer.MAX_VALUE) {
- // append elipsis
- buffer.append(" ...");
- }
- // for each argument
- while (i > 0) {
- --i;
- // if the next arg is optional
- if (i >= minimum && (optional || i > 0)) {
- buffer.append(']');
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#getDescription()
- */
- public String getDescription() {
- return description;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.Option#helpLines(int, java.util.Set, java.util.Comparator)
- */
- public List helpLines(
- final int depth,
- final Set helpSettings,
- final Comparator comp) {
- final HelpLine helpLine = new HelpLine(this, depth);
- return Collections.singletonList(helpLine);
- }
-
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Argument#processValues(org.apache.commons.cli2.CommandLine, java.util.ListIterator)
+ */
+ // public void processValues(
+ // final CommandLine commandLine,
+ // final ListIterator arguments)
+ // throws OptionException {
+ //
+ // commandLine.addOption(this);
+ // int argumentCount = commandLine.getValues(this).size();
+ // while (arguments.hasNext() && argumentCount < maximum) {
+ //
+ // final String allValues = (String) arguments.next();
+ //
+ // if (commandLine.looksLikeOption(allValues)) {
+ // arguments.previous();
+ // break;
+ // }
+ //
+ // final StringTokenizer values =
+ // new StringTokenizer(
+ // allValues,
+ // String.valueOf(subsequentSepatator));
+ //
+ // while (values.hasMoreTokens() && argumentCount < maximum) {
+ // ++argumentCount;
+ // commandLine.addValue(this, values.nextToken());
+ // }
+ // if (values.hasMoreTokens()) {
+ // throw new UnexpectedValueException(this, values.nextToken());
+ // }
+ // }
+ //
+ // if (argumentCount < minimum) {
+ // throw new MissingValueException(this);
+ // }
+ // }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Argument#getName()
+ */
+ public String getPreferredName()
+ {
+ return name;
+ }
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Argument#preProcess(java.util.ListIterator)
+ */
+ public void preProcess(final ListIterator args)
+ { //throws OptionException {
+
+ if (initialSplit)
+ {
+
+ final String argument = (String)args.next();
+ final int initialIndex = argument.indexOf(initialSeparator);
+ if (initialIndex > 0)
+ {
+ args.remove();
+ args.add(argument.substring(0, initialIndex));
+ args.add(argument.substring(initialIndex + 1));
+ args.previous();
+ }
+ args.previous();
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Argument#processValues(org.apache.commons.cli2.CommandLine, java.util.ListIterator, org.apache.commons.cli2.Option)
+ */
+ public void processValues(
+ final CommandLine commandLine,
+ final ListIterator args,
+ final Option option)
+ throws OptionException
+ {
+
+ //commandLine.addOption(option);
+ int argumentCount;
+ final List argumentValues = commandLine.getValues(option);
+ if (argumentValues == null)
+ {
+ argumentCount = 0;
+ } else
+ {
+ argumentCount = argumentValues.size();
+ }
+
+ String argument = "";
+ while (args.hasNext() && argumentCount < maximum)
+ {
+ argument = (String)args.next();
+
+ if (commandLine.looksLikeOption(argument))
+ {
+ args.previous();
+ break;
+ }
+
+ if (subsequentSplit)
+ {
+ args.remove();
+ String remaining = argument;
+ int subsequentIndex = remaining.indexOf(subsequentSepatator);
+
+ while (subsequentIndex >= 0 && argumentCount < maximum)
+ {
+ ++argumentCount;
+ final String head = remaining.substring(0, subsequentIndex);
+ args.add(head);
+ commandLine.addValue(option, head);
+ remaining = remaining.substring(subsequentIndex + 1);
+ subsequentIndex = remaining.indexOf(subsequentSepatator);
+ }
+ ++argumentCount;
+ args.add(remaining);
+ commandLine.addValue(option, remaining);
+
+ if (argumentCount > maximum)
+ {
+ throw new UnexpectedValueException(option, remaining);
+ }
+ } else
+ {
+ ++argumentCount;
+ commandLine.addValue(option, argument);
+ }
+ }
+
+ if (argumentCount < minimum)
+ {
+ throw new MissingValueException(option);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#canProcess(java.lang.String)
+ */
+ public boolean canProcess(String arg)
+ {
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#prefixes()
+ */
+ public Set prefixes()
+ {
+ return Collections.EMPTY_SET;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine, java.util.ListIterator)
+ */
+ public void process(CommandLine commandLine, ListIterator args)
+ throws OptionException
+ {
+
+ processValues(commandLine, args, this);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#triggers()
+ */
+ public Set triggers()
+ {
+ return Collections.EMPTY_SET;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Argument#canPreProcess(java.lang.String)
+ */
+ public String canPreProcess(final String arg)
+ {
+
+ if (initialSplit)
+ {
+ final int initialIndex = arg.indexOf(initialSeparator);
+ if (initialIndex > 0)
+ {
+ return arg.substring(0, initialIndex);
+ }
+ }
+
+ return arg;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine)
+ */
+ public void validate(final CommandLine commandLine) throws OptionException
+ {
+ validate(commandLine, this);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine,org.apache.commons.cli2.Option)
+ */
+ public void validate(final CommandLine commandLine, final Option option)
+ throws OptionException
+ {
+
+ final List values = commandLine.getValues(option);
+
+ if (values.size() < minimum)
+ {
+ throw new MissingValueException(option);
+ }
+
+ if (values.size() > maximum)
+ {
+ throw new UnexpectedValueException(
+ option,
+ (String)values.get(maximum));
+ }
+
+ if (validator != null)
+ {
+ try
+ {
+ validator.validate(values);
+ } catch (InvalidArgumentException ive)
+ {
+ throw new UnexpectedValueException(option, ive.getMessage());
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#appendUsage(java.lang.StringBuffer, java.util.Set, java.util.Comparator)
+ */
+ public void appendUsage(
+ final StringBuffer buffer,
+ final Set helpSettings,
+ final Comparator comp)
+ {
+
+ // do we display the outer optionality
+ final boolean optional =
+ helpSettings.contains(HelpSetting.DISPLAY_OPTIONAL);
+
+ // allow numbering if multiple args
+ final boolean numbered =
+ maximum > 1
+ && helpSettings.contains(HelpSetting.DISPLAY_ARGUMENT_NUMBERED);
+
+ final boolean bracketed =
+ helpSettings.contains(HelpSetting.DISPLAY_ARGUMENT_BRACKETED);
+
+ // if infinite args are allowed then crop the list
+ final int max = maximum == Integer.MAX_VALUE ? 2 : maximum;
+
+ int i = 0;
+ // for each argument
+ while (i < max)
+ {
+ // if we're past the first add a space
+ if (i > 0)
+ {
+ buffer.append(' ');
+ }
+ // if the next arg is optional
+ if (i >= minimum && (optional || i > 0))
+ {
+ buffer.append('[');
+ }
+ if (bracketed)
+ {
+ buffer.append('<');
+ }
+ // add name
+ buffer.append(name);
+ ++i;
+ // if numbering
+ if (numbered)
+ {
+ buffer.append(i);
+ }
+ if (bracketed)
+ {
+ buffer.append('>');
+ }
+ }
+ // if infinite args are allowed
+ if (maximum == Integer.MAX_VALUE)
+ {
+ // append elipsis
+ buffer.append(" ...");
+ }
+ // for each argument
+ while (i > 0)
+ {
+ --i;
+ // if the next arg is optional
+ if (i >= minimum && (optional || i > 0))
+ {
+ buffer.append(']');
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#getDescription()
+ */
+ public String getDescription()
+ {
+ return description;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.Option#helpLines(int, java.util.Set, java.util.Comparator)
+ */
+ public List helpLines(
+ final int depth,
+ final Set helpSettings,
+ final Comparator comp)
+ {
+ final HelpLine helpLine = new HelpLine(this, depth);
+ return Collections.singletonList(helpLine);
+ }
+
/**
* @return Returns the maximum.
*/
- public int getMaximum() {
+ public int getMaximum()
+ {
return maximum;
}
/**
* @return Returns the minimum.
*/
- public int getMinimum() {
+ public int getMinimum()
+ {
return minimum;
}
1.2 +44 -26 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/Argument.java
Index: Argument.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/Argument.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Argument.java 18 Oct 2003 22:00:02 -0000 1.1
+++ Argument.java 21 Oct 2003 20:23:21 -0000 1.2
@@ -64,32 +64,50 @@
/**
* An Option that can process values passed on the command line in the form
- * "--file README" .
- *
+ * "--file README".
+ *
* @author Rob Oxspring
*/
-public interface Argument extends Option {
+public interface Argument extends Option
+{
- public String canPreProcess(final String arg);
+ String canPreProcess(final String arg);
- public void preProcess(final ListIterator args);// throws OptionException;
+ void preProcess(final ListIterator args); // throws OptionException;
- /**
- * Processes the "README" style element of the argument
- *
- * Values identified should be added to the CommandLine object in association with this Argument.
- *
- * @see CommandLine#addValue(Option,Object)
- * @param commandLine The CommandLine object to store results in
- * @param args The arguments to process
- * @param option The option to register value against
- */
- public void processValues(
- final CommandLine commandLine,
- final ListIterator args,
- final Option option)
- throws OptionException;
+ /**
+ * Processes the "README" style element of the argument.
+ *
+ * Values identified should be added to the CommandLine object in
+ * association with this Argument.
+ *
+ * @see CommandLine#addValue(Option,Object)
+ * @param commandLine The CommandLine object to store results in.
+ * @param args The arguments to process.
+ * @param option The option to register value against.
+ * @throws OptionException if any problems occur.
+ */
+ void processValues(
+ final CommandLine commandLine,
+ final ListIterator args,
+ final Option option)
+ throws OptionException;
- public void validate(final CommandLine commandLine, final Option option)
- throws OptionException;
+ /**
+ * Performs any necessary validation on the values added to the
+ * CommandLine.
+ *
+ * Validation will typically involve using the
+ * CommandLine.getValues(option) method to retrieve the values
+ * and then either checking each value. Optionally the String
+ * value can be replaced by another Object such as a Number
+ * instance or a File instance.
+ *
+ * @see CommandLine#getValues(Option)
+ * @param commandLine The CommandLine object to query.
+ * @param option The option to lookup values with.
+ * @throws OptionException if any problems occur.
+ */
+ void validate(final CommandLine commandLine, final Option option)
+ throws OptionException;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org