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 2003/10/29 02:28:17 UTC

cvs commit: jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2 CommandLine.java ArgumentImpl.java ArgumentBuilder.java

jkeyes      2003/10/28 17:28:17

  Modified:    cli/src/test/org/apache/commons/cli2 ArgumentTest.java
               cli/src/java/org/apache/commons/cli2 CommandLine.java
                        ArgumentImpl.java ArgumentBuilder.java
  Log:
  
- support for default value(s) at definition stage
  (Bug 24042)
  
  Revision  Changes    Path
  1.4       +70 -6     jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/ArgumentTest.java
  
  Index: ArgumentTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/ArgumentTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ArgumentTest.java	25 Oct 2003 15:02:05 -0000	1.3
  +++ ArgumentTest.java	29 Oct 2003 01:28:16 -0000	1.4
  @@ -61,6 +61,7 @@
   package org.apache.commons.cli2;
   
   import java.text.ParseException;
  +import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.HashSet;
   import java.util.Iterator;
  @@ -85,11 +86,12 @@
   			'\0',
   			'\0',
   			null,
  -            ArgumentImpl.DEFAULT_CONSUME_REMAINING);
  +            ArgumentImpl.DEFAULT_CONSUME_REMAINING,
  +            null);
   	}
   
   	public static Argument buildHostArgument() {
  -		return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null, null);
  +		return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null, null, null);
   	}
   
   	public static Argument buildPathArgument() {
  @@ -101,7 +103,8 @@
   			'=',
   			';',
   			null, 
  -		    ArgumentImpl.DEFAULT_CONSUME_REMAINING);
  +		    ArgumentImpl.DEFAULT_CONSUME_REMAINING,
  +            null);
   	}
   
   	public static Argument buildDateLimitArgument() {
  @@ -113,6 +116,7 @@
   			'=',
   			'\0',
   			new DateValidator(DateValidatorTest.YYYY_MM_YY), 
  +            null,
               null);
   	}
   
  @@ -125,6 +129,7 @@
   			'\0',
   			',',
   			null, 
  +            null,
               null);
   	}
       
  @@ -139,6 +144,7 @@
                   '=',
                   '\0',
                   new DateValidator(DateValidatorTest.YYYY_MM_YY), 
  +                null,
                   null);
           }
           catch(IllegalArgumentException e){
  @@ -146,6 +152,39 @@
           }
       }
   
  +    public static Argument buildSizeArgument() {
  +        List defaults = new ArrayList();
  +        defaults.add("10");
  +        
  +        return new ArgumentImpl(
  +            "size",
  +            "The number of units",
  +            1,
  +            1,
  +            '\0',
  +            '\0',
  +            null,
  +            ArgumentImpl.DEFAULT_CONSUME_REMAINING,
  +            defaults);
  +    }
  +
  +    public static Argument buildBoundsArgument() {
  +        List defaults = new ArrayList();
  +        defaults.add("5");
  +        defaults.add("10");
  +        
  +        return new ArgumentImpl(
  +            "size",
  +            "The number of units",
  +            2,
  +            2,
  +            '\0',
  +            '\0',
  +            null,
  +            ArgumentImpl.DEFAULT_CONSUME_REMAINING,
  +            defaults);
  +    }
  +
   	/* (non-Javadoc)
   	 * @see org.apache.commons.cli2.ArgumentTestCase#testPreProcess()
   	 */
  @@ -491,5 +530,30 @@
   
           assertTrue(commandLine.getValues(option).isEmpty());
           assertFalse(iterator.hasNext());
  +    }
  +    
  +    public void testProcess_DefaultValue() throws OptionException {
  +        final Option size = buildSizeArgument();
  +        final List args = list("-size");
  +        final CommandLine commandLine = commandLine(size, args);
  +        final ListIterator iterator = args.listIterator();
  +        
  +        size.process(commandLine, iterator);
  +        
  +        assertEquals(commandLine.getValue(size), "10");
  +    }
  +
  +    public void testProcess_DefaultValues() throws OptionException {
  +        final Option bounds = buildBoundsArgument();
  +        final List args = list("-bounds");
  +        final CommandLine commandLine = commandLine(bounds, args);
  +        final ListIterator iterator = args.listIterator();
  +        
  +        bounds.process(commandLine, iterator);
  +        
  +        List values = new ArrayList();
  +        values.add("5");
  +        values.add("10");
  +        assertEquals(values, commandLine.getValues(bounds));
       }
   }
  
  
  
  1.6       +41 -12    jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLine.java
  
  Index: CommandLine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLine.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CommandLine.java	24 Oct 2003 18:49:48 -0000	1.5
  +++ CommandLine.java	29 Oct 2003 01:28:17 -0000	1.6
  @@ -83,6 +83,7 @@
   	private final Map nameToOption = new HashMap();
   	private final Map values = new HashMap();
   	private final Map switches = new HashMap();
  +    private final Map defaults = new HashMap();
   	private final List normalised;
   	private final Set prefixes;
   
  @@ -139,8 +140,16 @@
   	public List getValues(final Option option) {
   		final List valueList = (List) values.get(option);
   		if (valueList == null) {
  -			return Collections.EMPTY_LIST;
  -		} else {
  +            if (this.defaults != null &&
  +                this.defaults.containsKey(option)) {
  +                    
  +                return (List) this.defaults.get(option);
  +            }
  +            else {
  +                return Collections.EMPTY_LIST;
  +            }
  +		} 
  +        else {
   			return valueList;
   		}
   	}
  @@ -150,16 +159,32 @@
   	}
   
   	public Object getValue(final Option option) {
  -		final List values = getValues(option);
  +		List values = getValues(option);
  +        
   		if (values == null || values.isEmpty()) {
  -			return null;
  -		} else if (values.size() > 1) {
  -			throw new IllegalStateException("More than one value was supplied");
  -		} else {
  -			return values.get(0);
  -		}
  +            
  +            if (this.defaults != null &&
  +                this.defaults.containsKey(option)) {
  +                    
  +                values = (List) this.defaults.get(option);
  +                return getValue(values);
  +            }
  +            else {            
  +    			return null;
  +            }
  +		} 
  +        else {
  +            return getValue(values);
  +        }
   	}
   
  +    private Object getValue(final List values) {
  +        if (values.size() > 1) {
  +            throw new IllegalStateException("More than one value was supplied");
  +        } 
  +        return values.get(0);
  +    }
  +    
   	public Boolean getSwitch(final String trigger) {
   		return getSwitch(getOption(trigger));
   	}
  @@ -234,6 +259,10 @@
           return count;
       }
   
  +    public void setDefaultValues(final Option option, final List values) {
  +        this.defaults.put(option, values);
  +    }
  +    
       public List getOptions(){
           return Collections.unmodifiableList(options);
       }
  
  
  
  1.8       +24 -7     jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java
  
  Index: ArgumentImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ArgumentImpl.java	25 Oct 2003 15:02:05 -0000	1.7
  +++ ArgumentImpl.java	29 Oct 2003 01:28:17 -0000	1.8
  @@ -87,6 +87,7 @@
       private final boolean subsequentSplit;
       private final Validator validator;
       private final String consumeRemaining;
  +    private final List defaultValues;
   
       private static final char NUL = '\0';
       public static final char DEFAULT_INITIAL_SEPARATOR = NUL;
  @@ -101,6 +102,7 @@
        * @param maximum The maximum number of values allowed to be valid
        * @param initialSeparator The char separating option from value
        * @param subsequentSeparator The char separating values from each other
  +     * @param consumeRemaining The String used for the "consuming option" group
        */
       public ArgumentImpl(
           final String name,
  @@ -110,7 +112,8 @@
           final char initialSeparator,
           final char subsequentSeparator,
           final Validator validator,
  -        final String consumeRemaining)
  +        final String consumeRemaining,
  +        final List defaultValues)
       {
   
           if (name == null)
  @@ -130,12 +133,22 @@
           this.subsequentSplit = subsequentSeparator != NUL;
           this.validator = validator;
           this.consumeRemaining = consumeRemaining;
  -
  +        this.defaultValues = defaultValues;
  +        
           if (minimum > maximum)
           {
               throw new IllegalArgumentException("minimum must not exceed maximum");
           }
  +        
  +        if (defaultValues != null) {
  +            if (defaultValues.size() < minimum) {
  +                throw new IllegalArgumentException("not enough default values");
  +            }
   
  +            if (defaultValues.size() > maximum) {
  +                throw new IllegalArgumentException("too many default values");
  +            }
  +        }
       }
   
       /* (non-Javadoc)
  @@ -230,7 +243,9 @@
               }
           }
           
  -        if (argumentCount < minimum || initialCount == argumentCount) {
  +        if (this.defaultValues == null && 
  +           (argumentCount < minimum || initialCount == argumentCount)) {
  +                
               throw new MissingValueException(option);
           }
       }
  @@ -257,7 +272,9 @@
       public void process(CommandLine commandLine, ListIterator args)
           throws OptionException
       {
  -
  +        if (this.defaultValues != null) {
  +            commandLine.setDefaultValues(this, this.defaultValues);
  +        }
           processValues(commandLine, args, this);
       }
   
  
  
  
  1.7       +60 -35    jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java
  
  Index: ArgumentBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ArgumentBuilder.java	25 Oct 2003 15:02:05 -0000	1.6
  +++ ArgumentBuilder.java	29 Oct 2003 01:28:17 -0000	1.7
  @@ -60,6 +60,9 @@
    */
   package org.apache.commons.cli2;
   
  +import java.util.ArrayList;
  +import java.util.List;
  +
   import org.apache.commons.cli2.validation.Validator;
   
   /**
  @@ -68,53 +71,40 @@
    */
   public class ArgumentBuilder
   {
  -    /**
  -     * Creates a new ArgumentBuilder instance
  -     */
  -    public ArgumentBuilder()
  -    {
  -        reset();
  -    }
  -
  -    /**
  -     * The name of the argument. Used for display and lookups in CommandLine
  -     */
  +    /** name of the argument. Used for display and lookups in CommandLine */
       private String name;
   
  -    /**
  -     * A description of the argument. Used in the automated online help
  -     */
  +    /** description of the argument. Used in the automated online help */
       private String description;
   
  -    /**
  -     * The minimum number of values needed for to be valid
  -     */
  +    /** minimum number of values required */
       private int minimum;
   
  -    /**
  -     * The maximum number of values needed for to be valid
  -     */
  +    /** maximum number of values permitted */
       private int maximum;
   
  -    /**
  -     * The character used to separate the values from the option
  -     */
  +    /** character used to separate the values from the option */
       private char initialSeparator;
   
  -    /** 
  -     * The character used to separate the values from each other
  -     */
  +    /** character used to separate the values from each other */
       private char subsequentSeparator;
   
  -    /** 
  -     * The validator object that should be used to ensure the values are valid
  -     */
  +    /** object that should be used to ensure the values are valid */
       private Validator validator;
       
  +    /** used to identify the consume remaining option, typically "--" */
  +    private String consumeRemaining;
  +    
  +    /** default values for argument */
  +    private List defaultValues;
  +
       /**
  -     * The string used for any consume remaining option, typically "--"
  +     * Creates a new ArgumentBuilder instance
        */
  -    private String consumeRemaining;
  +    public ArgumentBuilder()
  +    {
  +        reset();
  +    }
   
       /**
        * Creates a new Argument instance using the options specified in this
  @@ -133,7 +123,8 @@
                   initialSeparator,
                   subsequentSeparator,
                   validator,
  -                consumeRemaining);
  +                consumeRemaining,
  +                defaultValues);
   
           reset();
   
  @@ -154,6 +145,7 @@
           initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
           subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
           consumeRemaining = "--";
  +        defaultValues = null;
       }
   
       /**
  @@ -258,6 +250,39 @@
       public final ArgumentBuilder withConsumeRemaining(final String consumeRemaining)
       {
           this.consumeRemaining = consumeRemaining;
  +        return this;
  +    }
  +    
  +    /**
  +     * Sets the default value.
  +     * 
  +     * @param defaultValue
  +     *     the default value for the Argument
  +     * 
  +     * @return ArgumentBuilder
  +     *     this ArgumentBuilder 
  +     */
  +    public final ArgumentBuilder withDefault(final Object defaultValue)
  +    {
  +        if (this.defaultValues == null) {
  +            this.defaultValues = new ArrayList(1);
  +        }
  +        this.defaultValues.add(defaultValue);
  +        return this;
  +    }
  +    
  +    /**
  +     * Sets the default values.
  +     * 
  +     * @param defaultValues
  +     *     the default values for the Argument
  +     * 
  +     * @return ArgumentBuilder
  +     *     this ArgumentBuilder
  +     */
  +    public final ArgumentBuilder withDefaults(final List defaultValues)
  +    {
  +        this.defaultValues = defaultValues;
           return this;
       }
   }
  
  
  

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