You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2004/10/15 00:50:32 UTC

cvs commit: jakarta-commons/cli/src/java/org/apache/commons/cli2/util HelpFormatter.java

roxspring    2004/10/14 15:50:32

  Modified:    cli/src/test/org/apache/commons/cli2/util
                        HelpFormatterTest.java
               cli/src/java/org/apache/commons/cli2/util HelpFormatter.java
  Log:
  HelpFormatter now applys a minimum description width of 1 character
  
  Revision  Changes    Path
  1.3       +57 -17    jakarta-commons/cli/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java
  
  Index: HelpFormatterTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HelpFormatterTest.java	22 Apr 2004 23:00:15 -0000	1.2
  +++ HelpFormatterTest.java	14 Oct 2004 22:50:31 -0000	1.3
  @@ -24,6 +24,7 @@
   
   import junit.framework.TestCase;
   
  +import org.apache.commons.cli2.Group;
   import org.apache.commons.cli2.Option;
   import org.apache.commons.cli2.OptionException;
   import org.apache.commons.cli2.builder.DefaultOptionBuilder;
  @@ -34,6 +35,7 @@
   public class HelpFormatterTest extends TestCase {
       private HelpFormatter helpFormatter;
       private Option verbose;
  +    private Group options;
   
       public void setUp() {
           helpFormatter = new HelpFormatter("|*", "*-*", "*|", 80);
  @@ -49,23 +51,24 @@
                   .withDescription("print the version information and exit")
                   .create();
   
  -        helpFormatter.setGroup(
  -            new GroupBuilder()
  -                .withName("options")
  -                .withOption(DefaultOptionTest.buildHelpOption())
  -                .withOption(ArgumentTest.buildTargetsArgument())
  -                .withOption(
  -                    new DefaultOptionBuilder()
  -                        .withLongName("diagnostics")
  -                        .withDescription("print information that might be helpful to diagnose or report problems.")
  -                        .create())
  -                .withOption(
  -                    new DefaultOptionBuilder()
  -                        .withLongName("projecthelp")
  -                        .withDescription("print project help information")
  -                        .create())
  -                .withOption(verbose)
  -                .create());
  +        options = new GroupBuilder()
  +            .withName("options")
  +            .withOption(DefaultOptionTest.buildHelpOption())
  +            .withOption(ArgumentTest.buildTargetsArgument())
  +            .withOption(
  +                new DefaultOptionBuilder()
  +                    .withLongName("diagnostics")
  +                    .withDescription("print information that might be helpful to diagnose or report problems.")
  +                    .create())
  +            .withOption(
  +                new DefaultOptionBuilder()
  +                    .withLongName("projecthelp")
  +                    .withDescription("print project help information")
  +                    .create())
  +            .withOption(verbose)
  +            .create();        
  +        
  +        helpFormatter.setGroup(options);
       }
   
       public void testPrint() throws IOException {
  @@ -190,6 +193,27 @@
           assertNull(reader.readLine());
       }
   
  +    public void testPrintHelp_TooNarrow() throws IOException {
  +        final StringWriter writer = new StringWriter();
  +        helpFormatter = new HelpFormatter("<","=",">",4);
  +        helpFormatter.setGroup(options);
  +        helpFormatter.setPrintWriter(new PrintWriter(writer));
  +        helpFormatter.printHelp();
  +        System.out.println(writer);
  +        final BufferedReader reader =
  +            new BufferedReader(new StringReader(writer.toString()));
  +        assertEquals(
  +            "<options              = >",
  +            reader.readLine());
  +        assertEquals(
  +            "<  --help (-?,-h)     =D>",
  +            reader.readLine());
  +        assertEquals(
  +            "<                     =i>",
  +            reader.readLine());
  +        // lots more lines unchecked
  +    }
  +
       public void testPrintException() throws IOException {
           final StringWriter writer = new StringWriter();
           helpFormatter.setPrintWriter(new PrintWriter(writer));
  @@ -322,6 +346,16 @@
           assertEquals("", i.next());
           assertFalse(i.hasNext());
       }
  +    
  +    public void testWrap_Below1Length() {
  +        try{
  +            HelpFormatter.wrap("Apache Software Foundation",-1);
  +            fail("IllegalArgumentException");
  +        }
  +        catch(IllegalArgumentException e) {
  +            assertEquals("width must be positive",e.getMessage());
  +        }
  +    }
   
       public void testPad() throws IOException {
           final StringWriter writer = new StringWriter();
  @@ -338,6 +372,12 @@
       public void testPad_TooLong() throws IOException {
           final StringWriter writer = new StringWriter();
           HelpFormatter.pad("hello world", 10, writer);
  +        assertEquals("hello world", writer.toString());
  +    }
  +    
  +    public void testPad_TooShort() throws IOException {
  +        final StringWriter writer = new StringWriter();
  +        HelpFormatter.pad("hello world", -5, writer);
           assertEquals("hello world", writer.toString());
       }
   }
  
  
  
  1.3       +7 -2      jakarta-commons/cli/src/java/org/apache/commons/cli2/util/HelpFormatter.java
  
  Index: HelpFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/util/HelpFormatter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HelpFormatter.java	22 Apr 2004 23:00:16 -0000	1.2
  +++ HelpFormatter.java	14 Oct 2004 22:50:31 -0000	1.3
  @@ -88,6 +88,7 @@
           final Set lineUsage = new HashSet();
           lineUsage.add(DisplaySetting.DISPLAY_ALIASES);
           lineUsage.add(DisplaySetting.DISPLAY_GROUP_NAME);
  +        lineUsage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
           DEFAULT_LINE_USAGE_SETTINGS = Collections.unmodifiableSet(lineUsage);
   
           final Set displayUsage = new HashSet(DisplaySetting.ALL);
  @@ -221,8 +222,8 @@
           for (int i = 0; i < usageWidth; i++) {
               blankBuffer.append(' ');
           }
  -        final int descriptionWidth =
  -            pageWidth - gutterCenter.length() - usageWidth;
  +        final int descriptionWidth = Math.max(1,
  +            pageWidth - gutterCenter.length() - usageWidth);
           for (final Iterator i = helpLines.iterator(); i.hasNext();) {
               final HelpLine helpLine = (HelpLine)i.next();
               final List descriptionLines =
  @@ -347,6 +348,10 @@
       }
   
       protected static List wrap(final String text, final int width) {
  +        if(width<1){
  +            throw new IllegalArgumentException("width must be positive");
  +        }
  +        
           if (text == null) {
               return Collections.singletonList("");
           }
  
  
  

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