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 2002/08/18 21:07:43 UTC

cvs commit: jakarta-commons/cli/src/java/org/apache/commons/cli CommandLine.java Option.java OptionBuilder.java

jkeyes      2002/08/18 12:07:43

  Modified:    cli/src/test/org/apache/commons/cli BugsTest.java
               cli/src/java/org/apache/commons/cli CommandLine.java
                        Option.java OptionBuilder.java
  Log:
  bug #11457: implemented fix, javadoc added to Option
  
  Revision  Changes    Path
  1.4       +19 -1     jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java
  
  Index: BugsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BugsTest.java	18 Aug 2002 15:52:23 -0000	1.3
  +++ BugsTest.java	18 Aug 2002 19:07:42 -0000	1.4
  @@ -37,6 +37,24 @@
       {
       }
   
  +    public void test11457() {
  +        Options options = new Options();
  +        options.addOption( OptionBuilder.withLongOpt( "verbose" )
  +                           .create() );
  +        String[] args = new String[] { "--verbose" };
  +
  +        CommandLineParser parser = CommandLineParserFactory.newParser();
  +
  +        try {
  +            CommandLine cmd = parser.parse( options, args );
  +            assertTrue( cmd.hasOption( "verbose" ) );
  +        }        
  +        catch( ParseException exp ) {
  +            exp.printStackTrace();
  +            fail( "Unexpected Exception: " + exp.getMessage() );
  +        }
  +    }
  +
       public void test11458()
       {
           Options options = new Options();
  
  
  
  1.9       +8 -4      jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java
  
  Index: CommandLine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CommandLine.java	15 Aug 2002 20:31:23 -0000	1.8
  +++ CommandLine.java	18 Aug 2002 19:07:42 -0000	1.9
  @@ -267,12 +267,16 @@
       void setOpt( Option opt ) {
           hashcodeMap.put( new Integer( opt.hashCode() ), opt );
   
  -        if( options.get( opt.getOpt() ) != null ) {
  -            ((java.util.List)options.get( opt.getOpt() )).add( opt );
  +        String key = opt.getOpt();
  +        if( " ".equals(key) ) {
  +            key = opt.getLongOpt();
  +        }
  +        if( options.get( key ) != null ) {
  +            ((java.util.List)options.get( key )).add( opt );
           }
           else {
  -            options.put( opt.getOpt(), new java.util.ArrayList() );
  -            ((java.util.List)options.get( opt.getOpt() ) ).add( opt );
  +            options.put( key, new java.util.ArrayList() );
  +            ((java.util.List)options.get( key ) ).add( opt );
           }
       }
   
  
  
  
  1.11      +128 -52   jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java
  
  Index: Option.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Option.java	15 Aug 2002 22:05:18 -0000	1.10
  +++ Option.java	18 Aug 2002 19:07:42 -0000	1.11
  @@ -90,56 +90,81 @@
    */
   
   public class Option implements Cloneable {
  +
  +    /** constant that specifies the number of argument values has not been specified */
  +    public final static int UNINITIALIZED = -1;
  +    
  +    /** constant that specifies the number of argument values is infinite */
  +    public final static int UNLIMITED_VALUES = -2;
       
       /** opt the single character representation of the option */
  -    private String opt = null;
  +    private String opt;
   
       /** longOpt is the long representation of the option */
  -    private String     longOpt      = null;
  +    private String longOpt;
   
       /** hasArg specifies whether this option has an associated argument */
  -    private boolean    hasArg       = false;
  +    private boolean hasArg;
   
       /** description of the option */
  -    private String     description  = null;
  +    private String description;
   
       /** required specifies whether this option is required to be present */
  -    private boolean    required     = false;
  +    private boolean required;
   
  -    private boolean optionalArg = false;
  +    /** specifies whether the argument value of this Option is optional */
  +    private boolean optionalArg;
   
       /** 
        * numberOfArgs specifies the number of argument values this option 
        * can have 
        */
  -    private int    numberOfArgs = UNINITIALIZED;   
  -
  -    /** number of arguments constants */
  -    public final static int UNINITIALIZED = -1;
  -    public final static int UNLIMITED_VALUES = -2;
  +    private int numberOfArgs = UNINITIALIZED;   
   
       /** the type of this Option */
  -    private Object     type         = null;   
  +    private Object type;
   
  -    /** ?? **/
  -    private ArrayList  values       = new ArrayList();
  +    /** the list of argument values **/
  +    private ArrayList values = new ArrayList();
       
       /** option char (only valid for single character options) */
       private char id;
   
  +    /** the character that is the value separator */
  +    private char valuesep;
  +
  +    /**
  +     * <p>Validates whether <code>opt</code> is a permissable Option
  +     * shortOpt.  The rules that specify if the <code>opt</code>
  +     * is valid are:</p>
  +     * <ul>
  +     *  <li><code>opt</code> is not NULL</li>
  +     *  <li>a single character <code>opt</code> that is either
  +     *  ' '(special case), '?', '@' or a letter</li>
  +     *  <li>a multi character <code>opt</code> that only contains
  +     *  letters.</li>
  +     * </ul>
  +     *
  +     * @param opt The option string to validate
  +     * @throws IllegalArgumentException if the Option is not valid.
  +     */
       private void validateOption( String opt ) 
       throws IllegalArgumentException
       {
  +        // check that opt is not NULL
           if( opt == null ) {
               throw new IllegalArgumentException( "opt is null" );
           }
  +        // handle the single character opt
           else if( opt.length() == 1 ) {
  -            if ( !isValidOpt( opt.charAt( 0 ) ) ) {
  +            char ch = opt.charAt( 0 );
  +            if ( !isValidOpt( ch ) ) {
                   throw new IllegalArgumentException( "illegal option value '" 
  -                                                    + opt.charAt( 0 ) + "'" );
  +                                                    + ch + "'" );
               }
  -            id = opt.charAt( 0 );
  +            id = ch;
           }
  +        // handle the multi character opt
           else {
               char[] chars = opt.toCharArray();
               for( int i = 0; i < chars.length; i++ ) {
  @@ -150,22 +175,33 @@
           }
       }
   
  -    private boolean isValidOpt( char c ) 
  -    {
  -        if ( ! ( isValidChar( c ) || c == '?' || c == '@') ) {
  -            return false;
  -        }
  -        return true;
  +    /**
  +     * <p>Returns whether the specified character is a valid Option.</p>
  +     *
  +     * @param c the option to validate
  +     * @return true if <code>c</code> is a letter, ' ', '?' or '@', otherwise false.
  +     */
  +    private boolean isValidOpt( char c ) {
  +        return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' );
       }
   
  -    private boolean isValidChar( char c ) 
  -    {
  -        if ( ! ( Character.isLetter( c ) ) ) {
  -            return false;
  -        }
  -        return true;
  +    /**
  +     * <p>Returns whether the specified character is a valid character.</p>
  +     *
  +     * @param c the character to validate
  +     * @return true if <code>c</code> is a letter.
  +     */
  +    private boolean isValidChar( char c ) {
  +        return Character.isLetter( c );
       }
   
  +    /**
  +     * <p>Returns the id of this Option.  This is only set when the
  +     * Option shortOpt is a single character.  This is used for switch
  +     * statements.</p>
  +     *
  +     * @return the id of this Option
  +     */
       public int getId( ) {
           return id;
       }
  @@ -177,10 +213,10 @@
        * @param hasArg specifies whether the Option takes an argument or not
        * @param description describes the function of the option
        */
  -    public Option(String opt, String description) 
  +    public Option( String opt, String description ) 
       throws IllegalArgumentException
       {
  -        this(opt, null, false, description);
  +        this( opt, null, false, description );
       }
   
       /**
  @@ -190,38 +226,41 @@
        * @param hasArg specifies whether the Option takes an argument or not
        * @param description describes the function of the option
        */
  -    public Option(String opt, boolean hasArg, String description) 
  +    public Option( String opt, boolean hasArg, String description ) 
       throws IllegalArgumentException
       {
  -        this(opt, null, hasArg, description);
  +        this( opt, null, hasArg, description );
       }
       
       /**
  -     * Creates an Option using the specified parameters.
  +     * <p>Creates an Option using the specified parameters.</p>
        *
        * @param opt short representation of the option
        * @param longOpt the long representation of the option
        * @param hasArg specifies whether the Option takes an argument or not
        * @param description describes the function of the option
        */
  -    public Option(String opt, String longOpt, boolean hasArg, String description) 
  +    public Option( String opt, String longOpt, boolean hasArg, String description ) 
       throws IllegalArgumentException
       {
  +        // ensure that the option is valid
           validateOption( opt );
   
           this.opt          = opt;
           this.longOpt      = longOpt;
   
  +        // if hasArg is set then the number of arguments is 1
           if( hasArg ) {
               this.numberOfArgs = 1;
           }
  +
           this.hasArg       = hasArg;
           this.description  = description;
       }
       
  -    /** <p>Retrieve the name of this Option</p>
  +    /** <p>Retrieve the name of this Option.</p>
        *
  -     * <p>It is this character which can be used with
  +     * <p>It is this String which can be used with
        * {@link CommandLine#hasOption(String opt)} and
        * {@link CommandLine#getOptionValue(String opt)} to check
        * for existence and argument.<p>
  @@ -233,7 +272,7 @@
       }
   
       /**
  -     * <p>Retrieve the type of this Option</p>
  +     * <p>Retrieve the type of this Option.</p>
        * 
        * @return The type of this option
        */
  @@ -241,11 +280,17 @@
           return this.type;
       }
   
  +    /**
  +     * <p>Sets the type of this Option.</p>
  +     *
  +     * @param type the type of this Option
  +     */
       public void setType( Object type ) {
           this.type = type;
       }
       
  -    /** <p>Retrieve the long name of this Option</p>
  +    /** 
  +     * <p>Retrieve the long name of this Option.</p>
        *
        * @return Long name of this option, or null, if there is no long name
        */
  @@ -253,12 +298,17 @@
           return this.longOpt;
       }
   
  +    /**
  +     * <p>Sets the long name of this Option.</p>
  +     *
  +     * @param longOpt the long name of this Option
  +     */
       public void setLongOpt( String longOpt ) {
           this.longOpt = longOpt;
       }
   
       /**
  -     * Sets whether this Option can have an optional argument.
  +     * <p>Sets whether this Option can have an optional argument.</p>
        *
        * @param optionalArg specifies whether the Option can have
        * an optional argument.
  @@ -301,7 +351,8 @@
           return this.description;
       }
   
  -     /** <p>Query to see if this Option requires an argument</p>
  +     /** 
  +      * <p>Query to see if this Option requires an argument</p>
         *
         * @return boolean flag indicating if an argument is required
         */
  @@ -309,11 +360,17 @@
            return this.required;
        }
   
  +     /**
  +      * <p>Sets whether this Option is mandatory.</p>
  +      *
  +      * @param required specifies whether this Option is mandatory
  +      */
        public void setRequired( boolean required ) {
            this.required = required;
        }
   
  -     /** <p>Query to see if this Option can take many values</p>
  +     /** 
  +      * <p>Query to see if this Option can take many values</p>
         *
         * @return boolean flag indicating if multiple values are allowed
         */
  @@ -321,7 +378,8 @@
            return ( this.numberOfArgs > 1 || this.numberOfArgs == UNLIMITED_VALUES );
        }
   
  -     /** <p>Sets the number of argument values this Option can take.</p>
  +     /** 
  +      * <p>Sets the number of argument values this Option can take.</p>
         *
         * @param num the number of argument values
         */
  @@ -329,17 +387,27 @@
            this.numberOfArgs = num;
        }
   
  -     private char valuesep;
  -
  +     /**
  +      * <p>Sets the value separator.  For example if the argument value
  +      * was a Java property, the value separator would be '='.</p>
  +      *
  +      * @param sep The value separator.
  +      */
        public void setValueSeparator( char sep ) {
            this.valuesep = sep;
        }
   
  +     /**
  +      * <p>Returns the value separator character.</p>
  +      *
  +      * @return the value separator character.
  +      */
        public char getValueSeparator() {
            return this.valuesep;
        }
   
  -     /** <p>Returns the number of argument values this Option can take.</p>
  +     /** 
  +      * <p>Returns the number of argument values this Option can take.</p>
         *
         * @return num the number of argument values
         */
  @@ -347,7 +415,8 @@
            return this.numberOfArgs;
        }
   
  -    /** <p>Dump state, suitable for debugging.</p>
  +    /** 
  +     * <p>Dump state, suitable for debugging.</p>
        *
        * @return Stringified form of this object
        */
  @@ -380,7 +449,7 @@
       }
   
       /**
  -     * Adds the specified value to this Option
  +     * <p>Adds the specified value to this Option.</p>
        * 
        * @param value is a/the value of this Option
        */
  @@ -420,7 +489,7 @@
   
       /**
        * @return the value/first value of this Option or 
  -     * null if there are no values.
  +     * <code>null</code> if there are no values.
        */
       public String getValue() {
           return this.values.size()==0 ? null : (String)this.values.get( 0 );
  @@ -428,7 +497,7 @@
   
       /**
        * @return the specified value of this Option or 
  -     * null if there are no values.
  +     * <code>null</code> if there are no values.
        */
       public String getValue( int index ) 
       throws IndexOutOfBoundsException
  @@ -446,17 +515,24 @@
       }
   
       /**
  -     * @return the values of this Option or null if there are no
  -     * values
  +     * @return the values of this Option as a String array 
  +     * or null if there are no values
        */
       public String[] getValues() {
           return this.values.size()==0 ? null : (String[])this.values.toArray(new String[]{});
       }
   
  +    /**
  +     * @return the values of this Option as a List
  +     * or null if there are no values
  +     */
       public java.util.List getValuesList() {
           return this.values;
       }
   
  +    /**
  +     * @return a copy of this Option
  +     */
       public Object clone() {
           Option option = new Option( getOpt(), getDescription() );
           option.setArgs( getArgs() );
  
  
  
  1.6       +13 -3     jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java
  
  Index: OptionBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- OptionBuilder.java	15 Aug 2002 22:05:18 -0000	1.5
  +++ OptionBuilder.java	18 Aug 2002 19:07:42 -0000	1.6
  @@ -300,4 +300,14 @@
           // return the Option instance
           return option;
       }
  +
  +    public static Option create() 
  +    throws IllegalArgumentException
  +    {
  +        if( longopt == null ) {
  +            throw new IllegalArgumentException( "must specify longopt" );
  +        }
  +
  +        return create( " " );
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>