From 36f851362e5be044f11291e50f78a9d62f61db65 Mon Sep 17 00:00:00 2001 From: hanna Date: Mon, 30 Mar 2009 18:51:39 +0000 Subject: [PATCH] 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 --- .../sting/utils/cmdLine/Argument.java | 1 + .../sting/utils/cmdLine/ArgumentParser.java | 56 +++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/Argument.java b/java/src/org/broadinstitute/sting/utils/cmdLine/Argument.java index 3a863c820..eb28eec85 100755 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/Argument.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/Argument.java @@ -24,4 +24,5 @@ public @interface Argument { String doc() default ""; boolean required() default true; String exclusive() default ""; + String defaultValue() default ""; } diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java index 9e0d9a8cc..9c69a7c36 100644 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java @@ -297,17 +297,17 @@ public class ArgumentParser { // logger.info("We have " + opts.size() + " options"); 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 fieldsInObj = Arrays.asList(obj.getClass().getFields()); + if( !fieldsInObj.contains(field) ) + continue; + 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 fieldsInObj = Arrays.asList(obj.getClass().getFields()); - if( !fieldsInObj.contains(field) ) - continue; - try { if (opt.hasArg()) field.set(obj, constructFromString(field, cmd.getOptionValues(opt.getOpt()))); @@ -318,9 +318,45 @@ public class ArgumentParser { 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. */