You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Brian Egge (JIRA)" <ji...@apache.org> on 2007/05/15 23:25:16 UTC

[jira] Commented: (CLI-38) [cli] HelpFormatter doesn't function correctly for options with only LongOpt

    [ https://issues.apache.org/jira/browse/CLI-38?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12496141 ] 

Brian Egge commented on CLI-38:
-------------------------------

Below is a test which shows this issue is fixed.  (Issue is closed, so I can't attach it.)  Additionally, I found a transient problem with the OptionBuilder class.  If you run this test in isolation it will fail, but when run with the other tests it passes.  This is because OptionBuilder doesn't initialize itself the it's used.  

Index: src/java/org/apache/commons/cli/OptionBuilder.java
===================================================================
--- src/java/org/apache/commons/cli/OptionBuilder.java	(revision 537541)
+++ src/java/org/apache/commons/cli/OptionBuilder.java	(working copy)
@@ -60,6 +60,7 @@
     private OptionBuilder()
     {
         // hide the constructor
+        reset();  // ensure arg is initialized.
     }
 
     /**
Index: src/test/org/apache/commons/cli/bug/BugCLI38Test.java
===================================================================
--- src/test/org/apache/commons/cli/bug/BugCLI38Test.java	(revision 0)
+++ src/test/org/apache/commons/cli/bug/BugCLI38Test.java	(revision 0)
@@ -0,0 +1,95 @@
+/*
+ * 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.bug;
+
+import junit.framework.TestCase;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.OptionGroup;
+import org.apache.commons.cli.Options;
+
+import java.io.StringWriter;
+import java.io.PrintWriter;
+
+/**
+ * http://issues.apache.org/bugzilla/show_bug.cgi?id=33340
+ */
+public class BugCLI38Test
+    extends TestCase
+{
+    private Options options;
+
+    private OptionGroup ogmain;
+
+    public BugCLI38Test()
+    {
+        super();
+        options = new Options();
+        ogmain = new OptionGroup();
+        ogmain.setRequired( true );
+    }
+
+
+    public void testPrintHelp()
+    {
+        options.addOption( OptionBuilder.withDescription( "add a parameter form name=value." ).hasArg().withLongOpt(
+            "params" ).create() );
+        options.addOption( OptionBuilder.withDescription( "recipient's email" ).hasArg().create( 'r' ) );
+        options.addOption(
+            OptionBuilder.withDescription( "flight which is the index into the email being sent" ).withLongOpt(
+                "flight" ).hasArg().create( 'f' ) );
+        options.addOption(
+            OptionBuilder.withDescription( "test mode - makes no permanent changes." ).withLongOpt( "test" ).create(
+                't' ) );
+
+        ogmain.addOption(
+            OptionBuilder.withDescription( "Print this usage information." ).withLongOpt( "help" ).create( 'h' ) );
+        ogmain.addOption(
+            OptionBuilder.withDescription( "send email command" ).withLongOpt( "send" ).hasArg().create( 's' ) );
+
+        ogmain.addOption(
+            OptionBuilder.withDescription( "add user to email queue." ).withLongOpt( "add" ).create( 'a' ) );
+        ogmain.addOption(
+            OptionBuilder.withDescription( "delete user from email queue." ).withLongOpt( "delete" ).create( 'd' ) );
+        ogmain.addOption( OptionBuilder.withDescription( "shutdown rpc server." ).withLongOpt( "shutdown" ).create() );
+
+        options.addOption( OptionBuilder.withDescription( "hostname for the XmlRpc client to connect to." ).withLongOpt(
+            "hostname" ).create() );
+        options.addOption(
+            OptionBuilder.withDescription( "port for the xmlrpc client to use." ).withLongOpt( "port" ).create() );
+        options.addOption(
+            OptionBuilder.withDescription( "additional xmlrpc connection" ).withLongOpt( "urlpath" ).create() );
+
+        options.addOptionGroup( this.ogmain );
+        HelpFormatter helpFormatter = new HelpFormatter();
+        StringWriter writer = new StringWriter();
+        PrintWriter pw = new PrintWriter(writer);
+        helpFormatter.printHelp( pw, 80, "Bug33340Test", "header", options, 0, 0, "footer" );
+        pw.flush();
+        String expected = "usage: Bug33340Test\n" + "header\n" +
+            "-t,--test        test mode - makes no permanent changes.\n" +
+            "-a,--add         add user to email queue.\n" + "-d,--delete      delete user from email queue.\n" +
+            "-f,--flight <arg>flight which is the index into the email being sent\n" +
+            "-h,--help        Print this usage information.\n" +
+            "   --hostname    hostname for the XmlRpc client to connect to.\n" +
+            "   --params <arg>add a parameter form name=value.\n" +
+            "   --port        port for the xmlrpc client to use.\n" + "   --shutdown    shutdown rpc server.\n" +
+            "   --urlpath     additional xmlrpc connection\n" + "-r <arg>         recipient's email\n" +
+            "-s,--send <arg>  send email command\n" + "footer\n";
+        assertEquals(expected, writer.getBuffer().toString());
+    }
+
+}


> [cli] HelpFormatter doesn't function correctly for options with only LongOpt
> ----------------------------------------------------------------------------
>
>                 Key: CLI-38
>                 URL: https://issues.apache.org/jira/browse/CLI-38
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x
>    Affects Versions: 1.0
>         Environment: Operating System: All
> Platform: All
>            Reporter: steve
>             Fix For: 1.1
>
>         Attachments: proposed-patch.txt
>
>
> The following doesn't work (org.apache.commons.cli.HelpFormatter
> )correctly and doesn't print out the help menu of options
> that only specify LongOpt.
> The output is:
>  [java] usage: -a | -d | --shutdown  |  -s | -h [-r <email>] [-f <flight>] [-t]
>      [java] VendMore - Email Management System
>      [java]  -t,--test       test mode - makes no permanent changes.
>      [java]     --shutdown   shutdown rpc server.
>      [java]  -a,--add        add user to email queue.
>      [java]  -d,--delete     delete user from email queue.
>      [java]  -f,--flight     flight which is the index into the email being sent
>      [java]  -h,--help       Print this usage information.
>      [java]  -r              recipient's email
>      [java]  -s,--send       send email command
>      [java]  For more information, see Steve Morin
>      [java] Java Result: 1
> it is missing the following options when printing the help display.
>    options.addOption(OptionBuilder.withDescription("hostname for the XmlRpc
> client to connect to.").withLongOpt("hostname").create());
>         options.addOption(OptionBuilder.withDescription("port for the xmlrpc
> client to use.").withLongOpt("port").create());
>         options.addOption(OptionBuilder.withDescription("additional xmlrpc
> connection").withLongOpt("urlpath").create());
>              options.addOption(OptionBuilder.withDescription("add a parameter
> form name=value.").hasArg().withLongOpt("params").create());
> example of the offending code
>     private CommandLineParser cmd;
>     private Options options;
>     private OptionGroup ogmain;
>     
>     public CmdLineArg() {
>         super();
>         cmd = new BasicParser();
>         options = new Options();
>         ogmain = new OptionGroup();
>         ogmain.setRequired(true);
>         localinit();
>         init();
>     }
>  public CommandLine run(String[] args) {
>         
>         try {
>              options.addOption(OptionBuilder.withDescription("add a parameter
> form name=value.").hasArg().withLongOpt("params").create());
>              options.addOption(OptionBuilder.withDescription("recipient's
> email").hasArg().create('r'));
>              options.addOption(OptionBuilder.withDescription("flight which is
> the index into the email being sent").withLongOpt("flight").hasArg().create('f'));
>              options.addOption(OptionBuilder.withDescription("test mode - makes
> no permanent changes.").withLongOpt("test").create('t'));
>         
>         
>              ogmain.addOption(OptionBuilder.withDescription("Print this usage
> information.").withLongOpt("help").create('h'));
>              ogmain.addOption(OptionBuilder.withDescription("send email
> command").withLongOpt("send").hasArg().create('s'));
>         ogmain.addOption(OptionBuilder.withDescription("add user to email
> queue.").withLongOpt("add").create('a'));
>         ogmain.addOption(OptionBuilder.withDescription("delete user from email
> queue.").withLongOpt("delete").create('d'));
>         ogmain.addOption(OptionBuilder.withDescription("shutdown rpc
> server.").withLongOpt("shutdown").create());
>         
>         options.addOption(OptionBuilder.withDescription("hostname for the XmlRpc
> client to connect to.").withLongOpt("hostname").create());
>         options.addOption(OptionBuilder.withDescription("port for the xmlrpc
> client to use.").withLongOpt("port").create());
>         options.addOption(OptionBuilder.withDescription("additional xmlrpc
> connection").withLongOpt("urlpath").create());
>             options.addOptionGroup(this.ogmain);
>             return cmd.parse(options,args);
>         } catch (ParseException e) {
>             printUsage();
>             System.exit(1);
>             return null;
>         }
>     }
>     
>     protected void printUsage(){
>         HelpFormatter helpFormatter = new HelpFormatter();
>         helpFormatter.printHelp(getUSAGE(),getHEADER(),options,getFOOTER());
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


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