commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roxspr...@apache.org
Subject svn commit: r209516 [2/4] - in /jakarta/commons/proper/cli/branches/CLI_1_BRANCH: ./ src/conf/ src/java/org/apache/commons/cli/ src/media/ src/test/org/apache/commons/cli/ xdocs/ xdocs/images/ xdocs/style/
Date Wed, 06 Jul 2005 21:34:30 GMT
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java?rev=209516&r1=209515&r2=209516&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java Wed Jul  6 14:34:26 2005
@@ -1,67 +1,22 @@
 /*
- * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/CommandLine.java,v 1.4 2002/06/06 22:32:37 bayard Exp $
- * $Revision: 1.4 $
- * $Date: 2002/06/06 22:32:37 $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- *    any, must include the following acknowlegement:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowlegement may appear in the software itself,
- *    if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- *    Foundation" must not be used to endorse or promote products derived
- *    from this software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache"
- *    nor may "Apache" appear in their names without prior written
- *    permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
+ * Copyright 1999-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
-
 package org.apache.commons.cli;
 
 import java.io.PrintWriter;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -75,466 +30,900 @@
  * @author Slawek Zachcial
  * @author John Keyes (john at integralsource.com)
  **/
-public class HelpFormatter
-{
-   // --------------------------------------------------------------- Constants
-
-   public static final int DEFAULT_WIDTH              = 74;
-   public static final int DEFAULT_LEFT_PAD           = 1;
-   public static final int DEFAULT_DESC_PAD           = 3;
-   public static final String DEFAULT_SYNTAX_PREFIX   = "usage: ";
-   public static final String DEFAULT_OPT_PREFIX      = "-";
-   public static final String DEFAULT_LONG_OPT_PREFIX = "--";
-   public static final String DEFAULT_ARG_NAME        = "arg";
-
-   // ------------------------------------------------------------------ Static
-
-   // -------------------------------------------------------------- Attributes
-
-   public int defaultWidth;
-   public int defaultLeftPad;
-   public int defaultDescPad;
-   public String defaultSyntaxPrefix;
-   public String defaultNewLine;
-   public String defaultOptPrefix;
-   public String defaultLongOptPrefix;
-   public String defaultArgName;
-
-   // ------------------------------------------------------------ Constructors
-   public HelpFormatter()
-   {
-      defaultWidth = DEFAULT_WIDTH;
-      defaultLeftPad = DEFAULT_LEFT_PAD;
-      defaultDescPad = DEFAULT_DESC_PAD;
-      defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX;
-      defaultNewLine = System.getProperty("line.separator");
-      defaultOptPrefix = DEFAULT_OPT_PREFIX;
-      defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
-      defaultArgName = DEFAULT_ARG_NAME;
-   }
-
-   // ------------------------------------------------------------------ Public
-
-   public void printHelp( String cmdLineSyntax,
-                          Options options )
-   {
-       printHelp( defaultWidth, cmdLineSyntax, null, options, null, false );
-   }
-
-   public void printHelp( String cmdLineSyntax,
-                          Options options,
-                          boolean autoUsage )
-   {
-       printHelp( defaultWidth, cmdLineSyntax, null, options, null, autoUsage );
-   }
-
-   public void printHelp( String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          String footer )
-   {
-       printHelp( cmdLineSyntax, header, options, footer, false );
-   }
-
-   public void printHelp( String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          String footer,
-                          boolean autoUsage )
-   {
-      printHelp(defaultWidth, cmdLineSyntax, header, options, footer, autoUsage );
-   }
-   
-   public void printHelp( int width,
-                          String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          String footer )
-   {
-       printHelp( width, cmdLineSyntax, header, options, footer, false );
-   }
-
-   public void printHelp( int width,
-                          String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          String footer,
-                          boolean autoUsage )
-   {
-      PrintWriter pw = new PrintWriter(System.out);
-      printHelp( pw, width, cmdLineSyntax, header,
-                 options, defaultLeftPad, defaultDescPad, footer, autoUsage );
-      pw.flush();
-   }
-   public void printHelp( PrintWriter pw,
-                          int width,
-                          String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          int leftPad,
-                          int descPad,
-                          String footer )
-   throws IllegalArgumentException
-   {
-       printHelp( pw, width, cmdLineSyntax, header, options, leftPad, descPad, footer, false );
-   }
-
-   public void printHelp( PrintWriter pw,
-                          int width,
-                          String cmdLineSyntax,
-                          String header,
-                          Options options,
-                          int leftPad,
-                          int descPad,
-                          String footer,
-                          boolean autoUsage )
-      throws IllegalArgumentException
-   {
-      if ( cmdLineSyntax == null || cmdLineSyntax.length() == 0 )
-      {
-         throw new IllegalArgumentException("cmdLineSyntax not provided");
-      }
-
-      if ( autoUsage ) {
-          printUsage( pw, width, cmdLineSyntax, options );
-      }
-      else {
-          printUsage( pw, width, cmdLineSyntax );
-      }
-
-      if ( header != null && header.trim().length() > 0 )
-      {
-         printWrapped( pw, width, header );
-      }
-      printOptions( pw, width, options, leftPad, descPad );
-      if ( footer != null && footer.trim().length() > 0 )
-      {
-         printWrapped( pw, width, footer );
-      }
-   }
-
-   /**
-    * <p>Prints the usage statement for the specified application.</p>
-    *
-    * @param pw The PrintWriter to print the usage statement 
-    * @param width ??
-    * @param appName The application name
-    * @param options The command line Options
-    *
-    */
-   public void printUsage( PrintWriter pw, int width, String app, Options options ) 
-   {
-       // initialise the string buffer
-       StringBuffer buff = new StringBuffer( defaultSyntaxPrefix ).append( app ).append( " " );
-       
-       // create a list for processed option groups
-       ArrayList list = new ArrayList();
-
-       // temp variable
-       Option option;
-
-       // iterate over the options
-       for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
-       {
-           // get the next Option
-           option = (Option) i.next();
-
-           // check if the option is part of an OptionGroup
-           OptionGroup group = options.getOptionGroup( option );
-
-           // if the option is part of a group and the group has not already
-           // been processed
-           if( group != null && !list.contains(group)) {
-
-               // add the group to the processed list
-               list.add( group );
-
-               // get the names of the options from the OptionGroup
-               Collection names = group.getNames();
-
-               buff.append( "[" ); 
-
-               // for each option in the OptionGroup
-               for( Iterator iter = names.iterator(); iter.hasNext(); ) {
-                   buff.append( iter.next() );
-                   if( iter.hasNext() ) {
-                       buff.append( " | " );
-                   }
-               }
-               buff.append( "]" );
-           }
-           // if the Option is not part of an OptionGroup
-           else {
-               // if the Option is not a required option
-               if( !option.isRequired() ) {
-                   buff.append( "[" );
-               }
-               
-               if( !" ".equals( option.getOpt() ) ) {
-                   buff.append( "-" ).append( option.getOpt() );
-               }
-               else {
-                   buff.append( "--" ).append( option.getLongOpt() );
-               }
-
-               // if the Option has a value
-               if( option.hasArg() && option.getArgName() != null ) {
-                   buff.append( " " ).append( option.getArgName() );
-               }
-
-               // if the Option is not a required option
-               if( !option.isRequired() ) {
-                   buff.append( "]" );
-               }
-               buff.append( " " );
-           }
-       }
-
-       // call printWrapped
-       printWrapped( pw, width, buff.toString().indexOf(' ')+1,
-                     buff.toString() );
-   }
-
-   public void printUsage( PrintWriter pw, int width, String cmdLineSyntax )
-   {
-      int argPos = cmdLineSyntax.indexOf(' ') + 1;
-      printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos,
-                   defaultSyntaxPrefix + cmdLineSyntax);
-   }
-
-   public void printOptions( PrintWriter pw, int width, Options options, int leftPad, int descPad )
-   {
-      StringBuffer sb = new StringBuffer();
-      renderOptions(sb, width, options, leftPad, descPad);
-      pw.println(sb.toString());
-   }
-
-   public void printWrapped( PrintWriter pw, int width, String text )
-   {
-      printWrapped(pw, width, 0, text);
-   }
-
-   public void printWrapped( PrintWriter pw, int width, int nextLineTabStop, String text )
-   {
-      StringBuffer sb = new StringBuffer(text.length());
-      renderWrappedText(sb, width, nextLineTabStop, text);
-      pw.println(sb.toString());
-   }
-
-   // --------------------------------------------------------------- Protected
-
-   protected StringBuffer renderOptions( StringBuffer sb,
-                                         int width,
-                                         Options options,
-                                         int leftPad,
-                                         int descPad )
-   {
-      final String lpad = createPadding(leftPad);
-      final String dpad = createPadding(descPad);
-
-      //first create list containing only <lpad>-a,--aaa where -a is opt and --aaa is
-      //long opt; in parallel look for the longest opt string
-      //this list will be then used to sort options ascending
-      int max = 0;
-      StringBuffer optBuf;
-      List prefixList = new ArrayList();
-      Option option;
-      List optList = options.helpOptions();
-      Collections.sort( optList, new StringBufferComparator() );
-      for ( Iterator i = optList.iterator(); i.hasNext(); )
-      {
-         option = (Option) i.next();
-         optBuf = new StringBuffer(8);
-
-         if (option.getOpt().equals(" "))
-         {
-             optBuf.append(lpad).append("   " + defaultLongOptPrefix).append(option.getLongOpt());
-         }
-         else
-         {
-             optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
-             if ( option.hasLongOpt() )
-             {
-                optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
-             }
-
-         }
-
-         if( option.hasArg() ) {
-             if( option.hasArgName() ) {
-                 optBuf.append(" <").append( option.getArgName() ).append( '>' );
-             }
-             else {
-                 optBuf.append(' ');
-             }
-         }
-
-         prefixList.add(optBuf);
-         max = optBuf.length() > max ? optBuf.length() : max;
-      }
-      int x = 0;
-      for ( Iterator i = optList.iterator(); i.hasNext(); )
-      {
-         option = (Option) i.next();
-         optBuf = new StringBuffer( prefixList.get( x++ ).toString() );
-
-         if ( optBuf.length() < max )
-         {
-             optBuf.append(createPadding(max - optBuf.length()));
-         }
-         optBuf.append( dpad );
-         
-         int nextLineTabStop = max + descPad;
-
-         if( option.getDescription() != null ) {
-             optBuf.append( option.getDescription() );
-         }
-         renderWrappedText(sb, width, nextLineTabStop,
-                           optBuf.toString());
-         if ( i.hasNext() )
-         {
-             sb.append(defaultNewLine);
-         }
-      }
-
-      return sb;
-   }
-
-   protected StringBuffer renderWrappedText( StringBuffer sb,
-                                             int width,
-                                             int nextLineTabStop,
-                                             String text )
-   {
-      int pos = findWrapPos( text, width, 0);
-      if ( pos == -1 )
-      {
-         sb.append(rtrim(text));
-         return sb;
-      }
-      else
-      {
-         sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
-      }
-
-      //all following lines must be padded with nextLineTabStop space characters
-      final String padding = createPadding(nextLineTabStop);
-
-      while ( true )
-      {
-         text = padding + text.substring(pos).trim();
-         pos = findWrapPos( text, width, nextLineTabStop );
-         if ( pos == -1 )
-         {
-            sb.append(text);
+public class HelpFormatter {
+    // --------------------------------------------------------------- Constants
+
+    /** default number of characters per line */
+    public static final int DEFAULT_WIDTH = 74;
+
+    /** default padding to the left of each line */
+    public static final int DEFAULT_LEFT_PAD = 1;
+
+    /** ?? */
+    public static final int DEFAULT_DESC_PAD = 3;
+
+    /** the string to display at the begining of the usage statement */
+    public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
+
+    /** default prefix for shortOpts */
+    public static final String DEFAULT_OPT_PREFIX = "-";
+
+    /** default prefix for long Option */
+    public static final String DEFAULT_LONG_OPT_PREFIX = "--";
+
+    /** default name for an argument */
+    public static final String DEFAULT_ARG_NAME = "arg";
+
+    // -------------------------------------------------------------- Attributes
+
+    /** number of characters per line */
+    private int defaultWidth = DEFAULT_WIDTH;
+
+    /** amount of padding to the left of each line */
+    private int defaultLeftPad = DEFAULT_LEFT_PAD;
+
+    /** ?? */
+    private int defaultDescPad = DEFAULT_DESC_PAD;
+
+    /** the string to display at the begining of the usage statement */
+    private String defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX;
+
+    /** the new line character/string ?? */
+    private String defaultNewLine = System.getProperty("line.separator");
+
+    /** the shortOpt prefix */
+    private String defaultOptPrefix = DEFAULT_OPT_PREFIX;
+
+    /** the long Opt prefix */
+    private String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
+
+    /** the name of the argument */
+    private String defaultArgName = DEFAULT_ARG_NAME;
+
+    /**
+     * Sets the 'width'.
+     *
+     * @param width the new value of 'width'
+     */
+    public void setWidth(int width)
+    {
+        this.defaultWidth = width;
+    }
+
+    /**
+     * Returns the 'width'.
+     *
+     * @return the 'width'
+     */
+    public int getWidth()
+    {
+        return this.defaultWidth;
+    }
+
+    /**
+     * Sets the 'leftPadding'.
+     *
+     * @param padding the new value of 'leftPadding'
+     */
+    public void setLeftPadding(int padding)
+    {
+        this.defaultLeftPad = padding;
+    }
+
+    /**
+     * Returns the 'leftPadding'.
+     *
+     * @return the 'leftPadding'
+     */
+    public int getLeftPadding()
+    {
+        return this.defaultLeftPad;
+    }
+
+    /**
+     * Sets the 'descPadding'.
+     *
+     * @param padding the new value of 'descPadding'
+     */
+    public void setDescPadding(int padding)
+    {
+        this.defaultDescPad = padding;
+    }
+
+    /**
+     * Returns the 'descPadding'.
+     *
+     * @return the 'descPadding'
+     */
+    public int getDescPadding()
+    {
+        return this.defaultDescPad;
+    }
+
+    /**
+     * Sets the 'syntaxPrefix'.
+     *
+     * @param prefix the new value of 'syntaxPrefix'
+     */
+    public void setSyntaxPrefix(String prefix)
+    {
+        this.defaultSyntaxPrefix = prefix;
+    }
+
+    /**
+     * Returns the 'syntaxPrefix'.
+     *
+     * @return the 'syntaxPrefix'
+     */
+    public String getSyntaxPrefix()
+    {
+        return this.defaultSyntaxPrefix;
+    }
+
+    /**
+     * Sets the 'newLine'.
+     *
+     * @param newline the new value of 'newLine'
+     */
+    public void setNewLine(String newline)
+    {
+        this.defaultNewLine = newline;
+    }
+
+    /**
+     * Returns the 'newLine'.
+     *
+     * @return the 'newLine'
+     */
+    public String getNewLine()
+    {
+        return this.defaultNewLine;
+    }
+
+    /**
+     * Sets the 'optPrefix'.
+     *
+     * @param prefix the new value of 'optPrefix'
+     */
+    public void setOptPrefix(String prefix)
+    {
+        this.defaultOptPrefix = prefix;
+    }
+
+    /**
+     * Returns the 'optPrefix'.
+     *
+     * @return the 'optPrefix'
+     */
+    public String getOptPrefix()
+    {
+        return this.defaultOptPrefix;
+    }
+
+    /**
+     * Sets the 'longOptPrefix'.
+     *
+     * @param prefix the new value of 'longOptPrefix'
+     */
+    public void setLongOptPrefix(String prefix)
+    {
+        this.defaultLongOptPrefix = prefix;
+    }
+
+    /**
+     * Returns the 'longOptPrefix'.
+     *
+     * @return the 'longOptPrefix'
+     */
+    public String getLongOptPrefix()
+    {
+        return this.defaultLongOptPrefix;
+    }
+
+    /**
+     * Sets the 'argName'.
+     *
+     * @param name the new value of 'argName'
+     */
+    public void setArgName(String name)
+    {
+        this.defaultArgName = name;
+    }
+
+    /**
+     * Returns the 'argName'.
+     *
+     * @return the 'argName'
+     */
+    public String getArgName()
+    {
+        return this.defaultArgName;
+    }
+
+
+    // ------------------------------------------------------------------ Public
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to
+     * System.out.</p>
+     *
+     * @param cmdLineSyntax the syntax for this application
+     * @param options the Options instance
+     */
+    public void printHelp(String cmdLineSyntax, Options options)
+    {
+        printHelp(defaultWidth, cmdLineSyntax, null, options, null, false);
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to 
+     * System.out.</p>
+     *
+     * @param cmdLineSyntax the syntax for this application
+     * @param options the Options instance
+     * @param autoUsage whether to print an automatically generated 
+     * usage statement
+     */
+    public void printHelp(String cmdLineSyntax, Options options, 
+                          boolean autoUsage)
+    {
+        printHelp(defaultWidth, cmdLineSyntax, null, options, null, autoUsage);
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to
+     * System.out.</p>
+     *
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param footer the banner to display at the end of the help
+     */
+    public void printHelp(String cmdLineSyntax, String header, Options options, 
+                          String footer)
+    {
+        printHelp(cmdLineSyntax, header, options, footer, false);
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to 
+     * System.out.</p>
+     *
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param footer the banner to display at the end of the help
+     * @param autoUsage whether to print an automatically generated 
+     * usage statement
+     */
+    public void printHelp(String cmdLineSyntax, String header, Options options, 
+                          String footer, boolean autoUsage)
+    {
+        printHelp(defaultWidth, cmdLineSyntax, header, options, footer, 
+                  autoUsage);
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to
+     * System.out.</p>
+     *
+     * @param width the number of characters to be displayed on each line
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param footer the banner to display at the end of the help
+     */
+    public void printHelp(int width, String cmdLineSyntax, String header, 
+                          Options options, String footer)
+    {
+        printHelp(width, cmdLineSyntax, header, options, footer, false);
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.  This method prints help information to
+     * System.out.</p>
+     *
+     * @param width the number of characters to be displayed on each line
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param footer the banner to display at the end of the help
+     * @param autoUsage whether to print an automatically generated 
+     * usage statement
+     */
+    public void printHelp(int width, String cmdLineSyntax, String header, 
+                          Options options, String footer, boolean autoUsage)
+    {
+        PrintWriter pw = new PrintWriter(System.out);
+
+        printHelp(pw, width, cmdLineSyntax, header, options, defaultLeftPad, 
+                  defaultDescPad, footer, autoUsage);
+        pw.flush();
+    }
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.</p>
+     *
+     * @param pw the writer to which the help will be written
+     * @param width the number of characters to be displayed on each line
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param leftPad the number of characters of padding to be prefixed
+     * to each line
+     * @param descPad the number of characters of padding to be prefixed
+     * to each description line
+     * @param footer the banner to display at the end of the help
+     */
+    public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, 
+                          String header, Options options, int leftPad, 
+                          int descPad, String footer)
+    {
+        printHelp(pw, width, cmdLineSyntax, header, options, leftPad, descPad, 
+                  footer, false);
+    }
+
+
+    /**
+     * <p>Print the help for <code>options</code> with the specified
+     * command line syntax.</p>
+     *
+     * @param pw the writer to which the help will be written
+     * @param width the number of characters to be displayed on each line
+     * @param cmdLineSyntax the syntax for this application
+     * @param header the banner to display at the begining of the help
+     * @param options the Options instance
+     * @param leftPad the number of characters of padding to be prefixed
+     * to each line
+     * @param descPad the number of characters of padding to be prefixed
+     * to each description line
+     * @param footer the banner to display at the end of the help
+     * @param autoUsage whether to print an automatically generated 
+     * usage statement
+     */
+    public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, 
+                          String header, Options options, int leftPad, 
+                          int descPad, String footer, boolean autoUsage)
+    {
+        if ((cmdLineSyntax == null) || (cmdLineSyntax.length() == 0))
+        {
+            throw new IllegalArgumentException("cmdLineSyntax not provided");
+        }
+
+        if (autoUsage)
+        {
+            printUsage(pw, width, cmdLineSyntax, options);
+        }
+        else
+        {
+            printUsage(pw, width, cmdLineSyntax);
+        }
+
+        if ((header != null) && (header.trim().length() > 0))
+        {
+            printWrapped(pw, width, header);
+        }
+
+        printOptions(pw, width, options, leftPad, descPad);
+
+        if ((footer != null) && (footer.trim().length() > 0))
+        {
+            printWrapped(pw, width, footer);
+        }
+    }
+
+    /**
+     * <p>Prints the usage statement for the specified application.</p>
+     *
+     * @param pw The PrintWriter to print the usage statement 
+     * @param width The number of characters to display per line
+     * @param app The application name
+     * @param options The command line Options
+     *
+     */
+    public void printUsage(PrintWriter pw, int width, String app, 
+                           Options options)
+    {
+        // initialise the string buffer
+        StringBuffer buff = new StringBuffer(defaultSyntaxPrefix).append(app)
+                                                                 .append(" ");
+
+        // create a list for processed option groups
+        final Collection processedGroups = new ArrayList();
+
+        // temp variable
+        Option option;
+
+        // iterate over the options
+        for (Iterator i = options.getOptions().iterator(); i.hasNext();)
+        {
+            // get the next Option
+            option = (Option) i.next();
+
+            // check if the option is part of an OptionGroup
+            OptionGroup group = options.getOptionGroup(option);
+
+            // if the option is part of a group 
+            if (group != null)
+            {
+                // and if the group has not already been processed
+                if (!processedGroups.contains(group))
+                {
+                    // add the group to the processed list
+                    processedGroups.add(group);
+
+
+                    // add the usage clause
+                    appendOptionGroup(buff, group);
+                }
+
+                // otherwise the option was displayed in the group
+                // previously so ignore it.
+            }
+
+            // if the Option is not part of an OptionGroup
+            else
+            {
+                appendOption(buff, option, option.isRequired());
+            }
+
+            if (i.hasNext())
+            {
+                buff.append(" ");
+            }
+        }
+
+
+        // call printWrapped
+        printWrapped(pw, width, buff.toString().indexOf(' ') + 1, 
+                     buff.toString());
+    }
+
+    /**
+     * Appends the usage clause for an OptionGroup to a StringBuffer.  
+     * The clause is wrapped in square brackets if the group is required.
+     * The display of the options is handled by appendOption
+     * @param buff the StringBuffer to append to
+     * @param group the group to append
+     * @see #appendOption(StringBuffer,Option,boolean)
+     */
+    private static void appendOptionGroup(final StringBuffer buff, 
+                                          final OptionGroup group)
+    {
+        if (!group.isRequired())
+        {
+            buff.append("[");
+        }
+
+        // for each option in the OptionGroup
+        for (Iterator i = group.getOptions().iterator(); i.hasNext();)
+        {
+            // whether the option is required or not is handled at group level
+            appendOption(buff, (Option) i.next(), true);
+
+            if (i.hasNext())
+            {
+                buff.append(" | ");
+            }
+        }
+
+        if (!group.isRequired())
+        {
+            buff.append("]");
+        }
+    }
+
+    /**
+     * Appends the usage clause for an Option to a StringBuffer.  
+     *
+     * @param buff the StringBuffer to append to
+     * @param option the Option to append
+     * @param required whether the Option is required or not
+     */
+    private static void appendOption(final StringBuffer buff, 
+                                     final Option option, 
+                                     final boolean required)
+    {
+        if (!required)
+        {
+            buff.append("[");
+        }
+
+        if (option.getOpt() != null)
+        {
+            buff.append("-").append(option.getOpt());
+        }
+        else
+        {
+            buff.append("--").append(option.getLongOpt());
+        }
+
+        // if the Option has a value
+        if (option.hasArg() && (option.getArgName() != null))
+        {
+            buff.append(" <").append(option.getArgName()).append(">");
+        }
+
+        // if the Option is not a required option
+        if (!required)
+        {
+            buff.append("]");
+        }
+    }
+
+    /**
+     * <p>Print the cmdLineSyntax to the specified writer, using the
+     * specified width.</p>
+     *
+     * @param pw The printWriter to write the help to
+     * @param width The number of characters per line for the usage statement.
+     * @param cmdLineSyntax The usage statement.
+     */
+    public void printUsage(PrintWriter pw, int width, String cmdLineSyntax)
+    {
+        int argPos = cmdLineSyntax.indexOf(' ') + 1;
+
+        printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos, 
+                     defaultSyntaxPrefix + cmdLineSyntax);
+    }
+
+    /**
+     * <p>Print the help for the specified Options to the specified writer, 
+     * using the specified width, left padding and description padding.</p>
+     *
+     * @param pw The printWriter to write the help to
+     * @param width The number of characters to display per line
+     * @param options The command line Options
+     * @param leftPad the number of characters of padding to be prefixed
+     * to each line
+     * @param descPad the number of characters of padding to be prefixed
+     * to each description line
+     */
+    public void printOptions(PrintWriter pw, int width, Options options, 
+                             int leftPad, int descPad)
+    {
+        StringBuffer sb = new StringBuffer();
+
+        renderOptions(sb, width, options, leftPad, descPad);
+        pw.println(sb.toString());
+    }
+
+    /**
+     * <p>Print the specified text to the specified PrintWriter.</p>
+     *
+     * @param pw The printWriter to write the help to
+     * @param width The number of characters to display per line
+     * @param text The text to be written to the PrintWriter
+     */
+    public void printWrapped(PrintWriter pw, int width, String text)
+    {
+        printWrapped(pw, width, 0, text);
+    }
+
+    /**
+     * <p>Print the specified text to the specified PrintWriter.</p>
+     *
+     * @param pw The printWriter to write the help to
+     * @param width The number of characters to display per line
+     * @param nextLineTabStop The position on the next line for the first tab.
+     * @param text The text to be written to the PrintWriter
+     */
+    public void printWrapped(PrintWriter pw, int width, int nextLineTabStop, 
+                             String text)
+    {
+        StringBuffer sb = new StringBuffer(text.length());
+
+        renderWrappedText(sb, width, nextLineTabStop, text);
+        pw.println(sb.toString());
+    }
+
+    // --------------------------------------------------------------- Protected
+
+    /**
+     * <p>Render the specified Options and return the rendered Options
+     * in a StringBuffer.</p>
+     *
+     * @param sb The StringBuffer to place the rendered Options into.
+     * @param width The number of characters to display per line
+     * @param options The command line Options
+     * @param leftPad the number of characters of padding to be prefixed
+     * to each line
+     * @param descPad the number of characters of padding to be prefixed
+     * to each description line
+     *
+     * @return the StringBuffer with the rendered Options contents.
+     */
+    protected StringBuffer renderOptions(StringBuffer sb, int width, 
+                                         Options options, int leftPad, 
+                                         int descPad)
+    {
+        final String lpad = createPadding(leftPad);
+        final String dpad = createPadding(descPad);
+
+        // first create list containing only <lpad>-a,--aaa where 
+        // -a is opt and --aaa is long opt; in parallel look for 
+        // the longest opt string this list will be then used to 
+        // sort options ascending
+        int max = 0;
+        StringBuffer optBuf;
+        List prefixList = new ArrayList();
+        Option option;
+        List optList = options.helpOptions();
+
+        Collections.sort(optList, new StringBufferComparator());
+
+        for (Iterator i = optList.iterator(); i.hasNext();)
+        {
+            option = (Option) i.next();
+            optBuf = new StringBuffer(8);
+
+            if (option.getOpt() == null)
+            {
+                optBuf.append(lpad).append("   " + defaultLongOptPrefix)
+                      .append(option.getLongOpt());
+            }
+            else
+            {
+                optBuf.append(lpad).append(defaultOptPrefix)
+                      .append(option.getOpt());
+
+                if (option.hasLongOpt())
+                {
+                    optBuf.append(',').append(defaultLongOptPrefix)
+                          .append(option.getLongOpt());
+                }
+            }
+
+            if (option.hasArg())
+            {
+                if (option.hasArgName())
+                {
+                    optBuf.append(" <").append(option.getArgName()).append(">");
+                }
+                else
+                {
+                    optBuf.append(' ');
+                }
+            }
+
+            prefixList.add(optBuf);
+            max = (optBuf.length() > max)       ? optBuf.length() : max;
+        }
+
+        int x = 0;
+
+        for (Iterator i = optList.iterator(); i.hasNext();)
+        {
+            option = (Option) i.next();
+            optBuf = new StringBuffer(prefixList.get(x++).toString());
+
+            if (optBuf.length() < max)
+            {
+                optBuf.append(createPadding(max - optBuf.length()));
+            }
+
+            optBuf.append(dpad);
+
+            int nextLineTabStop = max + descPad;
+
+            if (option.getDescription() != null)
+            {
+                optBuf.append(option.getDescription());
+            }
+
+            renderWrappedText(sb, width, nextLineTabStop, optBuf.toString());
+
+            if (i.hasNext())
+            {
+                sb.append(defaultNewLine);
+            }
+        }
+
+        return sb;
+    }
+
+    /**
+     * <p>Render the specified text and return the rendered Options
+     * in a StringBuffer.</p>
+     *
+     * @param sb The StringBuffer to place the rendered text into.
+     * @param width The number of characters to display per line
+     * @param nextLineTabStop The position on the next line for the first tab.
+     * @param text The text to be rendered.
+     *
+     * @return the StringBuffer with the rendered Options contents.
+     */
+    protected StringBuffer renderWrappedText(StringBuffer sb, int width, 
+                                             int nextLineTabStop, String text)
+    {
+        int pos = findWrapPos(text, width, 0);
+
+        if (pos == -1)
+        {
+            sb.append(rtrim(text));
+
             return sb;
-         }
+        }
+        sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
+
+        // all following lines must be padded with nextLineTabStop space 
+        // characters
+        final String padding = createPadding(nextLineTabStop);
+
+        while (true)
+        {
+            text = padding + text.substring(pos).trim();
+            pos = findWrapPos(text, width, nextLineTabStop);
 
-         sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
-      }
+            if (pos == -1)
+            {
+                sb.append(text);
+
+                return sb;
+            }
+
+            sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
+        }
+    }
 
-   }
+    /**
+     * Finds the next text wrap position after <code>startPos</code> for the 
+     * text in <code>text</code> with the column width <code>width</code>.
+     * The wrap point is the last postion before startPos+width having a 
+     * whitespace character (space, \n, \r).
+     *
+     * @param text The text being searched for the wrap position
+     * @param width width of the wrapped text
+     * @param startPos position from which to start the lookup whitespace 
+     * character
+     * @return postion on which the text must be wrapped or -1 if the wrap 
+     * position is at the end of the text
+     */
+    protected int findWrapPos(String text, int width, int startPos)
+    {
+        int pos = -1;
 
-   /**
-    * Finds the next text wrap position after <code>startPos</code> for the text
-    * in <code>sb</code> with the column width <code>width</code>.
-    * The wrap point is the last postion before startPos+width having a whitespace
-    * character (space, \n, \r).
-    *
-    * @param sb text to be analyzed
-    * @param width width of the wrapped text
-    * @param startPos position from which to start the lookup whitespace character
-    * @return postion on which the text must be wrapped or -1 if the wrap position is at the end
-    *         of the text
-    */
-   protected int findWrapPos( String text, int width, int startPos )
-   {
-      int pos = -1;
-      // the line ends before the max wrap pos or a new line char found
-      if ( ((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)  ||
-           ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width) )
-      {
-         return pos;
-      }
-      else if ( (startPos + width) >= text.length() )
-      {
-         return -1;
-      }
-
-      //look for the last whitespace character before startPos+width
-      pos = startPos + width;
-      char c;
-      while ( pos >= startPos && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
-      {
-         --pos;
-      }
-      //if we found it - just return
-      if ( pos > startPos )
-      {
-         return pos;
-      }
-      else
-      {
-         //must look for the first whitespace chearacter after startPos + width
-         pos = startPos + width;
-         while ( pos <= text.length() && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
-         {
+        // the line ends before the max wrap pos or a new line char found
+        if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
+            || ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
+        {
+            return pos+1;
+        }
+        else if ((startPos + width) >= text.length())
+        {
+            return -1;
+        }
+
+
+        // look for the last whitespace character before startPos+width
+        pos = startPos + width;
+
+        char c;
+
+        while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ')
+               && (c != '\n') && (c != '\r'))
+        {
+            --pos;
+        }
+
+        // if we found it - just return
+        if (pos > startPos)
+        {
+            return pos;
+        }
+        
+        // must look for the first whitespace chearacter after startPos 
+        // + width
+        pos = startPos + width;
+
+        while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
+               && (c != '\n') && (c != '\r'))
+        {
             ++pos;
-         }
-         return pos == text.length() ? -1 : pos;
-      }
-   }
-
-   protected String createPadding(int len)
-   {
-      StringBuffer sb = new StringBuffer(len);
-      for ( int i = 0; i < len; ++i )
-      {
-         sb.append(' ');
-      }
-      return sb.toString();
-   }
-
-   protected String rtrim( String s )
-   {
-      if ( s == null || s.length() == 0 )
-      {
-         return s;
-      }
-
-      int pos = s.length();
-      while ( pos >= 0 && Character.isWhitespace(s.charAt(pos-1)) )
-      {
-         --pos;
-      }
-      return s.substring(0, pos);
-   }
-
-   // ------------------------------------------------------- Package protected
-   
-   // ----------------------------------------------------------------- Private
-   
-   // ----------------------------------------------------------- Inner classes
+        }
 
-    private static class StringBufferComparator
-    implements Comparator
+        return (pos == text.length())        ? (-1) : pos;
+    }
+
+    /**
+     * <p>Return a String of padding of length <code>len</code>.</p>
+     *
+     * @param len The length of the String of padding to create.
+     *
+     * @return The String of padding
+     */
+    protected String createPadding(int len)
+    {
+        StringBuffer sb = new StringBuffer(len);
+
+        for (int i = 0; i < len; ++i)
+        {
+            sb.append(' ');
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * <p>Remove the trailing whitespace from the specified String.</p>
+     *
+     * @param s The String to remove the trailing padding from.
+     *
+     * @return The String of without the trailing padding
+     */
+    protected String rtrim(String s)
     {
-        public int compare( Object o1, Object o2 )
+        if ((s == null) || (s.length() == 0))
+        {
+            return s;
+        }
+
+        int pos = s.length();
+
+        while ((pos > 0) && Character.isWhitespace(s.charAt(pos - 1)))
+        {
+            --pos;
+        }
+
+        return s.substring(0, pos);
+    }
+
+    // ------------------------------------------------------ Package protected
+    // ---------------------------------------------------------------- Private
+    // ---------------------------------------------------------- Inner classes
+    /**
+     * <p>This class implements the <code>Comparator</code> interface
+     * for comparing StringBuffers.</p>
+     */
+    private static class StringBufferComparator
+        implements Comparator {
+
+        /**
+         * <p>Compares its two arguments for order. Returns a negative 
+         * integer, zero, or a positive integer as the first argument 
+         * is less than, equal to, or greater than the second.</p>
+         *
+         * @param o1 The first StringBuffer to be compared.
+         * @param o2 The second StringBuffer to be compared.
+         *
+         * @return a negative integer, zero, or a positive integer as 
+         * the first argument is less than, equal to, or greater than the 
+         * second.
+         */
+        public int compare(Object o1, Object o2)
         {
             String str1 = stripPrefix(o1.toString());
             String str2 = stripPrefix(o2.toString());
+
             return (str1.compareTo(str2));
         }
 
+        /**
+         * <p>Removes any leading hyphens from <code>strOption</code>.</p>
+         *         
+         * @param strOption The String from which to strip the leading
+         * hyphens.
+         *
+         * @return The String without any leading hyphens.
+         */
         private String stripPrefix(String strOption)
         {
             // Strip any leading '-' characters
             int iStartIndex = strOption.lastIndexOf('-');
+
             if (iStartIndex == -1)
             {
-              iStartIndex = 0;
+                iStartIndex = 0;
             }
-            return strOption.substring(iStartIndex);
 
+            return strOption.substring(iStartIndex);
         }
     }
 }

Propchange: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingArgumentException.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingArgumentException.java?rev=209516&r1=209515&r2=209516&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingArgumentException.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingArgumentException.java Wed Jul  6 14:34:26 2005
@@ -1,64 +1,18 @@
 /*
- * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/MissingArgumentException.java,v 1.2 2002/06/06 09:37:26 jstrachan Exp $
- * $Revision: 1.2 $
- * $Date: 2002/06/06 09:37:26 $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- *    any, must include the following acknowlegement:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowlegement may appear in the software itself,
- *    if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- *    Foundation" must not be used to endorse or promote products derived
- *    from this software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache"
- *    nor may "Apache" appear in their names without prior written
- *    permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
+ * Copyright 1999-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
-
 package org.apache.commons.cli;
 
 /** 
@@ -68,15 +22,17 @@
  * @author John Keyes (john at integralsource.com)
  * @see ParseException
  */
-public class MissingArgumentException extends ParseException {
-    
+public class MissingArgumentException
+    extends ParseException {
+
     /** 
      * <p>Construct a new <code>MissingArgumentException</code> 
      * with the specified detail message.</p>
      *
      * @param message the detail message
      */
-    public MissingArgumentException( String message ) {
-        super( message );
+    public MissingArgumentException(String message)
+    {
+        super(message);
     }
-}
+}
\ No newline at end of file

Propchange: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingArgumentException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingOptionException.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingOptionException.java?rev=209516&r1=209515&r2=209516&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingOptionException.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingOptionException.java Wed Jul  6 14:34:26 2005
@@ -1,64 +1,18 @@
 /*
- * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/MissingOptionException.java,v 1.2 2002/06/06 09:37:26 jstrachan Exp $
- * $Revision: 1.2 $
- * $Date: 2002/06/06 09:37:26 $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- *    any, must include the following acknowlegement:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowlegement may appear in the software itself,
- *    if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- *    Foundation" must not be used to endorse or promote products derived
- *    from this software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache"
- *    nor may "Apache" appear in their names without prior written
- *    permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
+ * Copyright 1999-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
-
 package org.apache.commons.cli;
 
 /** 
@@ -67,15 +21,17 @@
  * @author John Keyes ( john at integralsource.com )
  * @see ParseException
  */
-public class MissingOptionException extends ParseException {
-    
+public class MissingOptionException
+    extends ParseException {
+
     /** 
      * <p>Construct a new <code>MissingSelectedException</code> 
      * with the specified detail message.</p>
      *
      * @param message the detail message
      */
-    public MissingOptionException( String message ) {
-        super( message );
+    public MissingOptionException(String message)
+    {
+        super(message);
     }
-}
+}
\ No newline at end of file

Propchange: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/MissingOptionException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java?rev=209516&r1=209515&r2=209516&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java Wed Jul  6 14:34:26 2005
@@ -1,62 +1,17 @@
 /*
- * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java,v 1.6 2002/06/06 22:50:14 bayard Exp $
- * $Revision: 1.6 $
- * $Date: 2002/06/06 22:50:14 $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- *    any, must include the following acknowlegement:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowlegement may appear in the software itself,
- *    if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- *    Foundation" must not be used to endorse or promote products derived
- *    from this software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache"
- *    nor may "Apache" appear in their names without prior written
- *    permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
+ * Copyright 1999-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 package org.apache.commons.cli;
 
@@ -75,16 +30,17 @@
  *
  * @author bob mcwhirter (bob @ werken.com)
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.6 $
+ * @version $Revision$
  */
 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 has 
+        not been specified */
+    public static final int UNINITIALIZED = -1;
+
     /** constant that specifies the number of argument values is infinite */
-    public final static int UNLIMITED_VALUES = -2;
-    
+    public static final int UNLIMITED_VALUES = -2;
+
     /** opt the name of the option */
     private String opt;
 
@@ -95,7 +51,7 @@
     private boolean hasArg;
 
     /** argName specifies the name of the argument for this option */
-    private String argName;
+    private String argName = "arg";
 
     /** description of the option */
     private String description;
@@ -110,14 +66,14 @@
      * numberOfArgs specifies the number of argument values this option 
      * can have 
      */
-    private int numberOfArgs = UNINITIALIZED;   
+    private int numberOfArgs = UNINITIALIZED;
 
     /** the type of this Option */
     private Object type;
 
     /** the list of argument values **/
     private ArrayList values = new ArrayList();
-    
+
     /** the character that is the value separator */
     private char valuesep;
 
@@ -125,13 +81,15 @@
      * Creates an Option using the specified parameters.
      *
      * @param opt short representation of the option
-     * @param hasArg specifies whether the Option takes an argument or not
      * @param description describes the function of the option
+     *
+     * @throws IllegalArgumentException if there are any non valid
+     * Option characters in <code>opt</code>.
      */
-    public Option( String opt, String description ) 
-    throws IllegalArgumentException
+    public Option(String opt, String description)
+           throws IllegalArgumentException
     {
-        this( opt, null, false, description );
+        this(opt, null, false, description);
     }
 
     /**
@@ -140,13 +98,16 @@
      * @param opt short representation of the option
      * @param hasArg specifies whether the Option takes an argument or not
      * @param description describes the function of the option
+     *
+     * @throws IllegalArgumentException if there are any non valid
+     * Option characters in <code>opt</code>.
      */
-    public Option( String opt, boolean hasArg, String description ) 
-    throws IllegalArgumentException
+    public Option(String opt, boolean hasArg, String description)
+           throws IllegalArgumentException
     {
-        this( opt, null, hasArg, description );
+        this(opt, null, hasArg, description);
     }
-    
+
     /**
      * <p>Creates an Option using the specified parameters.</p>
      *
@@ -154,25 +115,30 @@
      * @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
+     *
+     * @throws IllegalArgumentException if there are any non valid
+     * Option characters in <code>opt</code>.
      */
-    public Option( String opt, String longOpt, boolean hasArg, String description ) 
-    throws IllegalArgumentException
+    public Option(String opt, String longOpt, boolean hasArg, 
+                  String description)
+           throws IllegalArgumentException
     {
         // ensure that the option is valid
-        OptionValidator.validateOption( opt );
+        OptionValidator.validateOption(opt);
 
-        this.opt          = opt;
-        this.longOpt      = longOpt;
+        this.opt = opt;
+        this.longOpt = longOpt;
 
         // if hasArg is set then the number of arguments is 1
-        if( hasArg ) {
+        if (hasArg)
+        {
             this.numberOfArgs = 1;
         }
 
-        this.hasArg       = hasArg;
-        this.description  = description;
+        this.hasArg = hasArg;
+        this.description = description;
     }
-    
+
     /**
      * <p>Returns the id of this Option.  This is only set when the
      * Option shortOpt is a single character.  This is used for switch
@@ -180,8 +146,9 @@
      *
      * @return the id of this Option
      */
-    public int getId( ) {
-        return getKey().charAt( 0 );
+    public int getId()
+    {
+        return getKey().charAt(0);
     }
 
     /**
@@ -189,11 +156,14 @@
      * 
      * @return the 'unique' Option identifier
      */
-    String getKey() {
+    String getKey()
+    {
         // if 'opt' is null, then it is a 'long' option
-        if( opt == null ) {
+        if (opt == null)
+        {
             return this.longOpt;
         }
+
         return this.opt;
     }
 
@@ -206,7 +176,8 @@
      *
      * @return The name of this option
      */
-    public String getOpt() {
+    public String getOpt()
+    {
         return this.opt;
     }
 
@@ -215,7 +186,8 @@
      * 
      * @return The type of this option
      */
-    public Object getType() {
+    public Object getType()
+    {
         return this.type;
     }
 
@@ -224,16 +196,18 @@
      *
      * @param type the type of this Option
      */
-    public void setType( Object type ) {
+    public void setType(Object type)
+    {
         this.type = type;
     }
-    
+
     /** 
      * <p>Retrieve the long name of this Option.</p>
      *
      * @return Long name of this option, or null, if there is no long name
      */
-    public String getLongOpt() {
+    public String getLongOpt()
+    {
         return this.longOpt;
     }
 
@@ -242,7 +216,8 @@
      *
      * @param longOpt the long name of this Option
      */
-    public void setLongOpt( String longOpt ) {
+    public void setLongOpt(String longOpt)
+    {
         this.longOpt = longOpt;
     }
 
@@ -252,136 +227,158 @@
      * @param optionalArg specifies whether the Option can have
      * an optional argument.
      */
-    public void setOptionalArg( boolean optionalArg ) {
+    public void setOptionalArg(boolean optionalArg)
+    {
         this.optionalArg = optionalArg;
     }
 
     /**
      * @return whether this Option can have an optional argument
      */
-    public boolean hasOptionalArg( ) {
+    public boolean hasOptionalArg()
+    {
         return this.optionalArg;
     }
-    
+
     /** <p>Query to see if this Option has a long name</p>
      *
      * @return boolean flag indicating existence of a long name
      */
-    public boolean hasLongOpt() {
-        return ( this.longOpt != null );
+    public boolean hasLongOpt()
+    {
+        return (this.longOpt != null);
     }
-    
+
     /** <p>Query to see if this Option requires an argument</p>
      *
      * @return boolean flag indicating if an argument is required
      */
-    public boolean hasArg() {
-        return this.numberOfArgs > 0 || numberOfArgs == UNLIMITED_VALUES;
+    public boolean hasArg()
+    {
+        return (this.numberOfArgs > 0) || (numberOfArgs == UNLIMITED_VALUES);
     }
-    
+
     /** <p>Retrieve the self-documenting description of this Option</p>
      *
      * @return The string description of this option
      */
-    public String getDescription() {
+    public String getDescription()
+    {
         return this.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 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>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 );
-     }
-
-     /** 
-      * <p>Query to see if this Option can take many values</p>
-      *
-      * @return boolean flag indicating if multiple values are allowed
-      */
-     public boolean hasArgs() {
-         return ( this.numberOfArgs > 1 || this.numberOfArgs == UNLIMITED_VALUES );
-     }
-
-     /** 
-      * <p>Sets the number of argument values this Option can take.</p>
-      *
-      * @param num the number of argument values
-      */
-     public void setArgs( int num ) {
-         this.numberOfArgs = num;
-     }
-
-     /**
-      * <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>
-      *
-      * @return num the number of argument values
-      */
-     public int getArgs( ) {
-         return this.numberOfArgs;
-     }
+    /** 
+     * <p>Query to see if this Option requires an argument</p>
+     *
+     * @return boolean flag indicating if an argument is required
+     */
+    public boolean isRequired()
+    {
+        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>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);
+    }
+
+    /** 
+     * <p>Query to see if this Option can take many values</p>
+     *
+     * @return boolean flag indicating if multiple values are allowed
+     */
+    public boolean hasArgs()
+    {
+        return (this.numberOfArgs > 1) 
+                || (this.numberOfArgs == UNLIMITED_VALUES);
+    }
 
-    public void clearValues() {
-        this.values.clear();
+    /** 
+     * <p>Sets the number of argument values this Option can take.</p>
+     *
+     * @param num the number of argument values
+     */
+    public void setArgs(int num)
+    {
+        this.numberOfArgs = num;
+    }
+
+    /**
+     * <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>Return whether this Option has specified a value separator.</p>
+     * 
+     * @return whether this Option has specified a value separator.</p>
+     */
+    public boolean hasValueSeparator()
+    {
+        return (this.valuesep > 0);
+    }
+
+    /** 
+     * <p>Returns the number of argument values this Option can take.</p>
+     *
+     * @return num the number of argument values
+     */
+    public int getArgs()
+    {
+        return this.numberOfArgs;
     }
 
     /**
@@ -389,94 +386,173 @@
      * 
      * @param value is a/the value of this Option
      */
-    public boolean addValue( String value ) {
+    void addValue(String value)
+    {
+        switch (numberOfArgs)
+        {
+        case UNINITIALIZED:
+            throw new RuntimeException("NO_ARGS_ALLOWED");
 
-        switch( numberOfArgs ) {
-            case UNINITIALIZED:
-                return false;
-            case UNLIMITED_VALUES:
-                if( getValueSeparator() > 0 ) {
-                    int index = 0;
-                    while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) {
-                        this.values.add( value.substring( 0, index ) );
-                        value = value.substring( index+1 );
-                    }
-                }
-                this.values.add( value );
-                return true;
-            default:
-                if( getValueSeparator() > 0 ) {
-                    int index = 0;
-                    while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) {
-                        if( values.size() > numberOfArgs-1 ) {
-                            return false;
-                        }
-                        this.values.add( value.substring( 0, index ) );
-                        value = value.substring( index+1 );
-                    }
-                }
-                if( values.size() > numberOfArgs-1 ) {
-                    return false;
+        default:
+            processValue(value);
+        }
+    }
+
+    /**
+     * <p>Processes the value.  If this Option has a value separator
+     * the value will have to be parsed into individual tokens.  When
+     * n-1 tokens have been processed and there are more value separators
+     * in the value, parsing is ceased and the remaining characters are
+     * added as a single token.</p>
+     *
+     * @param value The String to be processed.
+     *
+     * @since 1.0.1
+     */
+    private void processValue(String value)
+    {
+        // this Option has a separator character
+        if (hasValueSeparator())
+        {
+            // get the separator character
+            char sep = getValueSeparator();
+
+            // store the index for the value separator
+            int index = value.indexOf(sep);
+
+            // while there are more value separators
+            while (index != -1)
+            {
+                // next value to be added 
+                if (values.size() == (numberOfArgs - 1))
+                {
+                    break;
                 }
-                this.values.add( value );
-                return true;
+
+
+                // store
+                add(value.substring(0, index));
+
+
+                // parse
+                value = value.substring(index + 1);
+
+
+                // get new index
+                index = value.indexOf(sep);
+            }
+        }
+
+
+        // store the actual value or the last value that has been parsed
+        add(value);
+    }
+
+    /**
+     * <p>Add the value to this Option.  If the number of arguments
+     * is greater than zero and there is enough space in the list then
+     * add the value.  Otherwise, throw a runtime exception.
+     * </p>
+     *
+     * @param value The value to be added to this Option
+     *
+     * @since 1.0.1
+     */
+    private void add(String value)
+    {
+        if ((numberOfArgs > 0) && (values.size() > (numberOfArgs - 1)))
+        {
+            throw new RuntimeException("Cannot add value, list full.");
         }
+
+
+        // store value
+        this.values.add(value);
     }
 
     /**
+     * Returns the specified value of this Option or 
+     * <code>null</code> if there is no value.
+     *
      * @return the value/first value of this Option or 
-     * <code>null</code> if there are no values.
+     * <code>null</code> if there is no value.
      */
-    public String getValue() {
-        return this.values.size()==0 ? null : (String)this.values.get( 0 );
+    public String getValue()
+    {
+        return hasNoValues() ? null : (String) this.values.get(0);
     }
 
     /**
+     * Returns the specified value of this Option or 
+     * <code>null</code> if there is no value.
+     *
+     * @param index The index of the value to be returned.
+     *
      * @return the specified value of this Option or 
-     * <code>null</code> if there are no values.
+     * <code>null</code> if there is no value.
+     *
+     * @throws IndexOutOfBoundsException if index is less than 1
+     * or greater than the number of the values for this Option.
      */
-    public String getValue( int index ) 
-    throws IndexOutOfBoundsException
+    public String getValue(int index)
+        throws IndexOutOfBoundsException
     {
-        return ( this.values.size()==0 ) ? null : (String)this.values.get( index );
+        return hasNoValues() ? null : (String) this.values.get(index);
     }
 
     /**
+     * Returns the value/first value of this Option or the 
+     * <code>defaultValue</code> if there is no value.
+     *
+     * @param defaultValue The value to be returned if ther
+     * is no value.
+     *
      * @return the value/first value of this Option or the 
      * <code>defaultValue</code> if there are no values.
      */
-    public String getValue( String defaultValue ) {
-        String value = getValue( );
-        return ( value != null ) ? value : defaultValue;
+    public String getValue(String defaultValue)
+    {
+        String value = getValue();
+
+        return (value != null) ? value : defaultValue;
     }
 
     /**
+     * Return the values of this Option as a String array 
+     * 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[]{});
+    public String[] getValues()
+    {
+        return hasNoValues()
+               ? 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() {
+    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() );
-        option.setOptionalArg( hasOptionalArg() );
-        option.setRequired( isRequired() );
-        option.setLongOpt( getLongOpt() );
-        option.setType( getType() );
-        option.setValueSeparator( getValueSeparator() );
+    public Object clone()
+    {
+        Option option = new Option(getOpt(), getDescription());
+
+        option.setArgs(getArgs());
+        option.setOptionalArg(hasOptionalArg());
+        option.setRequired(isRequired());
+        option.setLongOpt(getLongOpt());
+        option.setType(getType());
+        option.setValueSeparator(getValueSeparator());
+
         return option;
     }
 
@@ -485,32 +561,43 @@
      *
      * @return Stringified form of this object
      */
-    public String toString() {
+    public String toString()
+    {
         StringBuffer buf = new StringBuffer().append("[ option: ");
-        
-        buf.append( this.opt );
-        
-        if ( this.longOpt != null ) {
-            buf.append(" ")
-            .append(this.longOpt);
+
+        buf.append(this.opt);
+
+        if (this.longOpt != null)
+        {
+            buf.append(" ").append(this.longOpt);
         }
-        
+
         buf.append(" ");
-        
-        if ( hasArg ) {
-            buf.append( "+ARG" );
+
+        if (hasArg)
+        {
+            buf.append("+ARG");
         }
-        
-        buf.append(" :: ")
-        .append( this.description );
-        
-        if ( this.type != null ) {
-            buf.append(" :: ")
-            .append( this.type );
+
+        buf.append(" :: ").append(this.description);
+
+        if (this.type != null)
+        {
+            buf.append(" :: ").append(this.type);
         }
 
         buf.append(" ]");
+
         return buf.toString();
     }
 
-}
+    /**
+     * Returns whether this Option has any values.
+     *
+     * @return whether this Option has any values.
+     */
+    private boolean hasNoValues()
+    {
+        return this.values.size() == 0;
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



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


Mime
View raw message