commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Heuer <heue...@acm.org>
Subject Re: [CLI1.x] help option doesn't work if there is a required option
Date Thu, 19 Mar 2009 18:04:59 GMT
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


Mime
View raw message