[
https://issues.apache.org/jira/browse/CLI-179?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12716380#action_12716380
]
Michael Heuer commented on CLI-179:
-----------------------------------
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: [email protected]
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.