You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Michael Heuer <he...@acm.org> on 2009/03/12 19:02:44 UTC

[CLI1.x] help option doesn't work if there is a required option

Hello,

My use case is -h,--help text prints to STDOUT and command line errors
print to STDERR.

This works fine in the simple case

Options options = new Options();
HelpFormatter helpFormatter = new HelpFormatter();
Option help = new Option("h", "help", false, "print help text to STDOUT");
help.setRequired(false);
options.addOption(help);

try
{
  CommandLineParser parser = new PosixParser();
  CommandLine commandLine = parser.parse(options, args);
  if (commandLine.hasOption(help.getOpt()))
  {
    helpFormatter.printHelp(...
  }
  ...
}
catch (ParseException e)
{
  System.err.println(e.getMessage() + "\n");
  helpFormatter.printHelp(new PrintWriter(System.err, true),  ...
}


But if there is a missing required option, the help part is unreachable,
since a MissingOptionException is thrown

Options options = new Options();
HelpFormatter helpFormatter = new HelpFormatter();
Option help = new Option("h", "help", false, "print help text to STDOUT");
help.setRequired(false);
Option foo = new Option(...);
foo.setRequired(true);
options.addOption(help);
options.addOption(foo);

try
{
  CommandLineParser parser = new PosixParser();
  CommandLine commandLine = parser.parse(options, args);
  if (commandLine.hasOption(help.getOpt()))
  {
    helpFormatter.printHelp(...
  }
  ...
}
catch (ParseException e)
{
  System.err.println(e.getMessage() + "\n");
  helpFormatter.printHelp(new PrintWriter(System.err, true),  ...
}


I could move the help part to the catch block, but there is no way to
query whether the help option was found.

catch (ParseException e)
{
  if ( help was found )
  {
    helpFormatter.printHelp(...
  }
  else
  {
    System.err.println(e.getMessage() + "\n");
    helpFormatter.printHelp(new PrintWriter(System.err, true),  ...
  }
}

   michael


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


Re: [CLI1.x] help option doesn't work if there is a required option

Posted by Roland Roberts <ro...@astrofoto.org>.
Oops, I just realized that my example was with CLI 2.0, but the idea is 
the same.

roland

-- 
		       PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                             RL Enterprises
roland@rlenter.com                            6818 Madeline Court
roland@astrofoto.org                           Brooklyn, NY 11220


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


Re: [CLI1.x] help option doesn't work if there is a required option

Posted by Michael Heuer <he...@acm.org>.
Roland Roberts wrote:

> Michael Heuer wrote:
> > Hello,
> >
> > My use case is -h,--help text prints to STDOUT and command line errors print to STDERR.
> [...]
> > But if there is a missing required option, the help part is unreachable, since a MissingOptionException is thrown
> >
> Yes, it does and in that case you need to handle the exception and call
> the helpformatter yourself.  Here's an (incomplete) extract from some of
> my working code:
>
>         CommandLineParser parser = new GnuParser();
>         MyOptions opt = new MyOptions();
>         try {
>             // parse the command line arguments
>             CommandLine line = parser.parse(options, args);
>
>             // validate that block-size has been set
>             if (line.hasOption("h")) {
>                 HelpFormatter formatter = new HelpFormatter();
>                 formatter.printHelp("java " +
> ProcessIEC.class.getName(), options);
>                 return null;
>             }
>
>         [...]
>
>         } catch (org.apache.commons.cli.ParseException exp) {
>             System.out.println(exp.getMessage());
>             HelpFormatter formatter = new HelpFormatter();
>             formatter.printHelp(ProcessIEC.class.getName(), options, true);
>             throw(exp);
>         }
>

Thanks for the reply Roland, but you may have missed the point of my use
case.  Once I catch the exception there is no way to determine whether or
not the help option was specified.

   michael


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


Re: [CLI1.x] help option doesn't work if there is a required option

Posted by Roland Roberts <ro...@astrofoto.org>.
Michael Heuer wrote:
> Hello,
>
> My use case is -h,--help text prints to STDOUT and command line errors print to STDERR.
[...]
> But if there is a missing required option, the help part is unreachable, since a MissingOptionException is thrown
>   
Yes, it does and in that case you need to handle the exception and call 
the helpformatter yourself.  Here's an (incomplete) extract from some of 
my working code:

        CommandLineParser parser = new GnuParser();
        MyOptions opt = new MyOptions();
        try {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args);

            // validate that block-size has been set
            if (line.hasOption("h")) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("java " + 
ProcessIEC.class.getName(), options);
                return null;
            }

        [...]

        } catch (org.apache.commons.cli.ParseException exp) {
            System.out.println(exp.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ProcessIEC.class.getName(), options, true);
            throw(exp);
        }
       
roland

-- 
		       PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                             RL Enterprises
roland@rlenter.com                            6818 Madeline Court
roland@astrofoto.org                           Brooklyn, NY 11220


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