Better support for required mutually exclusive options.
git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@199 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
08ece8df79
commit
2808fd4bbd
|
|
@ -117,6 +117,10 @@ public class ArgumentParser {
|
||||||
if (m_options.hasOption( opt.getOpt() ))
|
if (m_options.hasOption( opt.getOpt() ))
|
||||||
throw new IllegalArgumentException(opt.getOpt() + " was already added as an option");
|
throw new IllegalArgumentException(opt.getOpt() + " was already added as an option");
|
||||||
|
|
||||||
|
// Doesn't make much sense to have a single (ungrouped) required option. Force to unrequired.
|
||||||
|
if( !opt.hasArg() && opt.isRequired() )
|
||||||
|
opt.setRequired(false);
|
||||||
|
|
||||||
// add to the option list
|
// add to the option list
|
||||||
m_options.addOption(opt);
|
m_options.addOption(opt);
|
||||||
|
|
||||||
|
|
@ -253,42 +257,8 @@ public class ArgumentParser {
|
||||||
|
|
||||||
// add it to the option
|
// add it to the option
|
||||||
AddToOptionStorage( opt, fieldname );
|
AddToOptionStorage( opt, fieldname );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* addRequiredFlag
|
|
||||||
* <p/>
|
|
||||||
* Adds a required argument to check on the command line
|
|
||||||
*
|
|
||||||
* @param name the name of the argument, the long name
|
|
||||||
* @param letterform the short form
|
|
||||||
* @param description the description of the argument
|
|
||||||
* @param fieldname what field it should be stuck into on the calling class
|
|
||||||
*/
|
|
||||||
public void addRequiredFlag(String name, String letterform, String description, String fieldname) {
|
|
||||||
|
|
||||||
// if they've passed a non-Boolean as a object, beat them
|
|
||||||
try {
|
|
||||||
if (!(prog.getClass().getField(fieldname).getType() == Boolean.class)) {
|
|
||||||
throw new IllegalArgumentException("Fields to addRequiredlFlag must be of type Boolean");
|
|
||||||
}
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
throw new IllegalArgumentException("Fields to addRequiredlFlag must exist!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// we always want the help option to be available
|
|
||||||
Option opt = OptionBuilder.isRequired()
|
|
||||||
.withLongOpt(name)
|
|
||||||
.withDescription("(Required Flag) " + description)
|
|
||||||
.create(letterform);
|
|
||||||
|
|
||||||
// add it to the option
|
|
||||||
AddToOptionStorage( opt, fieldname );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is called to validate all the arguments to the program.
|
* This function is called to validate all the arguments to the program.
|
||||||
* If a required Arg isn't found, we generate the help message, and
|
* If a required Arg isn't found, we generate the help message, and
|
||||||
|
|
@ -442,11 +412,11 @@ public class ArgumentParser {
|
||||||
if( !isFlag ) {
|
if( !isFlag ) {
|
||||||
ob = ob.withArgName(fullName);
|
ob = ob.withArgName(fullName);
|
||||||
ob = isCollection ? ob.hasArgs() : ob.hasArg();
|
ob = isCollection ? ob.hasArgs() : ob.hasArg();
|
||||||
|
}
|
||||||
if( argument.required() ) {
|
if( argument.required() ) {
|
||||||
ob = ob.isRequired();
|
ob = ob.isRequired();
|
||||||
description = String.format("[%s] (Required Option) %s", source, argument.doc());
|
description = String.format("[%s] (Required Option) %s", source, argument.doc());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if( description.length() != 0 ) ob = ob.withDescription( description );
|
if( description.length() != 0 ) ob = ob.withDescription( description );
|
||||||
|
|
||||||
Option option = ob.create( shortName );
|
Option option = ob.create( shortName );
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue