commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Roberts <rol...@astrofoto.org>
Subject Re: [CLI1.x] help option doesn't work if there is a required option
Date Thu, 19 Mar 2009 17:19:07 GMT
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


Mime
View raw message