commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jke...@apache.org
Subject cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli BugsTest.java
Date Tue, 08 Oct 2002 21:24:11 GMT
jkeyes      2002/10/08 14:24:11

  Modified:    cli/src/java/org/apache/commons/cli HelpFormatter.java
                        Option.java OptionBuilder.java Options.java
                        Parser.java
               cli/src/test/org/apache/commons/cli BugsTest.java
  Log:
  fixed bug 13425, added argName support, fixed some bugs in the HelpFormatter
  
  Revision  Changes    Path
  1.8       +30 -35    jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java
  
  Index: HelpFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HelpFormatter.java	19 Sep 2002 22:59:43 -0000	1.7
  +++ HelpFormatter.java	8 Oct 2002 21:24:11 -0000	1.8
  @@ -269,11 +269,24 @@
                  if( !option.isRequired() ) {
                      buff.append( "[" );
                  }
  -               buff.append( "-" ).append( option.getOpt() );
  +               
  +               if( !" ".equals( option.getOpt() ) ) {
  +                   buff.append( "-" ).append( option.getOpt() );
  +               }
  +               else {
  +                   buff.append( "--" ).append( option.getLongOpt() );
  +               }
  +
  +               if( option.getValueSeparator() != (char)0 ) {
  +                   buff.append( option.getValueSeparator() );
  +               }
  +               else if( option.hasArg() ){
  +                   buff.append( " " );
  +               }
   
                  // if the Option has a value
                  if( option.hasArg() ) {
  -                   buff.append( " arg" );
  +                   buff.append( option.getArgName() );
                  }
   
                  // if the Option is not a required option
  @@ -284,7 +297,6 @@
              }
          }
   
  -       System.out.println( "->" + buff.toString() );
          // call printWrapped
          printWrapped( pw, width, buff.toString().indexOf(' ')+1,
                        buff.toString() );
  @@ -334,7 +346,9 @@
         StringBuffer optBuf;
         List prefixList = new ArrayList();
         Option option;
  -      for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
  +      List optList = options.helpOptions();
  +       Collections.sort( optList, new StringBufferComparator() );
  +      for ( Iterator i = optList.iterator(); i.hasNext(); )
         {
            option = (Option) i.next();
            optBuf = new StringBuffer(8);
  @@ -353,49 +367,30 @@
   
            }
   
  -
  -         if ( option.hasArg() )
  -         {
  -            //FIXME - should have a way to specify arg name per option
  -            optBuf.append(' ').append(defaultArgName);
  +         if( option.hasArg() ) {
  +             if( option.hasArgName() ) {
  +                 optBuf.append( option.getArgName() );
  +             }
  +             else {
  +                 optBuf.append(' ');
  +             }
            }
  +
            prefixList.add(optBuf);
            max = optBuf.length() > max ? optBuf.length() : max;
  -      }
   
  -      //right pad the prefixes
  -      for ( Iterator i = prefixList.iterator(); i.hasNext(); )
  -      {
  -         optBuf = (StringBuffer) i.next();
            if ( optBuf.length() < max )
            {
  -            optBuf.append(createPadding(max-optBuf.length()));
  +             optBuf.append(createPadding(max-optBuf.length()));
            }
            optBuf.append(dpad);
  -      }
  -
  -      //sort this list ascending
  -      Collections.sort(prefixList, new StringBufferComparator());
  -
  -      //finally render options
  -      int nextLineTabStop = max + descPad;
  -      String opt;
  -      int optOffset = leftPad + defaultOptPrefix.length();
  -
  -      for ( Iterator i = prefixList.iterator(); i.hasNext(); )
  -      {
  -         optBuf = (StringBuffer) i.next();
  -         opt = optBuf.toString().trim();
  -         if( opt.indexOf( ',' ) != -1 ) {
  -             opt = opt.substring(0, opt.indexOf( ',', optOffset ) );
  -         }
  -         option = options.getOption(opt);
  -
  +         
  +         int nextLineTabStop = max + descPad;
            renderWrappedText(sb, width, nextLineTabStop,
                              optBuf.append(option.getDescription()).toString());
            if ( i.hasNext() )
            {
  -            sb.append(defaultNewLine);
  +             sb.append(defaultNewLine);
            }
         }
   
  
  
  
  1.14      +32 -0     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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- Option.java	10 Sep 2002 21:35:57 -0000	1.13
  +++ Option.java	8 Oct 2002 21:24:11 -0000	1.14
  @@ -106,6 +106,9 @@
       /** hasArg specifies whether this option has an associated argument */
       private boolean hasArg;
   
  +    /** argName specifies the name of the argument for this option */
  +    private String argName;
  +
       /** description of the option */
       private String description;
   
  @@ -364,6 +367,35 @@
         */
        public void setRequired( boolean required ) {
            this.required = required;
  +     }
  +
  +     /**
  +      * <p>Sets the display name for the argument value.</p>
  +      *
  +      * @param argName the display name for the argument value.
  +      */
  +     public void setArgName( String argName ) {
  +         this.argName = argName;
  +     }
  +
  +     /**
  +      * <p>Gets the display name for the argument value.</p>
  +      *
  +      * @return the display name for the argument value.
  +      */
  +     public String getArgName() {
  +         return this.argName;
  +     }
  +
  +     /**
  +      * <p>Returns whether the display name for the argument value
  +      * has been set.</p>
  +      *
  +      * @return if the display name for the argument value has been
  +      * set.
  +      */
  +     public boolean hasArgName() {
  +         return (this.argName != null || this.argName.length() > 0 );
        }
   
        /** 
  
  
  
  1.11      +19 -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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- OptionBuilder.java	19 Sep 2002 22:59:43 -0000	1.10
  +++ OptionBuilder.java	8 Oct 2002 21:24:11 -0000	1.11
  @@ -76,6 +76,8 @@
       private static String longopt;
       /** option description */
       private static String description;
  +    /** argument name */
  +    private static String argName;
       /** is required? */
       private static boolean required;
       /** the number of arguments */
  @@ -99,6 +101,7 @@
        */
       private static void reset() {
           description = null;
  +        argName = null;
           longopt = null;
           type = null;
           required = false;
  @@ -143,6 +146,18 @@
       }
   
       /**
  +     * <p>The next Option created will have the specified argument value 
  +     * name.</p>
  +     *
  +     * @param name the name for the argument value
  +     * @return the OptionBuilder instance
  +     */
  +    public static OptionBuilder withArgName( String name ) {
  +        instance.argName = name;
  +        return instance;
  +    }
  +
  +    /**
        * <p>The next Option created will be required.</p>
        *
        * @return the OptionBuilder instance
  @@ -344,6 +359,7 @@
           option.setArgs( numberOfArgs );
           option.setType( type );
           option.setValueSeparator( valuesep );
  +        option.setArgName( argName );
           // reset the OptionBuilder properties
           instance.reset();
   
  
  
  
  1.13      +9 -0      jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java
  
  Index: Options.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Options.java	9 Sep 2002 19:46:26 -0000	1.12
  +++ Options.java	8 Oct 2002 21:24:11 -0000	1.13
  @@ -195,6 +195,15 @@
           return Collections.unmodifiableCollection( opts );
       }
   
  +    /**
  +     * <p>Returns the Options for use by the HelpFormatter.</p>
  +     *
  +     * @return the List of Options
  +     */
  +    List helpOptions() {
  +        return new ArrayList( shortOpts.values() );
  +    }
  +
       /** <p>Returns the required options as a 
        * <code>java.util.Collection</code>.</p>
        *
  
  
  
  1.6       +15 -15    jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java
  
  Index: Parser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Parser.java	19 Sep 2002 22:59:43 -0000	1.5
  +++ Parser.java	8 Oct 2002 21:24:11 -0000	1.6
  @@ -217,26 +217,26 @@
       }
   
       public void processArgs( Option opt, ListIterator iter ) 
  -    throws ParseException 
  +    throws ParseException
       {
  -        if( !iter.hasNext() && !opt.hasOptionalArg() ) {
  -            throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
  -        }
           // loop until an option is found
           while( iter.hasNext() ) {
               String var = (String)iter.next();
  +
  +            // found an Option
               if( options.hasOption( var ) ) {
                   iter.previous();
                   break;
               }
  -
  -            // its a value
  -            else {
  -                if( !opt.addValue( var ) ) {
  -                    iter.previous();
  -                    break;
  -                }
  +            // found a value
  +            else if( !opt.addValue( var ) ) {
  +                iter.previous();
  +                break;
               }
  +        }
  +
  +        if( opt.getValues() == null && !opt.hasOptionalArg() ) {
  +            throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
           }
       }
   
  
  
  
  1.8       +34 -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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BugsTest.java	19 Sep 2002 22:59:44 -0000	1.7
  +++ BugsTest.java	8 Oct 2002 21:24:11 -0000	1.8
  @@ -229,4 +229,37 @@
               fail( "Unexpected exception: " + exp.getMessage() );
           }
       }
  +
  +    public void test13425() {
  +        Options options = new Options();
  +        Option oldpass = OptionBuilder.withLongOpt( "old-password" )
  +            .withDescription( "Use this option to specify the old password" )
  +            .hasArg()
  +            .create( 'o' );
  +        Option newpass = OptionBuilder.withLongOpt( "new-password" )
  +            .withDescription( "Use this option to specify the new password" )
  +            .hasArg()
  +            .create( 'n' );
  +
  +        String[] args = { 
  +            "-o", 
  +            "-n", 
  +            "newpassword" 
  +        };
  +
  +        options.addOption( oldpass );
  +        options.addOption( newpass );
  +
  +        Parser parser = new PosixParser();
  +
  +        try {
  +            CommandLine line = parser.parse( options, args );
  +        }
  +        // catch the exception and leave the method
  +        catch( Exception exp ) {
  +            assertTrue( exp != null );
  +            return;
  +        }
  +        fail( "MissingArgumentException not caught." );
  +    }
   }
  
  
  

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


Mime
View raw message