commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Heuer (JIRA)" <j...@apache.org>
Subject [jira] Issue Comment Edited: (CLI-179) Needs "standalone" options that can be used even if required options are not set
Date Thu, 04 Jun 2009 19:55:07 GMT

    [ https://issues.apache.org/jira/browse/CLI-179?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12716380#action_12716380
] 

Michael Heuer edited comment on CLI-179 at 6/4/09 12:53 PM:
------------------------------------------------------------

This appears to be the same problem I posted about to commons-user earlier.  I will give your
workaround a try, Emmanuel.

http://mail-archives.apache.org/mod_mbox/commons-user/200903.mbox/%3CPine.GSO.4.44.0903121240051.16891-100000@shell3.shore.net%3E

      was (Author: heuermh):
    This appears to be the same problem I posted about to commons-user earlier.  I will give
your workaround a try, Emmanuel.

---
Date: Thu, 12 Mar 2009 13:02:44 -0500 (EST)
To: user@commons.apache.org
Subject: [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),  ...
  }
}

  
> Needs "standalone" options that can be used even if required options are not set
> --------------------------------------------------------------------------------
>
>                 Key: CLI-179
>                 URL: https://issues.apache.org/jira/browse/CLI-179
>             Project: Commons CLI
>          Issue Type: Improvement
>          Components: Parser
>    Affects Versions: 1.2
>            Reporter: Joël Royer
>             Fix For: 1.4
>
>
> it is currently impossible to have options like "help" or "version" if we also have some
required options. The parsing results in an error "Missing parameters".
> But for an "help" or "version" option, it would be usefull to display the wanted information,
even if required options are not set.

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


Mime
View raw message