You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/03/22 04:08:35 UTC

svn commit: r639943 [5/17] - in /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/resource/ jav...

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ParentImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ParentImpl.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ParentImpl.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ParentImpl.java Fri Mar 21 20:08:23 2008
@@ -1 +1,257 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.ListIterator;import java.util.Set;import 
 org.apache.commons.cli2.Argument;import org.apache.commons.cli2.DisplaySetting;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.Option;import org.apache.commons.cli2.OptionException;import org.apache.commons.cli2.Parent;import org.apache.commons.cli2.WriteableCommandLine;/** * A base implementation of Parent providing limited ground work for further * Parent implementations. */public abstract class ParentImpl    extends OptionImpl implements Parent {    private static final char NUL = '\0';    private final Group children;    private final Argument argument;    private final String description;    protected ParentImpl(final Argument argument,                         final Group children,                         final String description,                         final int id,                         final boolean required) {        super(id, required);        this.children = children;        this.argument = argument;        this.description = description;   
  }    /*     * (non-Javadoc)     *     * @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine,     *      java.util.ListIterator)     */    public void process(final WriteableCommandLine commandLine,                        final ListIterator arguments)        throws OptionException {        if (argument != null) {            handleInitialSeparator(arguments, argument.getInitialSeparator());        }        processParent(commandLine, arguments);        if (argument != null) {            argument.processValues(commandLine, arguments, this);        }        if ((children != null) && children.canProcess(commandLine, arguments)) {            children.process(commandLine, arguments);        }    }    /*     * (non-Javadoc)     *     * @see org.apache.commons.cli2.Option#canProcess(java.lang.String)     */    public boolean canProcess(final WriteableCommandLine commandLine,                              final String arg) {        final Set triggers = getTr
 iggers();        if (argument != null) {            final char separator = argument.getInitialSeparator();            // if there is a valid separator character            if (separator != NUL) {                final int initialIndex = arg.indexOf(separator);                // if there is a separator present                if (initialIndex > 0) {                    return triggers.contains(arg.substring(0, initialIndex));                }            }        }        return triggers.contains(arg);    }    /*     * (non-Javadoc)     *     * @see org.apache.commons.cli2.Option#prefixes()     */    public Set getPrefixes() {        return (children == null) ? Collections.EMPTY_SET : children.getPrefixes();    }    /*     * (non-Javadoc)     *     * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine)     */    public void validate(WriteableCommandLine commandLine)        throws OptionException {        if (commandLine.hasOption(this)) {            i
 f (argument != null) {                argument.validate(commandLine, this);            }            if (children != null) {                children.validate(commandLine);            }        }    }    /*     * (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) {        final boolean displayArgument =            (this.argument != null) &&            helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT);        final boolean displayChildren =            (this.children != null) &&            helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN);        if (displayArgument) {            buffer.append(' ');            argument.appendUsage(buffer, helpSettings, comp);        }        if (displayChildren) {       
      buffer.append(' ');            children.appendUsage(buffer, helpSettings, comp);        }    }    /**     * @return a description of this parent option     */    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 List helpLines = new ArrayList();        helpLines.add(new HelpLineImpl(this, depth));        if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) {            helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp));        }        if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) {            helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp));        
 }        return helpLines;    }    /**     * @return Returns the argument.     */    public Argument getArgument() {        return argument;    }    /**     * @return Returns the children.     */    public Group getChildren() {        return children;    }    /**     * Split the token using the specified separator character.     * @param arguments the current position in the arguments iterator     * @param separator the separator char to split on     */    private void handleInitialSeparator(final ListIterator arguments,                                        final char separator) {        // next token        final String newArgument = (String) arguments.next();        // split the token        final int initialIndex = newArgument.indexOf(separator);        if (initialIndex > 0) {            arguments.remove();            arguments.add(newArgument.substring(0, initialIndex));            String value = newArgument.substring(initialIndex + 1);            // The value obviousl
 y isn't an option, so we need to quote it if looks like an option.            // The quotes will be removed later            if (value.startsWith("-")) {                value = '"' + value + '"';            }            arguments.add(value);            arguments.previous();        }        arguments.previous();    }    /*     * @see org.apache.commons.cli2.Option#findOption(java.lang.String)     */    public Option findOption(final String trigger) {        final Option found = super.findOption(trigger);        if ((found == null) && (children != null)) {            return children.findOption(trigger);        } else {            return found;        }    }    public void defaults(final WriteableCommandLine commandLine) {        super.defaults(commandLine);        if (argument != null) {            argument.defaultValues(commandLine, this);        }        if (children != null) {            children.defaults(commandLine);        }    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.option;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.DisplaySetting;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.OptionException;
+import org.apache.commons.cli2.Parent;
+import org.apache.commons.cli2.WriteableCommandLine;
+
+/**
+ * A base implementation of Parent providing limited ground work for further
+ * Parent implementations.
+ */
+public abstract class ParentImpl
+    extends OptionImpl implements Parent {
+    private static final char NUL = '\0';
+    private final Group children;
+    private final Argument argument;
+    private final String description;
+
+    protected ParentImpl(final Argument argument,
+                         final Group children,
+                         final String description,
+                         final int id,
+                         final boolean required) {
+        super(id, required);
+        this.children = children;
+        this.argument = argument;
+        this.description = description;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine,
+     *      java.util.ListIterator)
+     */
+    public void process(final WriteableCommandLine commandLine,
+                        final ListIterator arguments)
+        throws OptionException {
+        if (argument != null) {
+            handleInitialSeparator(arguments, argument.getInitialSeparator());
+        }
+
+        processParent(commandLine, arguments);
+
+        if (argument != null) {
+            argument.processValues(commandLine, arguments, this);
+        }
+
+        if ((children != null) && children.canProcess(commandLine, arguments)) {
+            children.process(commandLine, arguments);
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.cli2.Option#canProcess(java.lang.String)
+     */
+    public boolean canProcess(final WriteableCommandLine commandLine,
+                              final String arg) {
+        final Set triggers = getTriggers();
+
+        if (argument != null) {
+            final char separator = argument.getInitialSeparator();
+
+            // if there is a valid separator character
+            if (separator != NUL) {
+                final int initialIndex = arg.indexOf(separator);
+
+                // if there is a separator present
+                if (initialIndex > 0) {
+                    return triggers.contains(arg.substring(0, initialIndex));
+                }
+            }
+        }
+
+        return triggers.contains(arg);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.cli2.Option#prefixes()
+     */
+    public Set getPrefixes() {
+        return (children == null) ? Collections.EMPTY_SET : children.getPrefixes();
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine)
+     */
+    public void validate(WriteableCommandLine commandLine)
+        throws OptionException {
+        if (commandLine.hasOption(this)) {
+            if (argument != null) {
+                argument.validate(commandLine, this);
+            }
+
+            if (children != null) {
+                children.validate(commandLine);
+            }
+        }
+    }
+
+    /*
+     * (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) {
+        final boolean displayArgument =
+            (this.argument != null) &&
+            helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
+        final boolean displayChildren =
+            (this.children != null) &&
+            helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN);
+
+        if (displayArgument) {
+            buffer.append(' ');
+            argument.appendUsage(buffer, helpSettings, comp);
+        }
+
+        if (displayChildren) {
+            buffer.append(' ');
+            children.appendUsage(buffer, helpSettings, comp);
+        }
+    }
+
+    /**
+     * @return a description of this parent option
+     */
+    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 List helpLines = new ArrayList();
+        helpLines.add(new HelpLineImpl(this, depth));
+
+        if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) {
+            helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp));
+        }
+
+        if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) {
+            helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp));
+        }
+
+        return helpLines;
+    }
+
+    /**
+     * @return Returns the argument.
+     */
+    public Argument getArgument() {
+        return argument;
+    }
+
+    /**
+     * @return Returns the children.
+     */
+    public Group getChildren() {
+        return children;
+    }
+
+    /**
+     * Split the token using the specified separator character.
+     * @param arguments the current position in the arguments iterator
+     * @param separator the separator char to split on
+     */
+    private void handleInitialSeparator(final ListIterator arguments,
+                                        final char separator) {
+        // next token
+        final String newArgument = (String) arguments.next();
+
+        // split the token
+        final int initialIndex = newArgument.indexOf(separator);
+
+        if (initialIndex > 0) {
+            arguments.remove();
+            arguments.add(newArgument.substring(0, initialIndex));
+            String value = newArgument.substring(initialIndex + 1);
+            // The value obviously isn't an option, so we need to quote it if looks like an option.
+            // The quotes will be removed later
+            if (value.startsWith("-")) {
+                value = '"' + value + '"';
+            }
+            arguments.add(value);
+            arguments.previous();
+        }
+
+        arguments.previous();
+    }
+
+    /*
+     * @see org.apache.commons.cli2.Option#findOption(java.lang.String)
+     */
+    public Option findOption(final String trigger) {
+        final Option found = super.findOption(trigger);
+
+        if ((found == null) && (children != null)) {
+            return children.findOption(trigger);
+        } else {
+            return found;
+        }
+    }
+
+    public void defaults(final WriteableCommandLine commandLine) {
+        super.defaults(commandLine);
+
+        if (argument != null) {
+            argument.defaultValues(commandLine, this);
+        }
+
+        if (children != null) {
+            children.defaults(commandLine);
+        }
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/PropertyOption.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/PropertyOption.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/PropertyOption.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/PropertyOption.java Fri Mar 21 20:08:23 2008
@@ -1 +1,167 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.ListIterator;import java.util.Set;import org.apache.commons.cli2.Dis
 playSetting;import org.apache.commons.cli2.HelpLine;import org.apache.commons.cli2.OptionException;import org.apache.commons.cli2.WriteableCommandLine;import org.apache.commons.cli2.resource.ResourceConstants;/** * Handles the java style "-Dprop=value" opions */public class PropertyOption    extends OptionImpl {    public static final String DEFAULT_OPTION_STRING = "-D";    public static final String DEFAULT_DESCRIPTION =        "Passes properties and values to the application";    /**     * A default PropertyOption instance     */    public static final PropertyOption INSTANCE = new PropertyOption();    private final String optionString;    private final String description;    private final Set prefixes;    /**     * Creates a new PropertyOption using the default settings of a "-D" trigger     * and an id of 'D'     */    public PropertyOption() {        this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D');    }    /**     * Creates a new PropertyOption using the specified
  parameters     * @param optionString the trigger for the Option     * @param description the description of the Option     * @param id the id of the Option     */    public PropertyOption(final String optionString,                          final String description,                          final int id) {        super(id, false);        this.optionString = optionString;        this.description = description;        this.prefixes = Collections.singleton(optionString);    }    public boolean canProcess(final WriteableCommandLine commandLine,                              final String argument) {        return (argument != null) && argument.startsWith(optionString) &&               (argument.length() > optionString.length());    }    public Set getPrefixes() {        return prefixes;    }    public void process(final WriteableCommandLine commandLine,                        final ListIterator arguments)        throws OptionException {        final String arg = (String) arguments
 .next();        if (!canProcess(commandLine, arg)) {            throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);        }        final int propertyStart = optionString.length();        final int equalsIndex = arg.indexOf('=', propertyStart);        final String property;        final String value;        if (equalsIndex < 0) {            property = arg.substring(propertyStart);            value = "true";        } else {            property = arg.substring(propertyStart, equalsIndex);            value = arg.substring(equalsIndex + 1);        }        commandLine.addProperty(property, value);    }    public Set getTriggers() {        return Collections.singleton(optionString);    }    public void validate(WriteableCommandLine commandLine) {        // PropertyOption needs no validation    }    public void appendUsage(final StringBuffer buffer,                            final Set helpSettings,                            final Comparator comp) {        f
 inal boolean display = helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION);        final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);        if (display) {            buffer.append(optionString);            if (bracketed) {                buffer.append('<');            }            buffer.append("property");            if (bracketed) {                buffer.append('>');            }            buffer.append("=");            if (bracketed) {                buffer.append('<');            }            buffer.append("value");            if (bracketed) {                buffer.append('>');            }        }    }    public String getPreferredName() {        return optionString;    }    public String getDescription() {        return description;    }    public List helpLines(final int depth,                          final Set helpSettings,                          final Comparator comp) {        if (helpSettings.contains(Display
 Setting.DISPLAY_PROPERTY_OPTION)) {            final HelpLine helpLine = new HelpLineImpl(this, depth);            return Collections.singletonList(helpLine);        } else {            return Collections.EMPTY_LIST;        }    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.option;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+
+import org.apache.commons.cli2.DisplaySetting;
+import org.apache.commons.cli2.HelpLine;
+import org.apache.commons.cli2.OptionException;
+import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.resource.ResourceConstants;
+
+/**
+ * Handles the java style "-Dprop=value" opions
+ */
+public class PropertyOption
+    extends OptionImpl {
+    public static final String DEFAULT_OPTION_STRING = "-D";
+    public static final String DEFAULT_DESCRIPTION =
+        "Passes properties and values to the application";
+
+    /**
+     * A default PropertyOption instance
+     */
+    public static final PropertyOption INSTANCE = new PropertyOption();
+    private final String optionString;
+    private final String description;
+    private final Set prefixes;
+
+    /**
+     * Creates a new PropertyOption using the default settings of a "-D" trigger
+     * and an id of 'D'
+     */
+    public PropertyOption() {
+        this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D');
+    }
+
+    /**
+     * Creates a new PropertyOption using the specified parameters
+     * @param optionString the trigger for the Option
+     * @param description the description of the Option
+     * @param id the id of the Option
+     */
+    public PropertyOption(final String optionString,
+                          final String description,
+                          final int id) {
+        super(id, false);
+        this.optionString = optionString;
+        this.description = description;
+        this.prefixes = Collections.singleton(optionString);
+    }
+
+    public boolean canProcess(final WriteableCommandLine commandLine,
+                              final String argument) {
+        return (argument != null) && argument.startsWith(optionString) &&
+               (argument.length() > optionString.length());
+    }
+
+    public Set getPrefixes() {
+        return prefixes;
+    }
+
+    public void process(final WriteableCommandLine commandLine,
+                        final ListIterator arguments)
+        throws OptionException {
+        final String arg = (String) arguments.next();
+
+        if (!canProcess(commandLine, arg)) {
+            throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);
+        }
+
+        final int propertyStart = optionString.length();
+        final int equalsIndex = arg.indexOf('=', propertyStart);
+        final String property;
+        final String value;
+
+        if (equalsIndex < 0) {
+            property = arg.substring(propertyStart);
+            value = "true";
+        } else {
+            property = arg.substring(propertyStart, equalsIndex);
+            value = arg.substring(equalsIndex + 1);
+        }
+
+        commandLine.addProperty(property, value);
+    }
+
+    public Set getTriggers() {
+        return Collections.singleton(optionString);
+    }
+
+    public void validate(WriteableCommandLine commandLine) {
+        // PropertyOption needs no validation
+    }
+
+    public void appendUsage(final StringBuffer buffer,
+                            final Set helpSettings,
+                            final Comparator comp) {
+        final boolean display = helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION);
+
+        final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
+
+        if (display) {
+            buffer.append(optionString);
+
+            if (bracketed) {
+                buffer.append('<');
+            }
+
+            buffer.append("property");
+
+            if (bracketed) {
+                buffer.append('>');
+            }
+
+            buffer.append("=");
+
+            if (bracketed) {
+                buffer.append('<');
+            }
+
+            buffer.append("value");
+
+            if (bracketed) {
+                buffer.append('>');
+            }
+        }
+    }
+
+    public String getPreferredName() {
+        return optionString;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public List helpLines(final int depth,
+                          final Set helpSettings,
+                          final Comparator comp) {
+        if (helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION)) {
+            final HelpLine helpLine = new HelpLineImpl(this, depth);
+
+            return Collections.singletonList(helpLine);
+        } else {
+            return Collections.EMPTY_LIST;
+        }
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/SourceDestArgument.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/SourceDestArgument.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/SourceDestArgument.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/SourceDestArgument.java Fri Mar 21 20:08:23 2008
@@ -1 +1,138 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.ArrayList;import java.util.Comparator;import java.util.Iterator;import java.util.List;import java.util.Set;import org.apache.commons.cli2.Argument;
 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;/** * An Argument implementation that allows a variable size Argument to precede a * fixed size argument.  The canonical example of it's use is in the unix * <code>cp</code> command where a number of source can be specified with * exactly one destination specfied at the end. */public class SourceDestArgument    extends ArgumentImpl {    private final Argument source;    private final Argument dest;    /**     * Creates a SourceDestArgument using defaults where possible.     *     * @param source the variable size Argument     * @param dest the fixed size Argument     */    public SourceDestArgument(final Argument source,                              final Argument dest) {        this(source, dest, DEFAULT_INITIAL_SEPARATOR, DEFAU
 LT_SUBSEQUENT_SEPARATOR,             DEFAULT_CONSUME_REMAINING, null);    }    /**     * Creates a SourceDestArgument using the specified parameters.     *     * @param source the variable size Argument     * @param dest the fixed size Argument     * @param initialSeparator the inistial separator to use     * @param subsequentSeparator the subsequent separator to use     * @param consumeRemaining the token triggering consume remaining behaviour     * @param defaultValues the default values for the SourceDestArgument     */    public SourceDestArgument(final Argument source,                              final Argument dest,                              final char initialSeparator,                              final char subsequentSeparator,                              final String consumeRemaining,                              final List defaultValues) {        super("SourceDestArgument", null, sum(source.getMinimum(), dest.getMinimum()),              sum(source.getMaximum()
 , dest.getMaximum()), initialSeparator, subsequentSeparator,              null, consumeRemaining, defaultValues, 0);        this.source = source;        this.dest = dest;        if (dest.getMinimum() != dest.getMaximum()) {            throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES));        }    }    private static int sum(final int a,                           final int b) {        return Math.max(a, Math.max(b, a + b));    }    public void appendUsage(final StringBuffer buffer,                            final Set helpSettings,                            final Comparator comp) {        final int length = buffer.length();        source.appendUsage(buffer, helpSettings, comp);        if (buffer.length() != length) {            buffer.append(' ');        }        dest.appendUsage(buffer, helpSettings, comp);    }    public List helpLines(int depth,                          Set helpSettings,   
                        Comparator comp) {        final List helpLines = new ArrayList();        helpLines.addAll(source.helpLines(depth, helpSettings, comp));        helpLines.addAll(dest.helpLines(depth, helpSettings, comp));        return helpLines;    }    public void validate(WriteableCommandLine commandLine,                         Option option)        throws OptionException {        final List values = commandLine.getValues(option);        final int limit = values.size() - dest.getMinimum();        int count = 0;        final Iterator i = values.iterator();        while (count++ < limit) {            commandLine.addValue(source, i.next());        }        while (i.hasNext()) {            commandLine.addValue(dest, i.next());        }        source.validate(commandLine, source);        dest.validate(commandLine, dest);    }    public boolean canProcess(final WriteableCommandLine commandLine,                              final String arg) {        return source.canProce
 ss(commandLine, arg) || dest.canProcess(commandLine, arg);    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.option;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+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;
+
+/**
+ * An Argument implementation that allows a variable size Argument to precede a
+ * fixed size argument.  The canonical example of it's use is in the unix
+ * <code>cp</code> command where a number of source can be specified with
+ * exactly one destination specfied at the end.
+ */
+public class SourceDestArgument
+    extends ArgumentImpl {
+    private final Argument source;
+    private final Argument dest;
+
+    /**
+     * Creates a SourceDestArgument using defaults where possible.
+     *
+     * @param source the variable size Argument
+     * @param dest the fixed size Argument
+     */
+    public SourceDestArgument(final Argument source,
+                              final Argument dest) {
+        this(source, dest, DEFAULT_INITIAL_SEPARATOR, DEFAULT_SUBSEQUENT_SEPARATOR,
+             DEFAULT_CONSUME_REMAINING, null);
+    }
+
+    /**
+     * Creates a SourceDestArgument using the specified parameters.
+     *
+     * @param source the variable size Argument
+     * @param dest the fixed size Argument
+     * @param initialSeparator the inistial separator to use
+     * @param subsequentSeparator the subsequent separator to use
+     * @param consumeRemaining the token triggering consume remaining behaviour
+     * @param defaultValues the default values for the SourceDestArgument
+     */
+    public SourceDestArgument(final Argument source,
+                              final Argument dest,
+                              final char initialSeparator,
+                              final char subsequentSeparator,
+                              final String consumeRemaining,
+                              final List defaultValues) {
+        super("SourceDestArgument", null, sum(source.getMinimum(), dest.getMinimum()),
+              sum(source.getMaximum(), dest.getMaximum()), initialSeparator, subsequentSeparator,
+              null, consumeRemaining, defaultValues, 0);
+
+        this.source = source;
+        this.dest = dest;
+
+        if (dest.getMinimum() != dest.getMaximum()) {
+            throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES));
+        }
+    }
+
+    private static int sum(final int a,
+                           final int b) {
+        return Math.max(a, Math.max(b, a + b));
+    }
+
+    public void appendUsage(final StringBuffer buffer,
+                            final Set helpSettings,
+                            final Comparator comp) {
+        final int length = buffer.length();
+
+        source.appendUsage(buffer, helpSettings, comp);
+
+        if (buffer.length() != length) {
+            buffer.append(' ');
+        }
+
+        dest.appendUsage(buffer, helpSettings, comp);
+    }
+
+    public List helpLines(int depth,
+                          Set helpSettings,
+                          Comparator comp) {
+        final List helpLines = new ArrayList();
+        helpLines.addAll(source.helpLines(depth, helpSettings, comp));
+        helpLines.addAll(dest.helpLines(depth, helpSettings, comp));
+
+        return helpLines;
+    }
+
+    public void validate(WriteableCommandLine commandLine,
+                         Option option)
+        throws OptionException {
+        final List values = commandLine.getValues(option);
+
+        final int limit = values.size() - dest.getMinimum();
+        int count = 0;
+
+        final Iterator i = values.iterator();
+
+        while (count++ < limit) {
+            commandLine.addValue(source, i.next());
+        }
+
+        while (i.hasNext()) {
+            commandLine.addValue(dest, i.next());
+        }
+
+        source.validate(commandLine, source);
+        dest.validate(commandLine, dest);
+    }
+
+    public boolean canProcess(final WriteableCommandLine commandLine,
+                              final String arg) {
+        return source.canProcess(commandLine, arg) || dest.canProcess(commandLine, arg);
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Switch.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Switch.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Switch.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/Switch.java Fri Mar 21 20:08:23 2008
@@ -1 +1,248 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.Iterator;import java.util.List;import 
 java.util.ListIterator;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.DisplaySetting;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;/** * A Parent implementation representing normal switch options. * For example: <code>+d|-d</code> or <code>--enable-x|--disable-x</code>. */public class Switch    extends ParentImpl {    /** i18n */    public static final ResourceHelper resources = ResourceHelper.getResourceHelper();    /**     * The default prefix for enabled switches     */    public static final String DEFAULT_ENABLED_PREFIX = "+";    /**     * The default prefix for disabled switches     */    public static final String DEFAULT_DISABLED_PREFIX = "-";    private final String enabledPrefix;    private final String disabledPrefix;   
  private final Set triggers;    private final String preferredName;    private final Set aliases;    private final Set prefixes;    private final Boolean defaultSwitch;    /**     * Creates a new Switch with the specified parameters     * @param enabledPrefix the prefix used for enabled switches     * @param disabledPrefix the prefix used for disabled switches     * @param preferredName the preferred name of the switch     * @param aliases the aliases by which the Switch is known     * @param description a description of the Switch     * @param required whether the Option is strictly required     * @param argument the Argument belonging to this Parent, or null     * @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 enabledPrefix or disabledPrefix     */    public Switch(final String enabledPrefix,      
             final String disabledPrefix,                  final String preferredName,                  final Set aliases,                  final String description,                  final boolean required,                  final Argument argument,                  final Group children,                  final int id,                  final Boolean switchDefault) {        super(argument, children, description, id, required);        if (enabledPrefix == null) {            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX));        }        if (disabledPrefix == null) {            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX));        }        if (enabledPrefix.startsWith(disabledPrefix)) {            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED));        }        if (disabledPrefix.startsWith(enabledPrefix)) {  
           throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED));        }        this.enabledPrefix = enabledPrefix;        this.disabledPrefix = disabledPrefix;        this.preferredName = preferredName;        if ((preferredName == null) || (preferredName.length() < 1)) {            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT));        }        final Set newTriggers = new HashSet();        newTriggers.add(enabledPrefix + preferredName);        newTriggers.add(disabledPrefix + preferredName);        this.triggers = Collections.unmodifiableSet(newTriggers);        if (aliases == null) {            this.aliases = Collections.EMPTY_SET;        } else {            this.aliases = Collections.unmodifiableSet(new HashSet(aliases));            for (final Iterator i = aliases.iterator(); i.hasNext();) {                final String alias = (String) i.next();       
          newTriggers.add(enabledPrefix + alias);                newTriggers.add(disabledPrefix + alias);            }        }        final Set newPrefixes = new HashSet(super.getPrefixes());        newPrefixes.add(enabledPrefix);        newPrefixes.add(disabledPrefix);        this.prefixes = Collections.unmodifiableSet(newPrefixes);        this.defaultSwitch = switchDefault;        checkPrefixes(newPrefixes);    }    public void processParent(final WriteableCommandLine commandLine,                              final ListIterator arguments)        throws OptionException {        final String arg = (String) arguments.next();        if (canProcess(commandLine, arg)) {            if (arg.startsWith(enabledPrefix)) {                commandLine.addSwitch(this, true);                arguments.set(enabledPrefix + preferredName);            }            if (arg.startsWith(disabledPrefix)) {                commandLine.addSwitch(this, false);                arguments.set(disabledPrefi
 x + preferredName);            }        } else {            throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);        }    }    public Set getTriggers() {        return triggers;    }    public Set getPrefixes() {        return prefixes;    }    public void validate(WriteableCommandLine commandLine)        throws OptionException {        if (isRequired() && !commandLine.hasOption(this)) {            throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,                                      getPreferredName());        }        super.validate(commandLine);    }    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(DisplaySett
 ing.DISPLAY_ALIASES);        final boolean disabled = helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_DISABLED);        final boolean enabled =            !disabled || helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_ENABLED);        final boolean both = disabled && enabled;        if (optional) {            buffer.append('[');        }        if (enabled) {            buffer.append(enabledPrefix).append(preferredName);        }        if (both) {            buffer.append('|');        }        if (disabled) {            buffer.append(disabledPrefix).append(preferredName);        }        if (displayAliases && !aliases.isEmpty()) {            buffer.append(" (");            final List list = new ArrayList(aliases);            Collections.sort(list);            for (final Iterator i = list.iterator(); i.hasNext();) {                final String alias = (String) i.next();                if (enabled) {                    buffer.append(enabledPrefix).append(alias);        
         }                if (both) {                    buffer.append('|');                }                if (disabled) {                    buffer.append(disabledPrefix).append(alias);                }                if (i.hasNext()) {                    buffer.append(',');                }            }            buffer.append(')');        }        super.appendUsage(buffer, helpSettings, comp);        if (optional) {            buffer.append(']');        }    }    public String getPreferredName() {        return enabledPrefix + preferredName;    }    public void defaults(final WriteableCommandLine commandLine) {        commandLine.setDefaultSwitch(this, defaultSwitch);    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.option;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.DisplaySetting;
+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;
+
+/**
+ * A Parent implementation representing normal switch options.
+ * For example: <code>+d|-d</code> or <code>--enable-x|--disable-x</code>.
+ */
+public class Switch
+    extends ParentImpl {
+    /** i18n */
+    public static final ResourceHelper resources = ResourceHelper.getResourceHelper();
+
+    /**
+     * The default prefix for enabled switches
+     */
+    public static final String DEFAULT_ENABLED_PREFIX = "+";
+
+    /**
+     * The default prefix for disabled switches
+     */
+    public static final String DEFAULT_DISABLED_PREFIX = "-";
+    private final String enabledPrefix;
+    private final String disabledPrefix;
+    private final Set triggers;
+    private final String preferredName;
+    private final Set aliases;
+    private final Set prefixes;
+    private final Boolean defaultSwitch;
+
+    /**
+     * Creates a new Switch with the specified parameters
+     * @param enabledPrefix the prefix used for enabled switches
+     * @param disabledPrefix the prefix used for disabled switches
+     * @param preferredName the preferred name of the switch
+     * @param aliases the aliases by which the Switch is known
+     * @param description a description of the Switch
+     * @param required whether the Option is strictly required
+     * @param argument the Argument belonging to this Parent, or null
+     * @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 enabledPrefix or disabledPrefix
+     */
+    public Switch(final String enabledPrefix,
+                  final String disabledPrefix,
+                  final String preferredName,
+                  final Set aliases,
+                  final String description,
+                  final boolean required,
+                  final Argument argument,
+                  final Group children,
+                  final int id,
+                  final Boolean switchDefault) {
+        super(argument, children, description, id, required);
+
+        if (enabledPrefix == null) {
+            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX));
+        }
+
+        if (disabledPrefix == null) {
+            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX));
+        }
+
+        if (enabledPrefix.startsWith(disabledPrefix)) {
+            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED));
+        }
+
+        if (disabledPrefix.startsWith(enabledPrefix)) {
+            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED));
+        }
+
+        this.enabledPrefix = enabledPrefix;
+        this.disabledPrefix = disabledPrefix;
+        this.preferredName = preferredName;
+
+        if ((preferredName == null) || (preferredName.length() < 1)) {
+            throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT));
+        }
+
+        final Set newTriggers = new HashSet();
+        newTriggers.add(enabledPrefix + preferredName);
+        newTriggers.add(disabledPrefix + preferredName);
+        this.triggers = Collections.unmodifiableSet(newTriggers);
+
+        if (aliases == null) {
+            this.aliases = Collections.EMPTY_SET;
+        } else {
+            this.aliases = Collections.unmodifiableSet(new HashSet(aliases));
+
+            for (final Iterator i = aliases.iterator(); i.hasNext();) {
+                final String alias = (String) i.next();
+                newTriggers.add(enabledPrefix + alias);
+                newTriggers.add(disabledPrefix + alias);
+            }
+        }
+
+        final Set newPrefixes = new HashSet(super.getPrefixes());
+        newPrefixes.add(enabledPrefix);
+        newPrefixes.add(disabledPrefix);
+        this.prefixes = Collections.unmodifiableSet(newPrefixes);
+
+        this.defaultSwitch = switchDefault;
+
+        checkPrefixes(newPrefixes);
+    }
+
+    public void processParent(final WriteableCommandLine commandLine,
+                              final ListIterator arguments)
+        throws OptionException {
+        final String arg = (String) arguments.next();
+
+        if (canProcess(commandLine, arg)) {
+            if (arg.startsWith(enabledPrefix)) {
+                commandLine.addSwitch(this, true);
+                arguments.set(enabledPrefix + preferredName);
+            }
+
+            if (arg.startsWith(disabledPrefix)) {
+                commandLine.addSwitch(this, false);
+                arguments.set(disabledPrefix + preferredName);
+            }
+        } else {
+            throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);
+        }
+    }
+
+    public Set getTriggers() {
+        return triggers;
+    }
+
+    public Set getPrefixes() {
+        return prefixes;
+    }
+
+    public void validate(WriteableCommandLine commandLine)
+        throws OptionException {
+        if (isRequired() && !commandLine.hasOption(this)) {
+            throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,
+                                      getPreferredName());
+        }
+
+        super.validate(commandLine);
+    }
+
+    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 disabled = helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_DISABLED);
+        final boolean enabled =
+            !disabled || helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_ENABLED);
+        final boolean both = disabled && enabled;
+
+        if (optional) {
+            buffer.append('[');
+        }
+
+        if (enabled) {
+            buffer.append(enabledPrefix).append(preferredName);
+        }
+
+        if (both) {
+            buffer.append('|');
+        }
+
+        if (disabled) {
+            buffer.append(disabledPrefix).append(preferredName);
+        }
+
+        if (displayAliases && !aliases.isEmpty()) {
+            buffer.append(" (");
+
+            final List list = new ArrayList(aliases);
+            Collections.sort(list);
+
+            for (final Iterator i = list.iterator(); i.hasNext();) {
+                final String alias = (String) i.next();
+
+                if (enabled) {
+                    buffer.append(enabledPrefix).append(alias);
+                }
+
+                if (both) {
+                    buffer.append('|');
+                }
+
+                if (disabled) {
+                    buffer.append(disabledPrefix).append(alias);
+                }
+
+                if (i.hasNext()) {
+                    buffer.append(',');
+                }
+            }
+
+            buffer.append(')');
+        }
+
+        super.appendUsage(buffer, helpSettings, comp);
+
+        if (optional) {
+            buffer.append(']');
+        }
+    }
+
+    public String getPreferredName() {
+        return enabledPrefix + preferredName;
+    }
+
+    public void defaults(final WriteableCommandLine commandLine) {
+        commandLine.setDefaultSwitch(this, defaultSwitch);
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java Fri Mar 21 20:08:23 2008
@@ -1 +1,70 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied.  See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.commons.cli2.resource;public abstract class ResourceConstants {    public static final String CLASSVALIDATOR_BAD_CLASSNAME = "ClassValidator.bad.classname";    public static final St
 ring CLASSVALIDATOR_CLASS_NOTFOUND = "ClassValidator.class.notfound";    public static final String CLASSVALIDATOR_CLASS_ACCESS = "ClassValidator.class.access";    public static final String CLASSVALIDATOR_CLASS_CREATE = "ClassValidator.class.create";    public static final String DATEVALIDATOR_DATE_OUTOFRANGE = "DateValidator.date.OutOfRange";    public static final String URLVALIDATOR_MALFORMED_URL = "URLValidator.malformed.URL";    public static final String NUMBERVALIDATOR_NUMBER_OUTOFRANGE =        "NumberValidator.number.OutOfRange";    public static final String ARGUMENT_UNEXPECTED_VALUE = "Argument.unexpected.value";    public static final String ARGUMENT_MIN_EXCEEDS_MAX = "Argument.minimum.exceeds.maximum";    public static final String ARGUMENT_TOO_FEW_DEFAULTS = "Argument.too.few.defaults";    public static final String ARGUMENT_TOO_MANY_DEFAULTS = "Argument.too.many.defaults";    public static final String ARGUMENT_MISSING_VALUES = "Argument.missing.values";    p
 ublic static final String ARGUMENT_TOO_MANY_VALUES = "Argument.too.many.values";    public static final String OPTION_TRIGGER_NEEDS_PREFIX = "Option.trigger.needs.prefix";    public static final String OPTION_MISSING_REQUIRED = "Option.missing.required";    public static final String OPTION_NO_NAME = "Option.no.name";    public static final String OPTION_ILLEGAL_LONG_PREFIX = "Option.illegal.long.prefix";    public static final String OPTION_ILLEGAL_SHORT_PREFIX = "Option.illegal.short.prefix";    public static final String UNEXPECTED_TOKEN = "Unexpected.token";    public static final String MISSING_OPTION = "Missing.option";    public static final String CANNOT_BURST = "Cannot.burst";    public static final String COMMAND_PREFERRED_NAME_TOO_SHORT = "Command.preferredName.too.short";    public static final String SWITCH_ILLEGAL_ENABLED_PREFIX = "Option.illegal.enabled.prefix";    public static final String SWITCH_ILLEGAL_DISABLED_PREFIX = "Option.illegal.disabled.prefix";   
  public static final String SWITCH_IDENTICAL_PREFIXES = "Option.identical.prefixes";    public static final String SWITCH_ALREADY_SET = "Switch.already.set";    public static final String SWITCH_NO_ENABLED_PREFIX = "Switch.no.enabledPrefix";    public static final String SWITCH_NO_DISABLED_PREFIX = "Switch.no.disabledPrefix";    public static final String SWITCH_ENABLED_STARTS_WITH_DISABLED =        "Switch.enabled.startsWith.disabled";    public static final String SWITCH_DISABLED_STARTWS_WITH_ENABLED =        "Switch.disabled.startsWith.enabled";    public static final String SWITCH_PREFERRED_NAME_TOO_SHORT = "Switch.preferredName.too.short";    public static final String SOURCE_DEST_MUST_ENFORCE_VALUES = "SourceDest.must.enforce.values";    public static final String HELPFORMATTER_GUTTER_TOO_LONG = "HelpFormatter.gutter.too.long";    public static final String HELPFORMATTER_WIDTH_TOO_NARROW = "HelpFormatter.width.too.narrow";    public static final String ENUM_ILLEGAL_VAL
 UE = "Enum.illegal.value";    public static final String ARGUMENT_BUILDER_NULL_CONSUME_REMAINING = "ArgumentBuilder.null.consume.remaining";    public static final String ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING = "ArgumentBuilder.empty.consume.remaining";    public static final String ARGUMENT_BUILDER_NULL_DEFAULT = "ArgumentBuilder.null.default";    public static final String ARGUMENT_BUILDER_NULL_DEFAULTS = "ArgumentBuilder.null.defaults";    public static final String ARGUMENT_BUILDER_NEGATIVE_MAXIMUM = "ArgumentBuilder.negative.maximum";    public static final String ARGUMENT_BUILDER_NEGATIVE_MINIMUM = "ArgumentBuilder.negative.minimum";    public static final String ARGUMENT_BUILDER_NULL_NAME = "ArgumentBuilder.null.name";    public static final String ARGUMENT_BUILDER_EMPTY_NAME = "ArgumentBuilder.empty.name";    public static final String ARGUMENT_BUILDER_NULL_VALIDATOR = "ArgumentBuilder.null.validator";}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.cli2.resource;
+
+public abstract class ResourceConstants {
+    public static final String CLASSVALIDATOR_BAD_CLASSNAME = "ClassValidator.bad.classname";
+    public static final String CLASSVALIDATOR_CLASS_NOTFOUND = "ClassValidator.class.notfound";
+    public static final String CLASSVALIDATOR_CLASS_ACCESS = "ClassValidator.class.access";
+    public static final String CLASSVALIDATOR_CLASS_CREATE = "ClassValidator.class.create";
+    public static final String DATEVALIDATOR_DATE_OUTOFRANGE = "DateValidator.date.OutOfRange";
+    public static final String URLVALIDATOR_MALFORMED_URL = "URLValidator.malformed.URL";
+    public static final String NUMBERVALIDATOR_NUMBER_OUTOFRANGE =
+        "NumberValidator.number.OutOfRange";
+    public static final String ARGUMENT_UNEXPECTED_VALUE = "Argument.unexpected.value";
+    public static final String ARGUMENT_MIN_EXCEEDS_MAX = "Argument.minimum.exceeds.maximum";
+    public static final String ARGUMENT_TOO_FEW_DEFAULTS = "Argument.too.few.defaults";
+    public static final String ARGUMENT_TOO_MANY_DEFAULTS = "Argument.too.many.defaults";
+    public static final String ARGUMENT_MISSING_VALUES = "Argument.missing.values";
+    public static final String ARGUMENT_TOO_MANY_VALUES = "Argument.too.many.values";
+    public static final String OPTION_TRIGGER_NEEDS_PREFIX = "Option.trigger.needs.prefix";
+    public static final String OPTION_MISSING_REQUIRED = "Option.missing.required";
+    public static final String OPTION_NO_NAME = "Option.no.name";
+    public static final String OPTION_ILLEGAL_LONG_PREFIX = "Option.illegal.long.prefix";
+    public static final String OPTION_ILLEGAL_SHORT_PREFIX = "Option.illegal.short.prefix";
+    public static final String UNEXPECTED_TOKEN = "Unexpected.token";
+    public static final String MISSING_OPTION = "Missing.option";
+    public static final String CANNOT_BURST = "Cannot.burst";
+    public static final String COMMAND_PREFERRED_NAME_TOO_SHORT = "Command.preferredName.too.short";
+    public static final String SWITCH_ILLEGAL_ENABLED_PREFIX = "Option.illegal.enabled.prefix";
+    public static final String SWITCH_ILLEGAL_DISABLED_PREFIX = "Option.illegal.disabled.prefix";
+    public static final String SWITCH_IDENTICAL_PREFIXES = "Option.identical.prefixes";
+    public static final String SWITCH_ALREADY_SET = "Switch.already.set";
+    public static final String SWITCH_NO_ENABLED_PREFIX = "Switch.no.enabledPrefix";
+    public static final String SWITCH_NO_DISABLED_PREFIX = "Switch.no.disabledPrefix";
+    public static final String SWITCH_ENABLED_STARTS_WITH_DISABLED =
+        "Switch.enabled.startsWith.disabled";
+    public static final String SWITCH_DISABLED_STARTWS_WITH_ENABLED =
+        "Switch.disabled.startsWith.enabled";
+    public static final String SWITCH_PREFERRED_NAME_TOO_SHORT = "Switch.preferredName.too.short";
+    public static final String SOURCE_DEST_MUST_ENFORCE_VALUES = "SourceDest.must.enforce.values";
+    public static final String HELPFORMATTER_GUTTER_TOO_LONG = "HelpFormatter.gutter.too.long";
+    public static final String HELPFORMATTER_WIDTH_TOO_NARROW = "HelpFormatter.width.too.narrow";
+    public static final String ENUM_ILLEGAL_VALUE = "Enum.illegal.value";
+    public static final String ARGUMENT_BUILDER_NULL_CONSUME_REMAINING = "ArgumentBuilder.null.consume.remaining";
+    public static final String ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING = "ArgumentBuilder.empty.consume.remaining";
+    public static final String ARGUMENT_BUILDER_NULL_DEFAULT = "ArgumentBuilder.null.default";
+    public static final String ARGUMENT_BUILDER_NULL_DEFAULTS = "ArgumentBuilder.null.defaults";
+    public static final String ARGUMENT_BUILDER_NEGATIVE_MAXIMUM = "ArgumentBuilder.negative.maximum";
+    public static final String ARGUMENT_BUILDER_NEGATIVE_MINIMUM = "ArgumentBuilder.negative.minimum";
+    public static final String ARGUMENT_BUILDER_NULL_NAME = "ArgumentBuilder.null.name";
+    public static final String ARGUMENT_BUILDER_EMPTY_NAME = "ArgumentBuilder.empty.name";
+    public static final String ARGUMENT_BUILDER_NULL_VALIDATOR = "ArgumentBuilder.null.validator";
+
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceHelper.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceHelper.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceHelper.java Fri Mar 21 20:08:23 2008
@@ -1 +1,160 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.resource;import java.text.MessageFormat;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;/** * A utility class used to pro
 vide internationalisation support. * * @author John Keyes */public class ResourceHelper {    /** system property */    private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";    /** default package name */    private static final String DEFAULT_BUNDLE =        "org.apache.commons.cli2.resource.CLIMessageBundle_en_US";    private static ResourceHelper helper;    /** resource bundle */    private ResourceBundle bundle;    private String prop;    /**     * Create a new ResourceHelper for the current locale.     */    private ResourceHelper() {        String bundleName = System.getProperty(PROP_LOCALE);        if (bundleName == null) {            bundleName = DEFAULT_BUNDLE;        }        this.prop = bundleName;        int firstUnderscore = bundleName.indexOf('_');        int secondUnderscore = bundleName.indexOf('_', firstUnderscore + 1);        Locale locale;        if (firstUnderscore != -1) {        String language = bundleName.substring(firstU
 nderscore + 1, secondUnderscore);        String country = bundleName.substring(secondUnderscore + 1);        	locale = new Locale(language, country);        }        else {        	locale = Locale.getDefault();        }        // initialize the bundle        try {            bundle = ResourceBundle.getBundle(bundleName, locale);        } catch (MissingResourceException exp) {            bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale);        }    }    public String getBundleName() {    	return this.prop;    }    /**     * Gets the ResourceHelper appropriate to the current locale.     * @return a ResourceHelper     */    public static ResourceHelper getResourceHelper() {        String bundleName = System.getProperty(PROP_LOCALE);        if (helper == null || !helper.getBundleName().equals(bundleName)) {            helper = new ResourceHelper();        }        return helper;    }    /**     * Returns the message for the specified key.     *     * @param key the uniq
 ue identifier of the message     * @return String the formatted String     */    public String getMessage(final String key) {        return getMessage(key, new Object[] {  });    }    /**     * Returns the message for the specified key and argument.     *     * @param key the unique identifier of the message     * @param value the argument value     * @return String the formatted String     */    public String getMessage(final String key,                             final Object value) {        return getMessage(key, new Object[] { value });    }    /**     * Returns the message for the specified key and arguments.     *     * @param key the unique identifier of the message     * @param value1 an argument value     * @param value2 an argument value     * @return String the formatted String     */    public String getMessage(final String key,                             final Object value1,                             final Object value2) {        return getMessage(key, new O
 bject[] { value1, value2 });    }    /**     * Returns the message for the specified key and arguments.     *     * @param key the unique identifier of the message     * @param value1 an argument value     * @param value2 an argument value     * @param value3 an argument value     *     * @return String the formatted String     */    public String getMessage(final String key,                             final Object value1,                             final Object value2,                             final Object value3) {        return getMessage(key, new Object[] { value1, value2, value3 });    }    /**     * Returns the message for the specified key and arguments.     *     * @param key the unique identifier of the message     * @param values argument values     * @return String the formatted String     */    public String getMessage(final String key,                             final Object[] values) {        final String msgFormatStr = bundle.getString(key);        final
  MessageFormat msgFormat = new MessageFormat(msgFormatStr);        return msgFormat.format(values);    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.resource;
+
+import java.text.MessageFormat;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ * A utility class used to provide internationalisation support.
+ *
+ * @author John Keyes
+ */
+public class ResourceHelper {
+    /** system property */
+    private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";
+
+    /** default package name */
+    private static final String DEFAULT_BUNDLE =
+        "org.apache.commons.cli2.resource.CLIMessageBundle_en_US";
+    private static ResourceHelper helper;
+
+    /** resource bundle */
+    private ResourceBundle bundle;
+
+    private String prop;
+    
+    /**
+     * Create a new ResourceHelper for the current locale.
+     */
+    private ResourceHelper() {
+        String bundleName = System.getProperty(PROP_LOCALE);
+
+        if (bundleName == null) {
+            bundleName = DEFAULT_BUNDLE;
+        }
+
+        this.prop = bundleName;
+        
+        int firstUnderscore = bundleName.indexOf('_');
+        int secondUnderscore = bundleName.indexOf('_', firstUnderscore + 1);
+
+        Locale locale;
+        if (firstUnderscore != -1) { 
+        String language = bundleName.substring(firstUnderscore + 1, secondUnderscore);
+        String country = bundleName.substring(secondUnderscore + 1);
+        	locale = new Locale(language, country);
+        }
+        else {
+        	locale = Locale.getDefault();
+        }
+        // initialize the bundle
+        try {
+            bundle = ResourceBundle.getBundle(bundleName, locale);
+        } catch (MissingResourceException exp) {
+            bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale);
+        }
+    }
+
+    public String getBundleName() {
+    	return this.prop;
+    }
+    
+    /**
+     * Gets the ResourceHelper appropriate to the current locale.
+     * @return a ResourceHelper
+     */
+    public static ResourceHelper getResourceHelper() {
+        String bundleName = System.getProperty(PROP_LOCALE);
+        if (helper == null || !helper.getBundleName().equals(bundleName)) {
+            helper = new ResourceHelper();
+        }
+
+        return helper;
+    }
+
+    /**
+     * Returns the message for the specified key.
+     *
+     * @param key the unique identifier of the message
+     * @return String the formatted String
+     */
+    public String getMessage(final String key) {
+        return getMessage(key, new Object[] {  });
+    }
+
+    /**
+     * Returns the message for the specified key and argument.
+     *
+     * @param key the unique identifier of the message
+     * @param value the argument value
+     * @return String the formatted String
+     */
+    public String getMessage(final String key,
+                             final Object value) {
+        return getMessage(key, new Object[] { value });
+    }
+
+    /**
+     * Returns the message for the specified key and arguments.
+     *
+     * @param key the unique identifier of the message
+     * @param value1 an argument value
+     * @param value2 an argument value
+     * @return String the formatted String
+     */
+    public String getMessage(final String key,
+                             final Object value1,
+                             final Object value2) {
+        return getMessage(key, new Object[] { value1, value2 });
+    }
+
+    /**
+     * Returns the message for the specified key and arguments.
+     *
+     * @param key the unique identifier of the message
+     * @param value1 an argument value
+     * @param value2 an argument value
+     * @param value3 an argument value
+     *
+     * @return String the formatted String
+     */
+    public String getMessage(final String key,
+                             final Object value1,
+                             final Object value2,
+                             final Object value3) {
+        return getMessage(key, new Object[] { value1, value2, value3 });
+    }
+
+    /**
+     * Returns the message for the specified key and arguments.
+     *
+     * @param key the unique identifier of the message
+     * @param values argument values
+     * @return String the formatted String
+     */
+    public String getMessage(final String key,
+                             final Object[] values) {
+        final String msgFormatStr = bundle.getString(key);
+        final MessageFormat msgFormat = new MessageFormat(msgFormatStr);
+
+        return msgFormat.format(values);
+    }
+}