Uploaded image for project: 'Commons CLI'
  1. Commons CLI
  2. CLI-38

[cli] HelpFormatter doesn't function correctly for options with only LongOpt

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.1
    • Component/s: CLI-1.x
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Bugzilla Id:
      33340

      Description

      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()); }

        Activity

        Hide
        ebourg Emmanuel Bourg added a comment -
            • COM-1878 has been marked as a duplicate of this bug. ***
        Show
        ebourg Emmanuel Bourg added a comment - COM-1878 has been marked as a duplicate of this bug. ***
        Hide
        steve.morin@gmail.com steve added a comment -

        Created an attachment (id=14157)
        PATCH but only a portion of a diff

        I created a fix for version 1.0 of cli but getting the latest code from nightly
        builds see that this bug has been fixed.

        Show
        steve.morin@gmail.com steve added a comment - Created an attachment (id=14157) PATCH but only a portion of a diff I created a fix for version 1.0 of cli but getting the latest code from nightly builds see that this bug has been fixed.
        Hide
        steve.morin@gmail.com steve added a comment -

        I checked and this fix only seems to be in nightly build could the same fix be
        updated and release as 1.0.1 or something because the nightly build is failing
        it's junit application test?

        Show
        steve.morin@gmail.com steve added a comment - I checked and this fix only seems to be in nightly build could the same fix be updated and release as 1.0.1 or something because the nightly build is failing it's junit application test?
        Hide
        sjr James Ring added a comment -

        I've just run up this test application, and it appears to work properly.

        This bug is pretty old now. Can we mark it as FIXED?

        Show
        sjr James Ring added a comment - I've just run up this test application, and it appears to work properly. This bug is pretty old now. Can we mark it as FIXED?
        Hide
        mvdb@apache.org Martin van den Bemt added a comment -

        No complaints anymore, so marking it as fixed.

        Show
        mvdb@apache.org Martin van den Bemt added a comment - No complaints anymore, so marking it as fixed.
        Hide
        brianegge Brian Egge added a comment -

        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()); + }

        +
        +}

        Show
        brianegge Brian Egge added a comment - 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()); + } + +}

          People

          • Assignee:
            Unassigned
            Reporter:
            scruffy323@aol.com steve
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development