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/java/org/apache/commons/cli HelpFormatter.java OptionGroup.java
Date Mon, 22 Jul 2002 22:49:58 GMT
jkeyes      2002/07/22 15:49:58

  Modified:    cli/src/test/org/apache/commons/cli TestHelpFormatter.java
               cli/src/java/org/apache/commons/cli HelpFormatter.java
                        OptionGroup.java
  Log:
  added automatic usage generation
  
  Revision  Changes    Path
  1.3       +77 -69    jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java
  
  Index: TestHelpFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestHelpFormatter.java	16 Jun 2002 23:00:59 -0000	1.2
  +++ TestHelpFormatter.java	22 Jul 2002 22:49:58 -0000	1.3
  @@ -12,21 +12,18 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  -import java.io.StringWriter;
  +import java.io.ByteArrayOutputStream;
   import java.io.PrintWriter;
  +import java.io.StringWriter;
   
   /** 
    * Test case for the HelpFormatter class 
    *
    * @author Slawek Zachcial
  + * @author John Keyes ( jbjk at mac.com )
    **/
  -public class TestHelpFormatter
  -extends TestCase
  +public class TestHelpFormatter extends TestCase
   {
  -   // --------------------------------------------------------------- Constants
  -
  -   // ------------------------------------------------------------------ Static
  -
      public static void main( String[] args )
      {
         String[] testName = { TestHelpFormatter.class.getName() };
  @@ -38,14 +35,10 @@
         return new TestSuite(TestHelpFormatter.class);
      }
   
  -   // -------------------------------------------------------------- Attributes
  -
  -   // ------------------------------------------------------------ Constructors
      public TestHelpFormatter( String s )
      {
         super( s );
      }
  -   // ------------------------------------------------------------------ Public
   
      public void testFindWrapPos()
         throws Exception
  @@ -100,64 +93,79 @@
      }
   
      public void testPrintOptions()
  -      throws Exception
  +   throws Exception
      {
  -      StringBuffer sb = new StringBuffer();
  -      HelpFormatter hf = new HelpFormatter();
  -      final int leftPad = 1;
  -      final int descPad = 3;
  -      final String lpad = hf.createPadding(leftPad);
  -      final String dpad = hf.createPadding(descPad);
  -      Options options = null;
  -      String expected = null;
  -
  -      options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
  -      expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
  -      hf.renderOptions(sb, 60, options, leftPad, descPad);
  -      assertEquals("simple non-wrapped option", expected, sb.toString());
  -
  -      int nextLineTabStop = leftPad+descPad+"-a".length();
  -      expected =
  -         lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine +
  -         hf.createPadding(nextLineTabStop) + "aaaa aaaa";
  -      sb.setLength(0);
  -      hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
  -      assertEquals("simple wrapped option", expected, sb.toString());
  -
  -
  -      options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd");
  -      expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
  -      sb.setLength(0);
  -      hf.renderOptions(sb, 60, options, leftPad, descPad);
  -      assertEquals("long non-wrapped option", expected, sb.toString());
  -
  -      nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
  -      expected =
  -         lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
  -         hf.createPadding(nextLineTabStop) + "dddd dddd";
  -      sb.setLength(0);
  -      hf.renderOptions(sb, 25, options, leftPad, descPad);
  -      assertEquals("long wrapped option", expected, sb.toString());
  -
  -      options = new Options().
  -         addOption("a", "aaa", false, "dddd dddd dddd dddd").
  -         addOption("b", false, "feeee eeee eeee eeee");
  -      expected =
  -         lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
  -         hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine +
  -         lpad + "-b      " + dpad + "feeee eeee" + hf.defaultNewLine +
  -         hf.createPadding(nextLineTabStop) + "eeee eeee";
  -      sb.setLength(0);
  -      hf.renderOptions(sb, 25, options, leftPad, descPad);
  -      assertEquals("multiple wrapped options", expected, sb.toString());
  +       StringBuffer sb = new StringBuffer();
  +       HelpFormatter hf = new HelpFormatter();
  +       final int leftPad = 1;
  +       final int descPad = 3;
  +       final String lpad = hf.createPadding(leftPad);
  +       final String dpad = hf.createPadding(descPad);
  +       Options options = null;
  +       String expected = null;
  +
  +       options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
  +       expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
  +       hf.renderOptions(sb, 60, options, leftPad, descPad);
  +       assertEquals("simple non-wrapped option", expected, sb.toString());
  +
  +       int nextLineTabStop = leftPad+descPad+"-a".length();
  +       expected =
  +           lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine +
  +           hf.createPadding(nextLineTabStop) + "aaaa aaaa";
  +       sb.setLength(0);
  +       hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
  +       assertEquals("simple wrapped option", expected, sb.toString());
  +
  +
  +       options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd");
  +       expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
  +       sb.setLength(0);
  +       hf.renderOptions(sb, 60, options, leftPad, descPad);
  +       assertEquals("long non-wrapped option", expected, sb.toString());
  +
  +       nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
  +       expected =
  +           lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
  +           hf.createPadding(nextLineTabStop) + "dddd dddd";
  +       sb.setLength(0);
  +       hf.renderOptions(sb, 25, options, leftPad, descPad);
  +       assertEquals("long wrapped option", expected, sb.toString());
  +
  +       options = new Options().
  +           addOption("a", "aaa", false, "dddd dddd dddd dddd").
  +           addOption("b", false, "feeee eeee eeee eeee");
  +       expected =
  +           lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
  +           hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine +
  +           lpad + "-b      " + dpad + "feeee eeee" + hf.defaultNewLine +
  +           hf.createPadding(nextLineTabStop) + "eeee eeee";
  +       sb.setLength(0);
  +       hf.renderOptions(sb, 25, options, leftPad, descPad);
  +       assertEquals("multiple wrapped options", expected, sb.toString());
  +   }
  +
  +   public void testAutomaticUsage()
  +   throws Exception
  +   {
  +       HelpFormatter hf = new HelpFormatter();
  +       Options options = null;
  +       String expected = "usage: app [-a]\n";
  +       ByteArrayOutputStream out = new ByteArrayOutputStream( );
  +       PrintWriter pw = new PrintWriter( out );
  +
  +       options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
  +       hf.printUsage( pw, 60, "app", options );
  +       pw.flush();
  +       assertEquals("simple auto usage", expected, out.toString());
  +       out.reset();
  +
  +       expected = "usage: app [-b] [-a]\n";
  +       options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa")
  +       .addOption("b", false, "bbb" );
  +       hf.printUsage( pw, 60, "app", options );
  +       pw.flush();
  +       assertEquals("simple auto usage", expected, out.toString());
  +       out.reset();
      }
  -
  -   // --------------------------------------------------------------- Protected
  -
  -   // ------------------------------------------------------- Package protected   
  -   
  -   // ----------------------------------------------------------------- Private
  -   
  -   // ----------------------------------------------------------- Inner classes
  -
   }
  
  
  
  1.5       +130 -7    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HelpFormatter.java	10 Jul 2002 21:08:32 -0000	1.4
  +++ HelpFormatter.java	22 Jul 2002 22:49:58 -0000	1.5
  @@ -62,11 +62,12 @@
   package org.apache.commons.cli;
   
   import java.io.PrintWriter;
  -import java.util.Iterator;
  -import java.util.List;
   import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Collections;
   import java.util.Comparator;
  +import java.util.Iterator;
  +import java.util.List;
   
   /** 
    * A formatter of help messages for the current command line options
  @@ -117,7 +118,14 @@
      public void printHelp( String cmdLineSyntax,
                             Options options )
      {
  -      printHelp( defaultWidth, cmdLineSyntax, null, options, null );
  +       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,
  @@ -125,21 +133,39 @@
                             Options options,
                             String footer )
      {
  -      printHelp(defaultWidth, cmdLineSyntax, header, options, 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 );
  +                 options, defaultLeftPad, defaultDescPad, footer, autoUsage );
         pw.flush();
      }
  -
      public void printHelp( PrintWriter pw,
                             int width,
                             String cmdLineSyntax,
  @@ -148,6 +174,20 @@
                             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 )
  @@ -155,7 +195,13 @@
            throw new IllegalArgumentException("cmdLineSyntax not provided");
         }
   
  -      printUsage( pw, width, cmdLineSyntax );
  +      if ( autoUsage ) {
  +          printUsage( pw, width, cmdLineSyntax, options );
  +      }
  +      else {
  +          printUsage( pw, width, cmdLineSyntax );
  +      }
  +
         if ( header != null && header.trim().length() > 0 )
         {
            printWrapped( pw, width, header );
  @@ -165,6 +211,83 @@
         {
            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( "[" );
  +               }
  +               buff.append( "-" ).append( option.getOpt() );
  +
  +               // if the Option has a value
  +               if( option.hasArg() ) {
  +                   buff.append( " arg" );
  +               }
  +
  +               // if the Option is not a required option
  +               if( !option.isRequired() ) {
  +                   buff.append( "]" );
  +               }
  +               buff.append( " " );
  +           }
  +       }
  +
  +       System.out.println( "->" + buff.toString() );
  +       // call printWrapped
  +       printWrapped( pw, width, buff.toString().indexOf(' ')+1,
  +                     buff.toString() );
      }
   
      public void printUsage( PrintWriter pw, int width, String cmdLineSyntax )
  
  
  
  1.2       +1 -1      jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java
  
  Index: OptionGroup.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- OptionGroup.java	10 Jun 2002 18:01:15 -0000	1.1
  +++ OptionGroup.java	22 Jul 2002 22:49:58 -0000	1.2
  @@ -94,7 +94,7 @@
        * @return the names of the options in this group as a 
        * <code>Collection</code>
        */
  -    private Collection getNames() {
  +    public Collection getNames() {
           // the key set is the collection of names
           return optionMap.keySet();
       }
  
  
  

--
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