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/06/06 21:29:30 UTC

cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli ParseRequiredTest.java

jkeyes      2003/06/06 12:29:30

  Modified:    cli/src/java/org/apache/commons/cli Tag: cli_1_x
                        OptionImpl.java
               cli/src/test/org/apache/commons/cli Tag: cli_1_x
                        ParseRequiredTest.java
  Log:
  o  added option validation
o  put the validation test back in
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.3   +77 -4     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/OptionImpl.java
  
  Index: OptionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/OptionImpl.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- OptionImpl.java	24 May 2003 22:20:53 -0000	1.1.2.2
  +++ OptionImpl.java	6 Jun 2003 19:29:30 -0000	1.1.2.3
  @@ -109,8 +109,24 @@
           final String description,
           final boolean required,
           final Set children) {
  -        this.name = name;
  -        this.longName = longName;
  +            
  +        // if the name and longName are null throw an exception
  +        if (name == null && longName == null) {
  +            throw new IllegalArgumentException("null names");
  +        }
  +        
  +        // short name processing
  +        if (name != null) {
  +            this.name = name;
  +            validateOption(this.name);
  +        }
  +        
  +        // long name processing
  +        if (longName != null) {
  +            this.longName = longName; 
  +            validateOption(this.longName);
  +        } 
  +        
           this.description = description;
           this.required = required;
           this.children = (children != null) ? children : new HashSet(0);
  @@ -172,4 +188,61 @@
           return (children != null && this.children.size() > 0);
       }
   
  +    /**
  +     * <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 {
  +        
  +        char[] chars = opt.toCharArray();
  +        
  +        for (int i = 0; i < chars.length; i++) {
  +            if (!isValidChar(chars[i])) {
  +                throw new IllegalArgumentException(
  +                    "opt contains illegal character value '"
  +                        + chars[i]
  +                        + "'");
  +            }
  +        }
  +    }
  +
  +    /**
  +     * <p>
  +     * Returns whether the specified character is a valid 
  +     * character for.
  +     * </p>
  +     *
  +     * @param c 
  +     *     the character to validate
  +     * 
  +     * @return boolean 
  +     *     true if <code>c</code> is a letter, ' ', '?' or '@', 
  +     *     otherwise false.
  +     */
  +    private boolean isValidChar(char c) {
  +        return (Character.isJavaIdentifierPart(c) || c == ' ' || c == '?' || c == '@' || c == '-');
  +    }
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.5.2.4   +6 -8      jakarta-commons/cli/src/test/org/apache/commons/cli/ParseRequiredTest.java
  
  Index: ParseRequiredTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ParseRequiredTest.java,v
  retrieving revision 1.5.2.3
  retrieving revision 1.5.2.4
  diff -u -r1.5.2.3 -r1.5.2.4
  --- ParseRequiredTest.java	4 Jun 2003 01:21:54 -0000	1.5.2.3
  +++ ParseRequiredTest.java	6 Jun 2003 19:29:30 -0000	1.5.2.4
  @@ -141,14 +141,12 @@
       public void testMissingRequiredOption() throws Exception {
           String[] args = new String[] { "-a" };
   
  -        //try
  -        //{
  -        CommandLine cl = parser.parse(_options, args);
  -        // fail( "exception should have been thrown" );
  -        //}
  -        //catch (MissingOptionException e)
  -        //{
  -        //}
  +        try {
  +            CommandLine cl = parser.parse(_options, args);
  +            fail( "exception should have been thrown" );
  +        }
  +        catch (MissingOptionException e) {
  +        }
       }
   
   }
  
  
  

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