You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Emmanuel Bourg <eb...@apache.org> on 2011/02/23 21:11:34 UTC

Re: [cli] help with required parameter without throwing exception

Hi Jordan,

Sorry for the late response. The solution is to put your required 
parameters into a group.

For example:

     OptionGroup group = new OptionGroup();
     group.addOption(OptionBuilder.create("help"));
     group.addOption(OptionBuilder.create("version"));
     group.addOption(OptionBuilder.create("file"));
     group.setRequired(true);

     Options options = new Options();
     options.addOption(OptionBuilder.create("verbose"));
     options.addOptionGroup(group);

In this case one of the 3 parameters 'help', 'version' or 'file' is 
required to validate the command line.

Does it work with your use case?

Emmanuel Bourg



Le 06/01/2011 20:34, Jordan Thompson a écrit :
> Hi there,
> My scenario is this: I have one or more required parameters and also have a
> help and a version parameter (obviously neither of these are required).
>
> If the user only passes in a -h or -v (the require parameter is missing) the
> parser will throw a ParseException and the CommandLine will be null.  I want
> to be able differentiate between a user passing a -h or -v.
>
> Is there a solution to this?
>
> thanks,
> Jordan
>
> Jordan R. Thompson
> Jordan@ThompCo.com
> www.ThompCo.com
>