You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jk...@apache.org on 2005/09/13 04:33:02 UTC
svn commit: r280462 [1/4] - in /jakarta/commons/proper/cli/trunk/src:
java/org/apache/commons/cli2/ java/org/apache/commons/cli2/builder/
java/org/apache/commons/cli2/commandline/
java/org/apache/commons/cli2/option/ java/org/apache/commons/cli2/resour...
Author: jkeyes
Date: Mon Sep 12 19:32:04 2005
New Revision: 280462
URL: http://svn.apache.org/viewcvs?rev=280462&view=rev
Log:
- rewrote i18n code
- externalised error strings
Added:
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
Removed:
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/messages.properties
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/messages.properties
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/messages.properties
Modified:
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Command.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/DefaultOption.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/PropertyOption.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/SourceDestArgument.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Switch.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceHelper.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/ClassValidator.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/DateValidator.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/EnumValidator.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/NumberValidator.java
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/UrlValidator.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/DefaultOptionBuilderTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/DefaultingCommandLineTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/PreferencesCommandLineTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/PropertiesCommandLineTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/WriteableCommandLineImplTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/DateValidatorTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/EnumValidatorTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/NumberValidatorTest.java
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/UrlValidatorTest.java
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2003-2004 The Apache Software Foundation
+/*
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,44 +23,42 @@
/**
* A problem found while dealing with command line options.
*/
-public class OptionException extends Exception {
-
+public class OptionException
+ extends Exception {
/**
* The settings used when displaying the related Option.
- *
+ *
* @see DisplaySetting
*/
- public static final Set HELP_SETTINGS =
- Collections.unmodifiableSet(
- Collections.singleton(
- DisplaySetting.DISPLAY_PROPERTY_OPTION));
+ public static final Set HELP_SETTINGS =
+ Collections.unmodifiableSet(Collections.singleton(DisplaySetting.DISPLAY_PROPERTY_OPTION));
+
+ /** resource helper instance */
+ private static final ResourceHelper helper = ResourceHelper.getResourceHelper();
/** The Option the exception relates to */
private final Option option;
-
+
/** The message explaining the Exception */
private final String message;
- /** resource helper instance */
- private static final ResourceHelper helper =
- ResourceHelper.getResourceHelper(OptionException.class);
-
/**
* Creates a new OptionException.
- *
+ *
* @param option
* The Option the exception relates to
*/
public OptionException(final Option option) {
this(option, null, null);
}
-
+
/**
* Creates a new OptionException.
* @param option the Option the exception relates to
* @param messageKey the id of the message to display
*/
- public OptionException(final Option option, final String messageKey) {
+ public OptionException(final Option option,
+ final String messageKey) {
this(option, messageKey, null);
}
@@ -70,40 +68,38 @@
* @param messageKey the id of the message to display
* @param value a value to display with the message
*/
- public OptionException(
- final Option option,
- final String messageKey,
- final String value) {
-
+ public OptionException(final Option option,
+ final String messageKey,
+ final String value) {
this.option = option;
-
+
if (messageKey != null) {
final StringBuffer buffer = new StringBuffer();
+
if (value != null) {
buffer.append(helper.getMessage(messageKey, value));
- }
- else {
+ } else {
buffer.append(helper.getMessage(messageKey));
}
+
buffer.append(" ");
option.appendUsage(buffer, HELP_SETTINGS, null);
message = buffer.toString();
- }
- else {
+ } else {
message = "";
}
}
-
+
/**
* Gets the Option the exception relates to
- *
+ *
* @return The related Option
*/
public Option getOption() {
return option;
}
-
+
public String getMessage() {
return message;
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2003-2004 The Apache Software Foundation
+/*
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,12 +21,13 @@
import org.apache.commons.cli2.Argument;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.option.Command;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* Builds Command instances
*/
public class CommandBuilder {
-
/** the preferred name of the command */
private String preferredName;
@@ -58,26 +59,18 @@
/**
* Creates a new <code>Command</code> instance using the properties of the
* <code>CommandBuilder</code>.
- *
+ *
* @return the new Command instance
*/
public Command create() {
-
// check we have a valid name
if (preferredName == null) {
- throw new IllegalStateException("Options must have at least one name");
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
}
// build the command
final Command option =
- new Command(
- preferredName,
- description,
- aliases,
- required,
- argument,
- children,
- id);
+ new Command(preferredName, description, aliases, required, argument, children, id);
// reset the builder
reset();
@@ -86,8 +79,8 @@
}
/**
- * Resets the CommandBuilder to the defaults for a new Command.
- *
+ * Resets the CommandBuilder to the defaults for a new Command.
+ *
* This method is called automatically at the end of the
* {@link #create() create} method.
*/
@@ -99,15 +92,16 @@
argument = null;
children = null;
id = 0;
+
return this;
}
/**
- * Specifies the name for the next <code>Command</code>
+ * Specifies the name for the next <code>Command</code>
* that is created. The first name is used as the preferred
- * display name for the <code>Command</code> and then
+ * display name for the <code>Command</code> and then
* later names are used as aliases.
- *
+ *
* @param name the name for the next <code>Command</code>
* that is created.
* @return this <code>CommandBuilder</code>.
@@ -115,8 +109,7 @@
public CommandBuilder withName(final String name) {
if (preferredName == null) {
preferredName = name;
- }
- else {
+ } else {
aliases.add(name);
}
@@ -125,15 +118,16 @@
/**
* Specifies the description for the next <code>Command</code>
- * that is created. This description is used to produce
+ * that is created. This description is used to produce
* help documentation for the <code>Command</code>.
- *
- * @param newDescription the description for the next
+ *
+ * @param newDescription the description for the next
* <code>Command</code> that is created.
* @return this <code>CommandBuilder</code>.
*/
public CommandBuilder withDescription(final String newDescription) {
this.description = newDescription;
+
return this;
}
@@ -146,43 +140,47 @@
*/
public CommandBuilder withRequired(final boolean newRequired) {
this.required = newRequired;
+
return this;
}
/**
- * Specifies the children for the next <code>Command</code>
+ * Specifies the children for the next <code>Command</code>
* that is created.
- *
+ *
* @param newChildren the child options for the next <code>Command</code>
* that is created.
* @return this <code>CommandBuilder</code>.
*/
public CommandBuilder withChildren(final Group newChildren) {
this.children = newChildren;
+
return this;
}
/**
- * Specifies the argument for the next <code>Command</code>
+ * Specifies the argument for the next <code>Command</code>
* that is created.
- *
- * @param newArgument the argument for the next <code>Command</code>
+ *
+ * @param newArgument the argument for the next <code>Command</code>
* that is created.
* @return this <code>CommandBuilder</code>.
*/
public CommandBuilder withArgument(final Argument newArgument) {
this.argument = newArgument;
+
return this;
}
/**
* Specifies the id for the next <code>Command</code> that is created.
- *
+ *
* @param newId the id for the next <code>Command</code> that is created.
* @return this <code>CommandBuilder</code>.
*/
public final CommandBuilder withId(final int newId) {
this.id = newId;
+
return this;
}
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2003-2004 The Apache Software Foundation
+/*
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,21 +21,20 @@
import org.apache.commons.cli2.Argument;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.option.DefaultOption;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* Builds DefaultOption instances.
*/
public class DefaultOptionBuilder {
-
private final String shortPrefix;
private final String longPrefix;
private final boolean burstEnabled;
-
private String preferredName;
private Set aliases;
private Set burstAliases;
private boolean required;
-
private String description;
private Argument argument;
private Group children;
@@ -48,10 +47,8 @@
* @see DefaultOption#DEFAULT_BURST_ENABLED
*/
public DefaultOptionBuilder() {
- this(
- DefaultOption.DEFAULT_SHORT_PREFIX,
- DefaultOption.DEFAULT_LONG_PREFIX,
- DefaultOption.DEFAULT_BURST_ENABLED);
+ this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX,
+ DefaultOption.DEFAULT_BURST_ENABLED);
}
/**
@@ -59,21 +56,21 @@
* @param shortPrefix the prefix to use for short options
* @param longPrefix the prefix to use for long options
* @param burstEnabled whether to allow gnu style bursting
- * @throws IllegalArgumentException if either prefix is less than on
+ * @throws IllegalArgumentException if either prefix is less than on
* character long
*/
- public DefaultOptionBuilder(
- final String shortPrefix,
- final String longPrefix,
- final boolean burstEnabled) throws IllegalArgumentException{
-
- if (shortPrefix == null || shortPrefix.length() == 0) {
- throw new IllegalArgumentException("shortPrefix should be at least 1 character long");
+ public DefaultOptionBuilder(final String shortPrefix,
+ final String longPrefix,
+ final boolean burstEnabled)
+ throws IllegalArgumentException {
+ if ((shortPrefix == null) || (shortPrefix.length() == 0)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX));
}
- if (longPrefix == null || longPrefix.length() == 0) {
- throw new IllegalArgumentException("longPrefix should be at least 1 character long");
+
+ if ((longPrefix == null) || (longPrefix.length() == 0)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX));
}
-
+
this.shortPrefix = shortPrefix;
this.longPrefix = longPrefix;
this.burstEnabled = burstEnabled;
@@ -85,30 +82,21 @@
* @return the new instance
* @throws IllegalStateException if no names have been supplied
*/
- public DefaultOption create() throws IllegalStateException {
+ public DefaultOption create()
+ throws IllegalStateException {
if (preferredName == null) {
- throw new IllegalStateException("Options must have at least one name");
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
}
final DefaultOption option =
- new DefaultOption(
- shortPrefix,
- longPrefix,
- burstEnabled,
- preferredName,
- description,
- aliases,
- burstAliases,
- required,
- argument,
- children,
- id);
+ new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description,
+ aliases, burstAliases, required, argument, children, id);
reset();
return option;
}
-
+
/**
* Resets the builder
*/
@@ -121,13 +109,14 @@
argument = null;
children = null;
id = 0;
+
return this;
}
/**
* Use this short option name. The first name is used as the preferred
* display name for the Command and then later names are used as aliases.
- *
+ *
* @param shortName the name to use
* @return this builder
*/
@@ -136,12 +125,11 @@
if (preferredName == null) {
preferredName = name;
- }
- else {
+ } else {
aliases.add(name);
}
- if (burstEnabled && name.length() == shortPrefix.length() + 1) {
+ if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) {
burstAliases.add(name);
}
@@ -151,18 +139,19 @@
/**
* Use this long option name. The first name is used as the preferred
* display name for the Command and then later names are used as aliases.
- *
+ *
* @param longName the name to use
* @return this builder
*/
public DefaultOptionBuilder withLongName(final String longName) {
final String name = longPrefix + longName;
+
if (preferredName == null) {
preferredName = name;
- }
- else {
+ } else {
aliases.add(name);
}
+
return this;
}
@@ -173,6 +162,7 @@
*/
public DefaultOptionBuilder withDescription(final String newDescription) {
this.description = newDescription;
+
return this;
}
@@ -183,6 +173,7 @@
*/
public DefaultOptionBuilder withRequired(final boolean newRequired) {
this.required = newRequired;
+
return this;
}
@@ -193,6 +184,7 @@
*/
public DefaultOptionBuilder withChildren(final Group newChildren) {
this.children = newChildren;
+
return this;
}
@@ -203,18 +195,20 @@
*/
public DefaultOptionBuilder withArgument(final Argument newArgument) {
this.argument = newArgument;
+
return this;
}
/**
* Sets the id
- *
+ *
* @param newId
* the id of the DefaultOption
* @return this DefaultOptionBuilder
*/
public final DefaultOptionBuilder withId(final int newId) {
this.id = newId;
+
return this;
}
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2003-2004 The Apache Software Foundation
+/*
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,15 +21,15 @@
import org.apache.commons.cli2.Argument;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.option.Switch;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* Builds Switch instance.
*/
public class SwitchBuilder {
-
private final String enabledPrefix;
private final String disabledPrefix;
-
private String description;
private String preferredName;
private Set aliases;
@@ -52,43 +52,37 @@
* Creates a new SwitchBuilder
* @param enabledPrefix the prefix to use for enabling the option
* @param disabledPrefix the prefix to use for disabling the option
- * @throws IllegalArgumentException if either prefix is less than 1
+ * @throws IllegalArgumentException if either prefix is less than 1
* character long or the prefixes match
*/
- public SwitchBuilder(
- final String enabledPrefix,
- final String disabledPrefix) throws IllegalArgumentException {
- if (enabledPrefix == null || enabledPrefix.length() < 1) {
- throw new IllegalArgumentException("enabledPrefix should be at least 1 character long");
+ public SwitchBuilder(final String enabledPrefix,
+ final String disabledPrefix)
+ throws IllegalArgumentException {
+ if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX));
}
- if (disabledPrefix == null || disabledPrefix.length() < 1) {
- throw new IllegalArgumentException("disabledPrefix should be at least 1 character long");
+
+ if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX));
}
+
if (enabledPrefix.equals(disabledPrefix)) {
- throw new IllegalArgumentException("disabledPrefix and enabledPrefix should be different");
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES));
}
+
this.enabledPrefix = enabledPrefix;
this.disabledPrefix = disabledPrefix;
reset();
}
-
+
/**
* Creates a new Switch instance
* @return a new Switch instance
*/
public Switch create() {
final Switch option =
- new Switch(
- enabledPrefix,
- disabledPrefix,
- preferredName,
- aliases,
- description,
- required,
- argument,
- children,
- id,
- switchDefault);
+ new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description,
+ required, argument, children, id, switchDefault);
reset();
@@ -107,6 +101,7 @@
children = null;
id = 0;
switchDefault = null;
+
return this;
}
@@ -117,21 +112,21 @@
*/
public SwitchBuilder withDescription(final String newDescription) {
this.description = newDescription;
+
return this;
}
/**
* Use this option name. The first name is used as the preferred
* display name for the Command and then later names are used as aliases.
- *
+ *
* @param name the name to use
* @return this builder
*/
public SwitchBuilder withName(final String name) {
if (preferredName == null) {
preferredName = name;
- }
- else {
+ } else {
aliases.add(name);
}
@@ -145,6 +140,7 @@
*/
public SwitchBuilder withRequired(final boolean newRequired) {
this.required = newRequired;
+
return this;
}
@@ -155,6 +151,7 @@
*/
public SwitchBuilder withArgument(final Argument newArgument) {
this.argument = newArgument;
+
return this;
}
@@ -165,29 +162,32 @@
*/
public SwitchBuilder withChildren(final Group newChildren) {
this.children = newChildren;
+
return this;
}
/**
* Sets the id
- *
+ *
* @param newId
* the id of the Switch
* @return this SwitchBuilder
*/
public final SwitchBuilder withId(final int newId) {
this.id = newId;
+
return this;
}
-
+
/**
* Sets the default state for this switch
- *
+ *
* @param newSwitchDefault the default state
* @return this SwitchBuilder
*/
public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) {
this.switchDefault = newSwitchDefault;
+
return this;
}
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2003-2004 The Apache Software Foundation
+/*
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,22 +21,24 @@
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* Instances of CommandLine represent a command line that has been processed
* according to the definition supplied to the parser.
*/
public abstract class CommandLineImpl implements CommandLine {
-
public final boolean hasOption(final String trigger) {
return hasOption(getOption(trigger));
}
public final List getValues(final String trigger) {
- return getValues(getOption(trigger),Collections.EMPTY_LIST);
+ return getValues(getOption(trigger), Collections.EMPTY_LIST);
}
- public final List getValues(final String trigger, final List defaultValues) {
+ public final List getValues(final String trigger,
+ final List defaultValues) {
return getValues(getOption(trigger), defaultValues);
}
@@ -45,10 +47,11 @@
}
public final Object getValue(final String trigger) {
- return getValue(getOption(trigger),null);
+ return getValue(getOption(trigger), null);
}
- public final Object getValue(final String trigger, final Object defaultValue) {
+ public final Object getValue(final String trigger,
+ final Object defaultValue) {
return getValue(getOption(trigger), defaultValue);
}
@@ -56,18 +59,18 @@
return getValue(option, null);
}
- public final Object getValue(final Option option, final Object defaultValue) {
-
+ public final Object getValue(final Option option,
+ final Object defaultValue) {
final List values;
+
if (defaultValue == null) {
values = getValues(option);
- }
- else {
+ } else {
values = getValues(option, Collections.singletonList(defaultValue));
}
if (values.size() > 1) {
- throw new IllegalStateException("More than one value was supplied");
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES));
}
if (values.isEmpty()) {
@@ -81,9 +84,8 @@
return getSwitch(getOption(trigger), null);
}
- public final Boolean getSwitch(
- final String trigger,
- final Boolean defaultValue) {
+ public final Boolean getSwitch(final String trigger,
+ final Boolean defaultValue) {
return getSwitch(getOption(trigger), defaultValue);
}
@@ -92,7 +94,7 @@
}
public final String getProperty(final String property) {
- return getProperty(property,null);
+ return getProperty(property, null);
}
public final int getOptionCount(final String trigger) {
@@ -105,6 +107,7 @@
}
int count = 0;
+
for (Iterator i = getOptions().iterator(); i.hasNext();) {
if (option.equals(i.next())) {
++count;
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java Mon Sep 12 19:32:04 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,7 @@
package org.apache.commons.cli2.commandline;
import java.io.IOException;
+
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
@@ -25,13 +26,14 @@
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
import org.apache.commons.cli2.util.HelpFormatter;
/**
* A class that implements the <code>Parser</code> interface can parse a
* String array according to the {@link Group}specified and return a
* {@link CommandLine}.
- *
+ *
* @author John Keyes (john at integralsource.com)
*/
public class Parser {
@@ -42,67 +44,67 @@
/**
* Parse the arguments according to the specified options and properties.
- *
+ *
* @param arguments
* the command line arguments
- *
+ *
* @return the list of atomic option and value tokens
* @throws OptionException
* if there are any problems encountered while parsing the
* command line tokens.
*/
- public CommandLine parse(final String[] arguments) throws OptionException {
-
+ public CommandLine parse(final String[] arguments)
+ throws OptionException {
// build a mutable list for the arguments
final List argumentList = new LinkedList();
-
+
// copy the arguments into the new list
for (int i = 0; i < arguments.length; i++) {
final String argument = arguments[i];
-
+
// ensure non intern'd strings are used
// so that == comparisons work as expected
argumentList.add(new String(argument));
}
-
+
// wet up a command line for this group
- final WriteableCommandLine commandLine =
- new WriteableCommandLineImpl(group, argumentList);
-
+ final WriteableCommandLine commandLine = new WriteableCommandLineImpl(group, argumentList);
+
// pick up any defaults from the model
group.defaults(commandLine);
-
+
// process the options as far as possible
final ListIterator iterator = argumentList.listIterator();
Object previous = null;
+
while (group.canProcess(commandLine, iterator)) {
-
// peek at the next item and backtrack
final Object next = iterator.next();
iterator.previous();
-
+
// if we have just tried to process this instance
- if(next==previous) {
+ if (next == previous) {
// abort
break;
}
+
// remember previous
previous = next;
-
+
group.process(commandLine, iterator);
}
-
+
// if there are more arguments we have a problem
if (iterator.hasNext()) {
- final String arg = (String)iterator.next();
- throw new OptionException(group, "cli.error.unexpected", arg);
+ final String arg = (String) iterator.next();
+ throw new OptionException(group, ResourceConstants.UNEXPECTED_TOKEN, arg);
}
-
+
// no need to validate if the help option is present
if (!commandLine.hasOption(helpOption) && !commandLine.hasOption(helpTrigger)) {
group.validate(commandLine);
}
-
+
return commandLine;
}
@@ -110,26 +112,23 @@
* Parse the arguments according to the specified options and properties and
* displays the usage screen if the CommandLine is not valid or the help
* option was specified.
- *
+ *
* @param arguments the command line arguments
* @return a valid CommandLine or null if the parse was unsuccessful
* @throws IOException if an error occurs while formatting help
*/
public CommandLine parseAndHelp(final String[] arguments)
throws IOException {
-
helpFormatter.setGroup(group);
try {
// attempt to parse the command line
final CommandLine commandLine = parse(arguments);
- if (!commandLine.hasOption(helpOption)
- && !commandLine.hasOption(helpTrigger)) {
+ if (!commandLine.hasOption(helpOption) && !commandLine.hasOption(helpTrigger)) {
return commandLine;
}
- }
- catch (final OptionException oe) {
+ } catch (final OptionException oe) {
// display help regarding the exception
helpFormatter.setException(oe);
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java Mon Sep 12 19:32:04 2005
@@ -1,5 +1,5 @@
-/**
- * Copyright 2004 The Apache Software Foundation
+/*
+ * Copyright 2004-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,13 +27,15 @@
import org.apache.commons.cli2.Argument;
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* A WriteableCommandLine implementation allowing Options to write their
* processed information to a CommandLine.
*/
-public class WriteableCommandLineImpl extends CommandLineImpl implements WriteableCommandLine {
-
+public class WriteableCommandLineImpl
+ extends CommandLineImpl implements WriteableCommandLine {
private final Properties properties = new Properties();
private final List options = new ArrayList();
private final Map nameToOption = new HashMap();
@@ -51,7 +53,8 @@
* @param rootOption the CommandLine's root Option
* @param arguments the arguments this CommandLine represents
*/
- public WriteableCommandLineImpl(final Option rootOption, final List arguments) {
+ public WriteableCommandLineImpl(final Option rootOption,
+ final List arguments) {
this.prefixes = rootOption.getPrefixes();
this.normalised = arguments;
}
@@ -59,55 +62,62 @@
public void addOption(Option option) {
options.add(option);
nameToOption.put(option.getPreferredName(), option);
+
for (Iterator i = option.getTriggers().iterator(); i.hasNext();) {
nameToOption.put(i.next(), option);
}
}
- public void addValue(final Option option, final Object value) {
+ public void addValue(final Option option,
+ final Object value) {
if (option instanceof Argument) {
addOption(option);
}
- List valueList = (List)values.get(option);
+
+ List valueList = (List) values.get(option);
+
if (valueList == null) {
valueList = new ArrayList();
values.put(option, valueList);
}
+
valueList.add(value);
}
- public void addSwitch(final Option option, final boolean value) {
+ public void addSwitch(final Option option,
+ final boolean value) {
addOption(option);
+
if (switches.containsKey(option)) {
- throw new IllegalStateException("Switch already set");
- }
- else {
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ALREADY_SET));
+ } else {
switches.put(option, value ? Boolean.TRUE : Boolean.FALSE);
}
}
public boolean hasOption(final Option option) {
final boolean present = options.contains(option);
+
return present;
}
-
+
public Option getOption(final String trigger) {
- return (Option)nameToOption.get(trigger);
+ return (Option) nameToOption.get(trigger);
}
- public List getValues(final Option option, final List defaultValues) {
-
+ public List getValues(final Option option,
+ final List defaultValues) {
// First grab the command line values
- List valueList = (List)values.get(option);
+ List valueList = (List) values.get(option);
// Secondly try the defaults supplied to the method
- if (valueList == null || valueList.isEmpty()) {
+ if ((valueList == null) || valueList.isEmpty()) {
valueList = defaultValues;
}
// Thirdly try the option's default values
- if (valueList == null || valueList.isEmpty()) {
- valueList = (List)this.defaultValues.get(option);
+ if ((valueList == null) || valueList.isEmpty()) {
+ valueList = (List) this.defaultValues.get(option);
}
// Finally use an empty list
@@ -117,10 +127,11 @@
return valueList;
}
-
- public Boolean getSwitch(final Option option, final Boolean defaultValue) {
+
+ public Boolean getSwitch(final Option option,
+ final Boolean defaultValue) {
// First grab the command line values
- Boolean bool = (Boolean)switches.get(option);
+ Boolean bool = (Boolean) switches.get(option);
// Secondly try the defaults supplied to the method
if (bool == null) {
@@ -129,27 +140,30 @@
// Thirdly try the option's default values
if (bool == null) {
- bool = (Boolean)this.defaultSwitches.get(option);
+ bool = (Boolean) this.defaultSwitches.get(option);
}
return bool;
}
- public void addProperty(final String property, final String value) {
+ public void addProperty(final String property,
+ final String value) {
properties.setProperty(property, value);
}
-
- public String getProperty(final String property, final String defaultValue) {
- return properties.getProperty(property,defaultValue);
+
+ public String getProperty(final String property,
+ final String defaultValue) {
+ return properties.getProperty(property, defaultValue);
}
public Set getProperties() {
return Collections.unmodifiableSet(properties.keySet());
}
-
+
public boolean looksLikeOption(final String trigger) {
for (final Iterator i = prefixes.iterator(); i.hasNext();) {
- final String prefix = (String)i.next();
+ final String prefix = (String) i.next();
+
if (trigger.startsWith(prefix)) {
return true;
}
@@ -162,15 +176,15 @@
final StringBuffer buffer = new StringBuffer();
// need to add group header
-
for (final Iterator i = normalised.iterator(); i.hasNext();) {
- final String arg = (String)i.next();
+ final String arg = (String) i.next();
+
if (arg.indexOf(' ') >= 0) {
buffer.append("\"").append(arg).append("\"");
- }
- else {
+ } else {
buffer.append(arg);
}
+
if (i.hasNext()) {
buffer.append(' ');
}
@@ -178,7 +192,7 @@
return buffer.toString();
}
-
+
public List getOptions() {
return Collections.unmodifiableList(options);
}
@@ -187,26 +201,25 @@
return Collections.unmodifiableSet(nameToOption.keySet());
}
- public void setDefaultValues(final Option option, final List defaults) {
- if (defaults==null) {
+ public void setDefaultValues(final Option option,
+ final List defaults) {
+ if (defaults == null) {
defaultValues.remove(option);
- }
- else {
+ } else {
defaultValues.put(option, defaults);
}
}
- public void setDefaultSwitch(final Option option, final Boolean defaultSwitch) {
- if (defaultSwitch==null) {
+ public void setDefaultSwitch(final Option option,
+ final Boolean defaultSwitch) {
+ if (defaultSwitch == null) {
defaultSwitches.remove(defaultSwitch);
- }
- else {
+ } else {
defaultSwitches.put(option, defaultSwitch);
}
}
-
+
public List getNormalised() {
return Collections.unmodifiableList(normalised);
}
-
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java Mon Sep 12 19:32:04 2005
@@ -28,6 +28,7 @@
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
import org.apache.commons.cli2.resource.ResourceHelper;
import org.apache.commons.cli2.validation.InvalidArgumentException;
import org.apache.commons.cli2.validation.Validator;
@@ -35,42 +36,40 @@
/**
* An implementation of an Argument.
*/
-public class ArgumentImpl extends OptionImpl 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 subsequentSplit;
- private final Validator validator;
- private final String consumeRemaining;
- private final List defaultValues;
- private final ResourceHelper resources
- = ResourceHelper.getResourceHelper(ArgumentImpl.class);
-
+public class ArgumentImpl
+ extends OptionImpl implements Argument {
private static final char NUL = '\0';
-
+
/**
* The default value for the initial separator char.
*/
public static final char DEFAULT_INITIAL_SEPARATOR = NUL;
-
+
/**
* The default value for the subsequent separator char.
*/
public static final char DEFAULT_SUBSEQUENT_SEPARATOR = NUL;
-
+
/**
* The default token to indicate that remaining arguments should be consumed
* as values.
*/
public static final String DEFAULT_CONSUME_REMAINING = "--";
+ 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 subsequentSplit;
+ private final Validator validator;
+ private final String consumeRemaining;
+ private final List defaultValues;
+ private final ResourceHelper resources = ResourceHelper.getResourceHelper();
/**
* Creates a new Argument instance.
- *
+ *
* @param name
* The name of the argument
* @param description
@@ -91,22 +90,20 @@
* The values to be used if none are specified.
* @param id
* The id of the option, 0 implies automatic assignment.
- *
+ *
* @see OptionImpl#OptionImpl(int,boolean)
*/
- public ArgumentImpl(
- final String name,
- final String description,
- final int minimum,
- final int maximum,
- final char initialSeparator,
- final char subsequentSeparator,
- final Validator validator,
- final String consumeRemaining,
- final List valueDefaults,
- final int id) {
-
- super(id,false);
+ public ArgumentImpl(final String name,
+ final String description,
+ final int minimum,
+ final int maximum,
+ final char initialSeparator,
+ final char subsequentSeparator,
+ final Validator validator,
+ final String consumeRemaining,
+ final List valueDefaults,
+ final int id) {
+ super(id, false);
this.name = (name == null) ? "arg" : name;
this.description = description;
@@ -120,19 +117,16 @@
this.defaultValues = valueDefaults;
if (minimum > maximum) {
- throw new IllegalArgumentException(
- resources.getMessage("cli.error.minimum.exceeds.maximum"));
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_MIN_EXCEEDS_MAX));
}
- if (valueDefaults != null && valueDefaults.size() > 0) {
+ if ((valueDefaults != null) && (valueDefaults.size() > 0)) {
if (valueDefaults.size() < minimum) {
- throw new IllegalArgumentException(
- resources.getMessage("cli.error.too.few.defaults"));
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_TOO_FEW_DEFAULTS));
}
if (valueDefaults.size() > maximum) {
- throw new IllegalArgumentException(
- resources.getMessage("cli.error.too.many.defaults"));
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_DEFAULTS));
}
}
}
@@ -141,20 +135,18 @@
return name;
}
- public void processValues(
- final WriteableCommandLine commandLine,
- final ListIterator arguments,
- final Option option)
+ public void processValues(final WriteableCommandLine commandLine,
+ final ListIterator arguments,
+ final Option option)
throws OptionException {
- int argumentCount = commandLine.getValues(option,Collections.EMPTY_LIST).size();
- while (arguments.hasNext() && argumentCount < maximum) {
+ int argumentCount = commandLine.getValues(option, Collections.EMPTY_LIST).size();
+
+ while (arguments.hasNext() && (argumentCount < maximum)) {
+ final String allValues = stripBoundaryQuotes((String) arguments.next());
- final String allValues =
- stripBoundaryQuotes((String)arguments.next());
-
// should we ignore things that look like options?
if (allValues.equals(consumeRemaining)) {
- while (arguments.hasNext() && argumentCount < maximum) {
+ while (arguments.hasNext() && (argumentCount < maximum)) {
++argumentCount;
commandLine.addValue(option, arguments.next());
}
@@ -162,29 +154,27 @@
// does it look like an option?
else if (commandLine.looksLikeOption(allValues)) {
arguments.previous();
+
break;
}
// should we split the string up?
else if (subsequentSplit) {
final StringTokenizer values =
- new StringTokenizer(
- allValues,
- String.valueOf(subsequentSepatator));
+ new StringTokenizer(allValues, String.valueOf(subsequentSepatator));
arguments.remove();
- while (values.hasMoreTokens() && argumentCount < maximum) {
+ while (values.hasMoreTokens() && (argumentCount < maximum)) {
++argumentCount;
+
final String token = values.nextToken();
commandLine.addValue(option, token);
arguments.add(token);
}
if (values.hasMoreTokens()) {
- throw new OptionException(
- option,
- "cli.error.unexpected.value",
- values.nextToken());
+ throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE,
+ values.nextToken());
}
}
// it must be a value as it is
@@ -195,7 +185,8 @@
}
}
- public boolean canProcess(final WriteableCommandLine commandLine, final String arg) {
+ public boolean canProcess(final WriteableCommandLine commandLine,
+ final String arg) {
return true;
}
@@ -203,7 +194,8 @@
return Collections.EMPTY_SET;
}
- public void process(WriteableCommandLine commandLine, ListIterator args)
+ public void process(WriteableCommandLine commandLine,
+ ListIterator args)
throws OptionException {
processValues(commandLine, args, this);
}
@@ -211,7 +203,7 @@
public char getInitialSeparator() {
return this.initialSeparator;
}
-
+
public Set getTriggers() {
return Collections.EMPTY_SET;
}
@@ -221,92 +213,89 @@
validate(commandLine, this);
}
- public void validate(
- final WriteableCommandLine commandLine,
- final Option option)
+ public void validate(final WriteableCommandLine commandLine,
+ final Option option)
throws OptionException {
-
final List values = commandLine.getValues(option);
if (values.size() < minimum) {
- throw new OptionException(option, "cli.error.missing.values");
+ throw new OptionException(option, ResourceConstants.ARGUMENT_MISSING_VALUES);
}
if (values.size() > maximum) {
- throw new OptionException(
- option,
- "cli.error.unexpected.value",
- (String)values.get(maximum));
+ throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE,
+ (String) values.get(maximum));
}
if (validator != null) {
try {
validator.validate(values);
- }
- catch (InvalidArgumentException ive) {
- throw new OptionException(
- option,
- "cli.error.unexpected.value",
- ive.getMessage());
+ } catch (InvalidArgumentException ive) {
+ throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE,
+ ive.getMessage());
}
}
}
- public void appendUsage(
- final StringBuffer buffer,
- final Set helpSettings,
- final Comparator comp) {
-
+ public void appendUsage(final StringBuffer buffer,
+ final Set helpSettings,
+ final Comparator comp) {
// do we display the outer optionality
- final boolean optional =
- helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
+ final boolean optional = helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
// allow numbering if multiple args
final boolean numbered =
- maximum > 1
- && helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED);
+ (maximum > 1) && helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED);
- final boolean bracketed =
- helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
+ final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
// if infinite args are allowed then crop the list
- final int max = maximum == Integer.MAX_VALUE ? 2 : maximum;
+ 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)) {
+ 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)) {
+ if ((i >= minimum) && (optional || (i > 0))) {
buffer.append(']');
}
}
@@ -316,11 +305,11 @@
return description;
}
- public List helpLines(
- final int depth,
- final Set helpSettings,
- final Comparator comp) {
+ public List helpLines(final int depth,
+ final Set helpSettings,
+ final Comparator comp) {
final HelpLine helpLine = new HelpLineImpl(this, depth);
+
return Collections.singletonList(helpLine);
}
@@ -335,17 +324,17 @@
/**
* If there are any leading or trailing quotes remove them from the
* specified token.
- *
+ *
* @param token
* the token to strip leading and trailing quotes
- *
+ *
* @return String the possibly modified token
*/
public String stripBoundaryQuotes(String token) {
if (!token.startsWith("\"") || !token.endsWith("\"")) {
return token;
}
-
+
if (token.startsWith("\"")) {
token = token.substring(1, token.length());
}
@@ -356,17 +345,18 @@
return token;
}
-
+
public boolean isRequired() {
- return getMinimum()>0;
+ return getMinimum() > 0;
}
-
+
public void defaults(final WriteableCommandLine commandLine) {
super.defaults(commandLine);
- defaultValues(commandLine,this);
+ defaultValues(commandLine, this);
}
-
- public void defaultValues(final WriteableCommandLine commandLine, final Option option) {
+
+ public void defaultValues(final WriteableCommandLine commandLine,
+ final Option option) {
commandLine.setDefaultValues(option, defaultValues);
}
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Command.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Command.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Command.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Command.java Mon Sep 12 19:32:04 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,15 +29,17 @@
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
/**
* Represents a cvs "update" style command line option.
- *
+ *
* Like all Parents, Commands can have child options and can be part of
* Arguments
*/
-public class Command extends ParentImpl {
-
+public class Command
+ extends ParentImpl {
/** The display name for the command */
private final String preferredName;
@@ -49,7 +51,7 @@
/**
* Creates a new Command instance.
- *
+ *
* @param preferredName
* The name normally used to refer to the Command
* @param description
@@ -64,31 +66,29 @@
* The Group of child options for this Command
* @param id
* A unique id for the Command
- *
+ *
* @see ParentImpl#ParentImpl(Argument, Group, String, int, boolean)
*/
- public Command(
- final String preferredName,
- final String description,
- final Set aliases,
- final boolean required,
- final Argument argument,
- final Group children,
- final int id) {
-
+ public Command(final String preferredName,
+ final String description,
+ final Set aliases,
+ final boolean required,
+ final Argument argument,
+ final Group children,
+ final int id) {
super(argument, children, description, id, required);
// check the preferred name is valid
- if (preferredName == null || preferredName.length() < 1) {
- throw new IllegalArgumentException("preferredName must be at least 1 character");
+ if ((preferredName == null) || (preferredName.length() < 1)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT));
}
this.preferredName = preferredName;
// gracefully and defensively handle aliases
- this.aliases = (aliases == null)
- ? Collections.EMPTY_SET
- : Collections.unmodifiableSet(new HashSet(aliases));
+ this.aliases =
+ (aliases == null) ? Collections.EMPTY_SET
+ : Collections.unmodifiableSet(new HashSet(aliases));
// populate the triggers Set
final Set newTriggers = new HashSet();
@@ -97,25 +97,21 @@
this.triggers = Collections.unmodifiableSet(newTriggers);
}
- public void processParent(
- final WriteableCommandLine commandLine,
- final ListIterator arguments)
+ public void processParent(final WriteableCommandLine commandLine,
+ final ListIterator arguments)
throws OptionException {
-
// grab the argument to process
- final String arg = (String)arguments.next();
+ final String arg = (String) arguments.next();
// if we can process it
if (canProcess(commandLine, arg)) {
-
// then note the option
commandLine.addOption(this);
// normalise the argument list
arguments.set(preferredName);
- }
- else {
- throw new OptionException(this, "cli.error.unexpected", arg);
+ } else {
+ throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);
}
}
@@ -126,26 +122,25 @@
public void validate(WriteableCommandLine commandLine)
throws OptionException {
if (isRequired() && !commandLine.hasOption(this)) {
- throw new OptionException(this,"cli.error.missing.required", getPreferredName());
+ throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,
+ getPreferredName());
}
super.validate(commandLine);
}
- public void appendUsage(
- final StringBuffer buffer,
- final Set helpSettings,
- final Comparator comp) {
-
+ public void appendUsage(final StringBuffer buffer,
+ final Set helpSettings,
+ final Comparator comp) {
// do we display optionality
final boolean optional =
!isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
- final boolean displayAliases =
- helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
+ final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
if (optional) {
buffer.append('[');
}
+
buffer.append(preferredName);
if (displayAliases && !aliases.isEmpty()) {
@@ -155,12 +150,14 @@
Collections.sort(list);
for (final Iterator i = list.iterator(); i.hasNext();) {
- final String alias = (String)i.next();
+ final String alias = (String) i.next();
buffer.append(alias);
+
if (i.hasNext()) {
buffer.append(',');
}
}
+
buffer.append(')');
}
Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/DefaultOption.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/DefaultOption.java?rev=280462&r1=280461&r2=280462&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/DefaultOption.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/DefaultOption.java Mon Sep 12 19:32:04 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,41 +29,39 @@
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
/**
* A Parent implementation representing normal options.
*/
-public class DefaultOption extends ParentImpl {
-
+public class DefaultOption
+ extends ParentImpl {
/**
* The default token used to prefix a short option
*/
public static final String DEFAULT_SHORT_PREFIX = "-";
-
+
/**
* The default token used to prefix a long option
*/
public static final String DEFAULT_LONG_PREFIX = "--";
-
+
/**
* The default value for the burstEnabled constructor parameter
*/
public static final boolean DEFAULT_BURST_ENABLED = true;
-
private final String preferredName;
private final Set aliases;
private final Set burstAliases;
private final Set triggers;
private final Set prefixes;
-
private final String shortPrefix;
private final boolean burstEnabled;
-
private final int burstLength;
/**
* Creates a new DefaultOption
- *
+ *
* @param shortPrefix the prefix used for short options
* @param longPrefix the prefix used for long options
* @param burstEnabled should option bursting be enabled
@@ -76,20 +74,19 @@
* @param children the Group children belonging to this Parent, ot null
* @param id the unique identifier for this Option
* @throws IllegalArgumentException if the preferredName or an alias isn't
- * prefixed with shortPrefix or longPrefix
+ * prefixed with shortPrefix or longPrefix
*/
- public DefaultOption(
- final String shortPrefix,
- final String longPrefix,
- final boolean burstEnabled,
- final String preferredName,
- final String description,
- final Set aliases,
- final Set burstAliases,
- final boolean required,
- final Argument argument,
- final Group children,
- final int id) {
+ public DefaultOption(final String shortPrefix,
+ final String longPrefix,
+ final boolean burstEnabled,
+ final String preferredName,
+ final String description,
+ final Set aliases,
+ final Set burstAliases,
+ final boolean required,
+ final Argument argument,
+ final Group children,
+ final int id) {
super(argument, children, description, id, required);
this.shortPrefix = shortPrefix;
@@ -98,13 +95,13 @@
this.burstLength = shortPrefix.length() + 1;
this.preferredName = preferredName;
- this.aliases = (aliases == null)
- ? Collections.EMPTY_SET
- : Collections.unmodifiableSet(new HashSet(aliases));
-
- this.burstAliases = (burstAliases == null)
- ? Collections.EMPTY_SET
- : Collections.unmodifiableSet(new HashSet(burstAliases));
+ this.aliases =
+ (aliases == null) ? Collections.EMPTY_SET
+ : Collections.unmodifiableSet(new HashSet(aliases));
+
+ this.burstAliases =
+ (burstAliases == null) ? Collections.EMPTY_SET
+ : Collections.unmodifiableSet(new HashSet(burstAliases));
final Set newTriggers = new HashSet();
newTriggers.add(preferredName);
@@ -116,29 +113,27 @@
newPrefixes.add(shortPrefix);
newPrefixes.add(longPrefix);
this.prefixes = Collections.unmodifiableSet(newPrefixes);
-
+
checkPrefixes(newPrefixes);
}
- public boolean canProcess(final WriteableCommandLine commandLine, final String argument) {
- return argument != null
- && (super.canProcess(commandLine, argument)
- || (argument.length() >= burstLength
- && burstAliases.contains(argument.substring(0, burstLength))));
+ public boolean canProcess(final WriteableCommandLine commandLine,
+ final String argument) {
+ return (argument != null) &&
+ (super.canProcess(commandLine, argument) ||
+ ((argument.length() >= burstLength) &&
+ burstAliases.contains(argument.substring(0, burstLength))));
}
- public void processParent(
- WriteableCommandLine commandLine,
- ListIterator arguments)
+ public void processParent(WriteableCommandLine commandLine,
+ ListIterator arguments)
throws OptionException {
-
- final String argument = (String)arguments.next();
+ final String argument = (String) arguments.next();
if (triggers.contains(argument)) {
commandLine.addOption(this);
arguments.set(preferredName);
- }
- else if (burstEnabled && argument.length() >= burstLength) {
+ } else if (burstEnabled && (argument.length() >= burstLength)) {
final String burst = argument.substring(0, burstLength);
if (burstAliases.contains(burst)) {
@@ -148,21 +143,17 @@
arguments.set(preferredName);
if (getArgument() == null) {
- arguments.add(
- shortPrefix + argument.substring(burstLength));
- }
- else {
+ arguments.add(shortPrefix + argument.substring(burstLength));
+ } else {
arguments.add(argument.substring(burstLength));
}
arguments.previous();
+ } else {
+ throw new OptionException(this, ResourceConstants.CANNOT_BURST, argument);
}
- else {
- throw new OptionException(this, "cli.error.burst", argument);
- }
- }
- else {
- throw new OptionException(this, "cli.error.unexpected", argument);
+ } else {
+ throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, argument);
}
}
@@ -177,26 +168,25 @@
public void validate(WriteableCommandLine commandLine)
throws OptionException {
if (isRequired() && !commandLine.hasOption(this)) {
- throw new OptionException(this,"cli.error.missing.required", getPreferredName());
+ throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,
+ getPreferredName());
}
super.validate(commandLine);
}
- public void appendUsage(
- final StringBuffer buffer,
- final Set helpSettings,
- final Comparator comp) {
-
+ public void appendUsage(final StringBuffer buffer,
+ final Set helpSettings,
+ final Comparator comp) {
// do we display optionality
final boolean optional =
!isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
- final boolean displayAliases =
- helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
+ final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
if (optional) {
buffer.append('[');
}
+
buffer.append(preferredName);
if (displayAliases && !aliases.isEmpty()) {
@@ -206,12 +196,14 @@
Collections.sort(list);
for (final Iterator i = list.iterator(); i.hasNext();) {
- final String alias = (String)i.next();
+ final String alias = (String) i.next();
buffer.append(alias);
+
if (i.hasNext()) {
buffer.append(',');
}
}
+
buffer.append(')');
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org