jstrachan    02/04/23 09:08:02

  Modified:    cli/src/java/org/apache/commons/cli Option.java Options.java
               cli/src/test/org/apache/commons/cli AllTest.java
  Added:       cli/src/java/org/apache/commons/cli
                        AlreadySelectedException.java
                        MissingOptionException.java OptionGroup.java
               cli/src/test/org/apache/commons/cli OptionGroupTest.java
                        ParseRequiredTest.java
  Log:
  Applied patches kindly submitted by John Keyes to support mandatory options and 
option groups along with new test cases
  
  Revision  Changes    Path
  1.2       +22 -9     
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java
  
  Index: Option.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Option.java       19 Dec 2001 18:16:25 -0000      1.1
  +++ Option.java       23 Apr 2002 16:08:02 -0000      1.2
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: Option.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
  + * $Id: Option.java,v 1.2 2002/04/23 16:08:02 jstrachan Exp $
    */
   
   package org.apache.commons.cli;
  @@ -23,7 +23,7 @@
    *
    * @author bob mcwhirter (bob @ werken.com)
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   
   public class Option {
  @@ -32,18 +32,23 @@
       private String        _longOpt     = null;
       private boolean       _hasArg      = false;
       private String        _description = null;
  +    private boolean       _required    = false;
       
  -    public Option(char opt, boolean required, String description) {
  -        _opt         = new Character( opt );
  -        _hasArg      = required;
  -        _description = description;
  +    public Option(char opt, boolean hasArg, String description) {
  +        this(opt, null, hasArg, description, false);
       }
       
  -    public Option(char opt, String longOpt, boolean required, String description) {
  +    public Option(char opt, String longOpt, boolean hasArg, String description) {
  +        this(opt, longOpt, hasArg, description, false);
  +    }
  +
  +    public Option(char opt, String longOpt, boolean hasArg, String description, 
  +                  boolean required) {
           _opt         = new Character( opt );
           _longOpt     = longOpt;
  -        _hasArg      = required;
  +        _hasArg      = hasArg;
           _description = description;
  +        _required    = required;
       }
       
       /** <p>Retrieve the single-character name of this Option</p>
  @@ -90,7 +95,15 @@
       public String getDescription() {
           return _description;
       }
  -    
  +
  +     /** <p>Query to see if this Option requires an argument</p>
  +      *
  +      * @return boolean flag indicating if an argument is required
  +      */
  +     public boolean isRequired() {
  +         return _required;
  +     }
  +
       /** <p>Dump state, suitable for debugging.</p>
        *
        * @return Stringified form of this object
  
  
  
  1.2       +95 -21    
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java
  
  Index: Options.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Options.java      19 Dec 2001 18:16:25 -0000      1.1
  +++ Options.java      23 Apr 2002 16:08:02 -0000      1.2
  @@ -5,15 +5,17 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: Options.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
  + * $Id: Options.java,v 1.2 2002/04/23 16:08:02 jstrachan Exp $
    */
   
   package org.apache.commons.cli;
   
   import java.util.Map;
  +import java.util.Set;
   import java.util.HashMap;
   import java.util.List;
   import java.util.ArrayList;
  +import java.util.Arrays;
   import java.util.LinkedList;
   import java.util.Iterator;
   import java.util.Collections;
  @@ -31,19 +33,34 @@
    *
    * @author bob mcwhirter (bob @ werken.com)
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class Options {
       
  -    private List _options   = new ArrayList();
  -    private Map  _shortOpts = new HashMap();
  -    private Map  _longOpts  = new HashMap();
  +    private List _options      = new ArrayList();
  +    private Map  _shortOpts    = new HashMap();
  +    private Map  _longOpts     = new HashMap();
  +    private Map  _requiredOpts = new HashMap();
       
  +    private Map _optionGroups  = new HashMap();
  +
       /** <p>Construct a new Options descriptor</p>
        */
       public Options() {        
       }
  -    
  +
  +    public Options addOptionGroup( OptionGroup group )
  +    {
  +        Iterator options = group.getOptions().iterator();
  +        while( options.hasNext() )
  +        {
  +            Option option = (Option)options.next();
  +            addOption( option );
  +            _optionGroups.put( option, group );
  +        }
  +        return this;
  +    }
  +
       /** <p>Add an option that only contains a short-name</p>
        * <p>It may be specified as requiring an argument.</p>
        *
  @@ -52,7 +69,7 @@
        * @param description Self-documenting description
        */
       public Options addOption(char opt, boolean hasArg, String description) {
  -        addOption( new Option(opt, hasArg, description) );        
  +        addOption( opt, null, hasArg, description, false );
           return this;
       }
       
  @@ -64,17 +81,31 @@
        * @param description Self-documenting description
        */
       public Options addOption(char opt, String longOpt, boolean hasArg, String 
description) {
  -        addOption( new Option(opt, longOpt, hasArg, description) );        
  +        addOption( opt, longOpt, hasArg, description, false );        
           return this;
       }
  -    
  +
  +    /** <p>Add an option that contains a short-name and a long-name</p>
  +     * <p>It may be specified as requiring an argument.</p>
  +     *
  +     * @param opt Short single-character name of the option.
  +     * @param hasArg flag signally if an argument is required after this option
  +     * @param description Self-documenting description
  +     */
  +    public Options addOption(char opt, String longOpt, boolean hasArg, String 
description,
  +                             boolean required) {
  +        addOption( new Option(opt, longOpt, hasArg, description, required) );       
 
  +        return this;
  +    }
  +
       /** <p>Parse the given list of arguments against this descriptor<p>
        *
        * @param args Args to parse
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(String[] args) throws MissingArgumentException, 
UnrecognizedOptionException {
  +    public CommandLine parse(String[] args) 
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
           return parse( args, 0, args.length, false);
       }
       
  @@ -101,7 +132,8 @@
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(String[] args, boolean stopAtNonOption) throws 
MissingArgumentException, UnrecognizedOptionException {
  +    public CommandLine parse(String[] args, boolean stopAtNonOption) 
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
           return parse( args, 0, args.length, stopAtNonOption);
       }
       
  @@ -117,7 +149,8 @@
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(String[] args, int fromIndex, int toIndex) throws 
MissingArgumentException, UnrecognizedOptionException {
  +    public CommandLine parse(String[] args, int fromIndex, int toIndex) 
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
           return parse( args, fromIndex, toIndex, false );
       }
       
  @@ -150,12 +183,9 @@
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(String[] args, int fromIndex, int toIndex, boolean 
stopAtNonOption) throws MissingArgumentException, UnrecognizedOptionException {
  -        List argList = new ArrayList();
  -        
  -        for ( int i = fromIndex; i < toIndex ; ++i ) {
  -            argList.add( args[i] );
  -        }
  +    public CommandLine parse(String[] args, int fromIndex, int toIndex, boolean 
stopAtNonOption) 
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
  +        List argList = java.util.Arrays.asList( args );
           
           return parse( argList, stopAtNonOption);
       }
  @@ -166,7 +196,8 @@
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(List args) throws MissingArgumentException, 
UnrecognizedOptionException {
  +    public CommandLine parse(List args)
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
           return parse( args, false );
       }
       
  @@ -197,7 +228,8 @@
        *
        * @returns {@link CommandLine} containing information related to parse state
        */
  -    public CommandLine parse(List inArgs, boolean stopAtNonOption) throws 
MissingArgumentException, UnrecognizedOptionException {
  +    public CommandLine parse(List inArgs, boolean stopAtNonOption) 
  +    throws MissingArgumentException, UnrecognizedOptionException, 
MissingOptionException, AlreadySelectedException {
           CommandLine cl = new CommandLine();
           
           List args = burst( inArgs,
  @@ -225,6 +257,16 @@
                       // cl.addArg( eachArg );
                   }
                   else {
  +                    
  +                    if ( _optionGroups.get( eachOpt ) != null ) {
  +                        ( (OptionGroup)( _optionGroups.get( eachOpt ) ) 
).setSelected( eachOpt );
  +                    }
  +                    
  +                    // if required remove from list
  +                    if ( eachOpt.isRequired() ) {
  +                        _requiredOpts.remove( "-" + eachOpt.getOpt() );
  +                    }
  +
                       if ( eachOpt.hasArg() ) {
                           if ( argIter.hasNext() ) {
                               eachArg = (String) argIter.next();
  @@ -256,12 +298,21 @@
               else if ( eachArg.startsWith("-") ) {
                   eachOpt = (Option) _shortOpts.get( eachArg );
                   
  +                if ( _optionGroups.get( eachOpt ) != null ) {
  +                    ( (OptionGroup)( _optionGroups.get( eachOpt ) ) ).setSelected( 
eachOpt );
  +                }
  +
                   if ( eachOpt == null ) {
                       throw new UnrecognizedOptionException("Unrecognized option: " + 
eachArg);
                       // maybe someone will parse these args later
                       // cl.addArg( eachArg );
                   }
                   else {
  +                    // if required remove from list
  +                    if ( eachOpt.isRequired() ) {
  +                        _requiredOpts.remove( "-" + eachOpt.getOpt() );
  +                    }
  +
                       if ( eachOpt.hasArg() ) {
                           if ( argIter.hasNext() ) {
                               eachArg = (String) argIter.next();
  @@ -293,7 +344,26 @@
                   }
               }
           }
  -        
  +
  +        if( _requiredOpts.size() > 0 )
  +        {
  +            Set optKeys = _requiredOpts.keySet();
  +
  +            Iterator iter = optKeys.iterator();
  +
  +            StringBuffer buff = new StringBuffer();
  +
  +            while( iter.hasNext() )
  +            {
  +                Option missing = (Option)_requiredOpts.get( iter.next() );
  +                buff.append( "-" );
  +                buff.append( missing.getOpt() );
  +                buff.append( " " );
  +                buff.append( missing.getDescription() );
  +            }
  +
  +            throw new MissingOptionException( buff.toString() );
  +        }
           return cl;
       }
       
  @@ -403,6 +473,10 @@
               _longOpts.put( "--" + opt.getLongOpt(), opt );
           }
           
  +        if ( opt.isRequired() ) {
  +            _requiredOpts.put( "-" + opt.getOpt(), opt );
  +        }
  +
           _shortOpts.put( "-" + opt.getOpt(), opt );
           
           _options.add( opt );
  
  
  
  1.1                  
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/AlreadySelectedException.java
  
  Index: AlreadySelectedException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: AlreadySelectedException.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
   */
  
  package org.apache.commons.cli;
  
  /** <p>Exception thrown when more than one option in an option group
   * has been provided.</p>
   *
   * @author John Keyes (john @ integralsource.com)
   * @version $Revision: 1.1 $
   */
  class AlreadySelectedException extends ParseException
  {
  
       /** Construct a new Exception with a message
        *
        * @param msg Explanation of the exception
        */
      public AlreadySelectedException( String message )
      {
          super( message );
      }
  }
  
  
  1.1                  
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/MissingOptionException.java
  
  Index: MissingOptionException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: MissingOptionException.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
   */
  
  package org.apache.commons.cli;
  
  /** <p>Exception thrown when an option requiring an argument
   * is not provided with an argument.</p>
   *
   * @author bob mcwhirter (bob @ werken.com)
   * @version $Revision: 1.1 $
   */
  public class MissingOptionException extends ParseException {
      
      /** Construct a new Exception with a message
       *
       * @param msg Explanation of the exception
       */
      public MissingOptionException(String msg) {
          super(msg);
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/OptionGroup.java
  
  Index: OptionGroup.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: OptionGroup.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
   */
  package org.apache.commons.cli;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  
  /**
   * A group of mutually exclusive options.
   * @author John Keyes ( john at integralsource.com )
   * @version $Revision: 1.1 $
   */
  public class OptionGroup 
  {
      /*
       * hold the options
       */
      private HashMap optionMap = new HashMap();
  
      /*
       * the name of the selected option
       */
      private Option selected;
  
      /**
       * add <code>opt</code> to this group
       */
      public OptionGroup addOption( Option opt )
      {
          // key   - option name
          // value - the option
          optionMap.put( "-" + opt.getOpt(), opt );
          return this;
      }
  
      /**
       * @return the names of the options in this group as a 
       * <code>Collection</code>
       */
      private Collection getNames( )
      {
          // the key set is the collection of names
          return optionMap.keySet();
      }
  
      /**
       * @return the options in this group as a <code>Collection</code>
       */
      public Collection getOptions( )
      {
          // the values are the collection of options
          return optionMap.values();
      }
  
      /**
       * set the selected option of this group to <code>name</code>.
       * @param name the name of the option that is selected
       * @throws AlreadySelectedException
       */
      public void setSelected( Option opt )
      throws AlreadySelectedException
      {
          // if no option has already been selected or the 
          // same option is being reselected then set the
          // selected member variable
          if ( this.selected == null || this.selected.equals( opt ) )
          {
              this.selected = opt;
          }
          else
          {
              throw new AlreadySelectedException( "an option from this group has 
already been selected: '" + selected + "'");
          }
      }
  
      /**
       * @return the selected option name
       */
      public Option getSelected( )
      {
          return selected;
      }
  
      /**
       * @return the usage string for this option group
       */
      /*
      public String usageString()
      {
          StringBuffer buff = new StringBuffer();
  
          buff.append( "<\n");
  
          Iterator oiter = getOptions().iterator();
  
          while( oiter.hasNext() )
          {
              Option option = (Option)oiter.next();
              Collection names = option.getNames();
  
              Iterator iter = names.iterator();
  
              while( iter.hasNext() )
              {
                  buff.append( option.getPrefix() );
                  buff.append( iter.next() );
                  if( iter.hasNext() )
                  {
                      buff.append( " | " );
                  }
              }
              buff.append( " " );
              buff.append( option.getDescription( ) );
              if ( oiter.hasNext() )
              {
                  buff.append( "\n  or\n" );
              }
          }
          buff.append( "\n>");
          buff.append( "\n" );
          return buff.toString();
      }
      */
  
      /**
       * return the stringified representation of this group
       */
      public String toString()
      {
          StringBuffer buff = new StringBuffer();
  
          Iterator iter = getOptions().iterator();
  
          buff.append( "[" );
          while( iter.hasNext() )
          {
              Option option = (Option)iter.next();
  
              buff.append( "-" );
              buff.append( option.getOpt() );
              buff.append( " " );
              buff.append( option.getDescription( ) );
  
              if( iter.hasNext() )
              {
                  buff.append( ", " );
              }
          }
          buff.append( "]" );
  
          return buff.toString();
      }
  }
  
  
  
  1.3       +3 -1      
jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/AllTest.java
  
  Index: AllTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/AllTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AllTest.java      1 Feb 2002 16:28:34 -0000       1.2
  +++ AllTest.java      23 Apr 2002 16:08:02 -0000      1.3
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: AllTest.java,v 1.2 2002/02/01 16:28:34 jstrachan Exp $
  + * $Id: AllTest.java,v 1.3 2002/04/23 16:08:02 jstrachan Exp $
    */
   
   package org.apache.commons.cli;
  @@ -27,6 +27,8 @@
           suite.addTest(BuildTest.suite()); 
           suite.addTest(ValueTest.suite()); 
           suite.addTest(ParseTest.suite()); 
  +        suite.addTest(ParseRequiredTest.suite()); 
  +        suite.addTest(OptionGroupTest.suite()); 
           suite.addTest(TestHelpFormatter.suite()); 
   
           return suite; 
  
  
  
  1.1                  
jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/OptionGroupTest.java
  
  Index: OptionGroupTest.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: OptionGroupTest.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
   */
  
  package org.apache.commons.cli;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * @author John Keyes (john at integralsource.com)
   * @version $Revision: 1.1 $
   */
  public class OptionGroupTest extends TestCase
  {
  
      private Options _options = null;
  
      public static Test suite() 
      { 
          return new TestSuite ( OptionGroupTest.class ); 
      }
  
      public OptionGroupTest( String name )
      {
          super( name );
      }
  
      public void setUp()
      {
          Option file = new Option( 'f', "file", false, "file to process" );
          Option dir = new Option( 'd', "directory", false, "directory to process" );
          OptionGroup group = new OptionGroup();
          group.addOption( file );
          group.addOption( dir );
          _options = new Options().addOptionGroup( group );
  
          Option section = new Option( 's', "section", false, "section to process" );
          Option chapter = new Option( 'c', "chapter", false, "chapter to process" );
          OptionGroup group2 = new OptionGroup();
          group2.addOption( section );
          group2.addOption( chapter );
  
          _options.addOptionGroup( group2 );
          _options.addOption( 'r', "revision", false, "revision number" );
      }
  
      public void tearDown()
      {
      }
  
      public void testSingleOptionFromGroup()
      {
          String[] args = new String[] { "-f" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
              assertTrue( "Confirm -f is set", cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testSingleOption()
      {
          String[] args = new String[] { "-r" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is set", cl.hasOption('r') );
              assertTrue( "Confirm -f is NOT set", !cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testTwoValidOptions()
      {
          String[] args = new String[] { "-r", "-f" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is set", cl.hasOption('r') );
              assertTrue( "Confirm -f is set", cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testSingleLongOption()
      {
          String[] args = new String[] { "--file" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
              assertTrue( "Confirm -f is set", cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testTwoValidLongOptions()
      {
          String[] args = new String[] { "--revision", "--file" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is set", cl.hasOption('r') );
              assertTrue( "Confirm -f is set", cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testNoOptionsExtraArgs()
      {
          String[] args = new String[] { "arg1", "arg2" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
              assertTrue( "Confirm -f is NOT set", !cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testTwoOptionsFromGroup()
      {
          String[] args = new String[] { "-f", "-d" };
  
          try
          {
              CommandLine cl = _options.parse(args);
              fail( "two arguments from group not allowed" );
          }
          catch (ParseException e)
          {
              if( !( e instanceof AlreadySelectedException ) )
              {
                  fail( "incorrect exception caught:" + e.getMessage() );
              }
          }
      }
  
      public void testTwoLongOptionsFromGroup()
      {
          String[] args = new String[] { "--file", "--directory" };
  
          try
          {
              CommandLine cl = _options.parse(args);
              fail( "two arguments from group not allowed" );
          }
          catch (ParseException e)
          {
              if( !( e instanceof AlreadySelectedException ) )
              {
                  fail( "incorrect exception caught:" + e.getMessage() );
              }
          }
      }
  
      public void testTwoOptionsFromDifferentGroup()
      {
          String[] args = new String[] { "-f", "-s" };
  
          try
          {
              CommandLine cl = _options.parse(args);
              assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
              assertTrue( "Confirm -f is set", cl.hasOption('f') );
              assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
              assertTrue( "Confirm -s is set", cl.hasOption('s') );
              assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
              assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/ParseRequiredTest.java
  
  Index: ParseRequiredTest.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: ParseRequiredTest.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
   */
  
  package org.apache.commons.cli;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * @author John Keyes (john at integralsource.com)
   * @version $Revision: 1.1 $
   */
  public class ParseRequiredTest extends TestCase
  {
  
      private Options _options = null;
  
      public static Test suite() { 
          return new TestSuite(ParseRequiredTest.class); 
      }
  
      public ParseRequiredTest(String name)
      {
          super(name);
      }
  
      public void setUp()
      {
          _options = new Options()
              .addOption('a',
                         "enable-a",
                         false,
                         "turn [a] on or off")
              .addOption('b',
                         "bfile",
                         true,
                         "set the value of [b]",
                         true);
      }
  
      public void tearDown()
      {
  
      }
  
      public void testWithRequiredOption()
      {
          String[] args = new String[] {  "-b", "file" };
  
          try
          {
              CommandLine cl = _options.parse(args);
              
              assertTrue( "Confirm -a is NOT set", !cl.hasOption('a') );
              assertTrue( "Confirm -b is set", cl.hasOption('b') );
              assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("file") );
              assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testOptionAndRequiredOption()
      {
          String[] args = new String[] {  "-a", "-b", "file" };
  
          try
          {
              CommandLine cl = _options.parse(args);
  
              assertTrue( "Confirm -a is set", cl.hasOption('a') );
              assertTrue( "Confirm -b is set", cl.hasOption('b') );
              assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("file") );
              assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0);
          }
          catch (ParseException e)
          {
              fail( e.toString() );
          }
      }
  
      public void testMissingRequiredOption()
      {
          String[] args = new String[] { "-a" };
  
          try
          {
              CommandLine cl = _options.parse(args);
              fail( "exception should have been thrown" );
          }
          catch (ParseException e)
          {
              if( !( e instanceof MissingOptionException ) )
              {
                  fail( "expected to catch MissingOptionException" );
              }
          }
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to