commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Roberts <>
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) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ProcessIEC.class.getName(), options, true);

		       PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                             RL Enterprises                            6818 Madeline Court                           Brooklyn, NY 11220

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message