You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2004/09/07 02:18:24 UTC

cvs commit: jakarta-commons/cli/src/java/org/apache/commons/cli2/builder DefaultOptionBuilder.java SwitchBuilder.java

roxspring    2004/09/06 17:18:24

  Modified:    cli/src/java/org/apache/commons/cli2/option
                        DefaultOption.java Switch.java messages.properties
                        OptionImpl.java
               cli/src/java/org/apache/commons/cli2/builder
                        DefaultOptionBuilder.java SwitchBuilder.java
  Log:
  Added checks and documentation to ensure preferredName and any aliases begin with a specified prefix
  Bug: 30979
  
  Revision  Changes    Path
  1.3       +5 -1      jakarta-commons/cli/src/java/org/apache/commons/cli2/option/DefaultOption.java
  
  Index: DefaultOption.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/DefaultOption.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultOption.java	22 Apr 2004 23:00:07 -0000	1.2
  +++ DefaultOption.java	7 Sep 2004 00:18:23 -0000	1.3
  @@ -67,7 +67,7 @@
        * @param shortPrefix the prefix used for short options
        * @param longPrefix the prefix used for long options
        * @param burstEnabled should option bursting be enabled
  -     * @param preferredName the preferred name for this Option
  +     * @param preferredName the preferred name for this Option, this should begin with either shortPrefix or longPrefix
        * @param description a description of this Option
        * @param aliases the alternative names for this Option
        * @param burstAliases the aliases that can be burst
  @@ -75,6 +75,8 @@
        * @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 shortPrefix or longPrefix 
        */
       public DefaultOption(
           final String shortPrefix,
  @@ -121,6 +123,8 @@
           newPrefixes.add(shortPrefix);
           newPrefixes.add(longPrefix);
           this.prefixes = Collections.unmodifiableSet(newPrefixes);
  +        
  +        checkPrefixes(newPrefixes);
       }
   
       public boolean canProcess(final String argument) {
  
  
  
  1.4       +4 -0      jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java
  
  Index: Switch.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Switch.java	6 Sep 2004 22:57:44 -0000	1.3
  +++ Switch.java	7 Sep 2004 00:18:23 -0000	1.4
  @@ -65,6 +65,8 @@
        * @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,
  @@ -126,6 +128,8 @@
           this.prefixes = Collections.unmodifiableSet(newPrefixes);
           
           this.defaultSwitch = switchDefault;
  +        
  +        checkPrefixes(newPrefixes);
       }
   
       public void processParent(
  
  
  
  1.3       +2 -0      jakarta-commons/cli/src/java/org/apache/commons/cli2/option/messages.properties
  
  Index: messages.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/messages.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- messages.properties	22 Apr 2004 23:00:07 -0000	1.2
  +++ messages.properties	7 Sep 2004 00:18:23 -0000	1.3
  @@ -1,3 +1,5 @@
   cli.error.minimum.exceeds.maximum = Minimum number of values must not exceed maximum number
   cli.error.too.few.defaults = Not enough default values.
   cli.error.too.many.defaults = Too many default values.
  +cli.error.trigger.needs.prefix = Trigger {0} must be prefixed with a value from {1}
  +cli.error.badproperty = Could not understand property: {0}
  
  
  
  1.4       +38 -0     jakarta-commons/cli/src/java/org/apache/commons/cli2/option/OptionImpl.java
  
  Index: OptionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/OptionImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- OptionImpl.java	6 Sep 2004 22:57:44 -0000	1.3
  +++ OptionImpl.java	7 Sep 2004 00:18:23 -0000	1.4
  @@ -15,11 +15,14 @@
    */
   package org.apache.commons.cli2.option;
   
  +import java.util.Iterator;
   import java.util.ListIterator;
  +import java.util.Set;
   
   import org.apache.commons.cli2.DisplaySetting;
   import org.apache.commons.cli2.Option;
   import org.apache.commons.cli2.WriteableCommandLine;
  +import org.apache.commons.cli2.resource.ResourceHelper;
   
   /**
    * A base implementation of Option providing limited ground work for further
  @@ -104,4 +107,39 @@
       public void defaults(final WriteableCommandLine commandLine) {
           // nothing to do normally
       }
  +    
  +    protected void checkPrefixes(final Set prefixes) {
  +        
  +        // nothing to do if empty prefix list
  +        if(prefixes.isEmpty()) {
  +            return;
  +        }
  +        
  +        // check preferred name
  +        checkPrefix(prefixes, getPreferredName());
  +        
  +        // check triggers
  +        this.getTriggers();
  +        for (final Iterator i = getTriggers().iterator(); i.hasNext();) {
  +            checkPrefix(prefixes, (String) i.next());
  +        }
  +    }
  +
  +    private void checkPrefix(final Set prefixes, final String trigger) {
  +        for (final Iterator i = prefixes.iterator(); i.hasNext();) {
  +            String prefix = (String) i.next();
  +            if(trigger.startsWith(prefix)) {
  +                return;
  +            }
  +        }
  +        
  +        final ResourceHelper helper = 
  +            ResourceHelper.getResourceHelper(OptionImpl.class);
  +        final String message = 
  +            helper.getMessage("cli.error.trigger.needs.prefix",
  +                    trigger,prefixes.toString());
  +        throw new IllegalArgumentException(message);
  +    }
  +    
  +    
   }
  
  
  
  1.3       +6 -2      jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java
  
  Index: DefaultOptionBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultOptionBuilder.java	22 Apr 2004 23:00:15 -0000	1.2
  +++ DefaultOptionBuilder.java	7 Sep 2004 00:18:24 -0000	1.3
  @@ -128,7 +128,9 @@
       }
   
       /**
  -     * Use this short option name
  +     * 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
        */
  @@ -150,7 +152,9 @@
       }
   
       /**
  -     * Use this long option name
  +     * 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
        */
  
  
  
  1.4       +3 -1      jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java
  
  Index: SwitchBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SwitchBuilder.java	6 Sep 2004 22:57:44 -0000	1.3
  +++ SwitchBuilder.java	7 Sep 2004 00:18:24 -0000	1.4
  @@ -120,7 +120,9 @@
       }
   
       /**
  -     * Use this option name
  +     * 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
        */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org