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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to