}
private static void processCommand(ProcessedCommand processedCommand, Class<? extends Command> clazz) throws CommandLineParserException {
for(Field field : clazz.getDeclaredFields()) {
Option o;
OptionGroup og;
OptionList ol;
Arguments a;
if((o = field.getAnnotation(Option.class)) != null) {
OptionType optionType;
if(o.hasValue())
optionType = OptionType.NORMAL;
else
optionType = OptionType.BOOLEAN;
if(o.name() == null || o.name().length() < 1) {
processedCommand.addOption(
/*
o.shortName(), field.getName(), o.description(),
o.argument(), o.required(), ',', o.defaultValue(),
field.getType(), field.getName(), optionType, o.converter(),
o.completer(), o.validator(), o.activator(), o.renderer(), o.overrideRequired());
*/
new ProcessedOptionBuilder()
.shortName(o.shortName())
.name(field.getName())
.description(o.description())
.required(o.required())
.valueSeparator(',')
.addAllDefaultValues(o.defaultValue())
.type(field.getType())
.fieldName(field.getName())
.optionType(optionType)
.converter(o.converter())
.completer(o.completer())
.validator(o.validator())
.activator(o.activator())
.renderer(o.renderer())
.overrideRequired(o.overrideRequired())
.create()
);
}
else {
processedCommand.addOption(
//o.shortName(), o.name(), o.description(),
//o.argument(), o.required(), ',', o.defaultValue(),
//field.getType(), field.getName(), optionType, o.converter(),
//o.completer(), o.validator(), o.activator(), o.renderer(), o.overrideRequired());
new ProcessedOptionBuilder()
.shortName(o.shortName())
.name(o.name())
.description(o.description())
.required(o.required())
.valueSeparator(',')
.addAllDefaultValues(o.defaultValue())
.type(field.getType())
.fieldName(field.getName())
.optionType(optionType)
.converter(o.converter())
.completer(o.completer())
.validator(o.validator())
.activator(o.activator())
.renderer(o.renderer())
.overrideRequired(o.overrideRequired())
.create());
}
}
else if((ol = field.getAnnotation(OptionList.class)) != null) {
if(!Collection.class.isAssignableFrom(field.getType()))
throw new CommandLineParserException("OptionGroup field must be instance of Collection");
Class type = Object.class;
if(field.getGenericType() != null) {
ParameterizedType listType = (ParameterizedType) field.getGenericType();
type = (Class) listType.getActualTypeArguments()[0];
}
if(ol.name() == null || ol.name().length() < 1) {
processedCommand.addOption(
new ProcessedOptionBuilder()
.shortName(ol.shortName())
.name(field.getName())
.description(ol.description())
.required(ol.required())
.valueSeparator(ol.valueSeparator())
.addAllDefaultValues(ol.defaultValue())
.type(type)
.fieldName(field.getName())
.optionType(OptionType.LIST)
.converter(ol.converter())
.completer(ol.completer())
.validator(ol.validator())
.activator(ol.activator())
.renderer(ol.renderer()).create());
}
else {
processedCommand.addOption(
new ProcessedOptionBuilder()
.shortName(ol.shortName())
.name(ol.name())
.description(ol.description())
.required(ol.required())
.valueSeparator(ol.valueSeparator())
.addAllDefaultValues(ol.defaultValue())
.type(type)
.fieldName(field.getName())
.optionType(OptionType.LIST)
.converter(ol.converter())
.completer(ol.completer())
.validator(ol.validator())
.activator(ol.activator())
.renderer(ol.renderer()).create());
}
}
else if((og = field.getAnnotation(OptionGroup.class)) != null) {
if(!Map.class.isAssignableFrom(field.getType()))
throw new CommandLineParserException("OptionGroup field must be instance of Map");
Class type = Object.class;
if(field.getGenericType() != null) {
ParameterizedType listType = (ParameterizedType) field.getGenericType();
type = (Class) listType.getActualTypeArguments()[1];
}
String name;
if(og.name() == null || og.name().length() < 1)
name = field.getName();
else
name = og.name();
processedCommand.addOption( new ProcessedOptionBuilder()
.shortName(og.shortName())
.name(name)
.description(og.description())
.required(og.required())
.valueSeparator(',')
.addAllDefaultValues(og.defaultValue())
.type(type)
.fieldName(field.getName())
.optionType(OptionType.GROUP)
.converter(og.converter())
.completer(og.completer())
.validator(og.validator())
.activator(og.activator())
.renderer(og.renderer())
.create());
}
else if((a = field.getAnnotation(Arguments.class)) != null) {
if(!Collection.class.isAssignableFrom(field.getType()))