Oops. While writing command-line argument docs, I realized I introduced
a regression in default value handling. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@226 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
875802e8fc
commit
36f851362e
|
|
@ -24,4 +24,5 @@ public @interface Argument {
|
||||||
String doc() default "";
|
String doc() default "";
|
||||||
boolean required() default true;
|
boolean required() default true;
|
||||||
String exclusive() default "";
|
String exclusive() default "";
|
||||||
|
String defaultValue() default "";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -297,17 +297,17 @@ public class ArgumentParser {
|
||||||
|
|
||||||
// logger.info("We have " + opts.size() + " options");
|
// logger.info("We have " + opts.size() + " options");
|
||||||
for (Option opt : opts) {
|
for (Option opt : opts) {
|
||||||
|
//logger.info("looking at " + m_storageLocations.get(opt.getLongOpt()));
|
||||||
|
Field field = m_storageLocations.get(opt.getLongOpt());
|
||||||
|
|
||||||
|
// Check to see if the object contains the specified field. Iterate through
|
||||||
|
// the array rather than doing a name lookup in case field names overlap between
|
||||||
|
// multiple classes in the application.
|
||||||
|
List<Field> fieldsInObj = Arrays.asList(obj.getClass().getFields());
|
||||||
|
if( !fieldsInObj.contains(field) )
|
||||||
|
continue;
|
||||||
|
|
||||||
if (cmd.hasOption(opt.getOpt())) {
|
if (cmd.hasOption(opt.getOpt())) {
|
||||||
//logger.info("looking at " + m_storageLocations.get(opt.getLongOpt()));
|
|
||||||
Field field = m_storageLocations.get(opt.getLongOpt());
|
|
||||||
|
|
||||||
// Check to see if the object contains the specified field. Iterate through
|
|
||||||
// the array rather than doing a name lookup in case field names overlap between
|
|
||||||
// multiple classes in the application.
|
|
||||||
List<Field> fieldsInObj = Arrays.asList(obj.getClass().getFields());
|
|
||||||
if( !fieldsInObj.contains(field) )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (opt.hasArg())
|
if (opt.hasArg())
|
||||||
field.set(obj, constructFromString(field, cmd.getOptionValues(opt.getOpt())));
|
field.set(obj, constructFromString(field, cmd.getOptionValues(opt.getOpt())));
|
||||||
|
|
@ -318,9 +318,45 @@ public class ArgumentParser {
|
||||||
throw new RuntimeException("processArgs: Failed conversion " + e.getMessage());
|
throw new RuntimeException("processArgs: Failed conversion " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if( hasDefaultValue( field ) ) {
|
||||||
|
try {
|
||||||
|
field.set(obj, constructFromString(field, new String[] { getDefaultValue(field) }) );
|
||||||
|
}
|
||||||
|
catch (IllegalAccessException e) {
|
||||||
|
logger.fatal("processArgs: cannot convert field " + field.toString());
|
||||||
|
throw new RuntimeException("processArgs: Failed conversion " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether this field is annotated with a default value.
|
||||||
|
* @param f the field to check for a default.
|
||||||
|
* @return whether a default value exists.
|
||||||
|
*/
|
||||||
|
private boolean hasDefaultValue( Field f ) {
|
||||||
|
return getDefaultValue( f ) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the String representation for the default value of a given argument.
|
||||||
|
* @return The string of a default value, or null if no default exists.
|
||||||
|
*/
|
||||||
|
private String getDefaultValue( Field f ) {
|
||||||
|
Argument arg = f.getAnnotation( Argument.class );
|
||||||
|
if( arg == null )
|
||||||
|
return null;
|
||||||
|
|
||||||
|
String defaultValue = arg.defaultValue().trim();
|
||||||
|
if( defaultValue.length() == 0 )
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple class to wrap the posix parser and get access to the parsed cmd object.
|
* Simple class to wrap the posix parser and get access to the parsed cmd object.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue