From e6ce80c8e3da4b9ab5ff5a1ea96e117c82b95ca2 Mon Sep 17 00:00:00 2001 From: hanna Date: Mon, 18 May 2009 00:42:00 +0000 Subject: [PATCH] Fix for GSA-44...don't throw exception when user specifies -h. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@742 348d0f76-0448-11de-a6fe-93d51630548a --- .../utils/cmdLine/CommandLineProgram.java | 42 +++++++++++--- .../sting/utils/cmdLine/ParsingEngine.java | 56 +++++++++++++------ 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java index 845173396..3e8a24400 100644 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java @@ -166,17 +166,31 @@ public abstract class CommandLineProgram { if( clp.canAddArgumentsDynamically() ) { // if the command-line program can toss in extra args, fetch them and reparse the arguments. parser.parse(args); - parser.validate( EnumSet.of(ParsingEngine.ValidationType.InvalidArgument) ); + + // Allow invalid and missing required arguments to pass this validation step. + // - InvalidArgument in case these arguments are specified by plugins. + // - MissingRequiredArgument in case the user requested help. Handle that later, once we've + // determined the full complement of arguments. + parser.validate( EnumSet.of(ParsingEngine.ValidationType.MissingRequiredArgument, + ParsingEngine.ValidationType.InvalidArgument) ); parser.loadArgumentsIntoObject( clp ); Class[] argumentSources = clp.getArgumentSources(); for( Class argumentSource: argumentSources ) parser.addArgumentSource( clp.getArgumentSourceName(argumentSource), argumentSource ); parser.parse(args); + + if( isHelpPresent( clp, parser ) ) + printHelpAndExit( clp, parser ); + parser.validate(); } else { parser.parse(args); + + if( isHelpPresent( clp, parser ) ) + printHelpAndExit( clp, parser ); + parser.validate(); parser.loadArgumentsIntoObject( clp ); } @@ -202,12 +216,6 @@ public abstract class CommandLineProgram { //logger.removeAllAppenders(); } - // they asked for help, give it to them - if (clp.help) { - parser.printHelp( clp.getRunningInstructions() ); - System.exit(1); - } - // if they specify a log location, output our data there if (clp.toFile != null) { FileAppender appender = null; @@ -337,6 +345,26 @@ public abstract class CommandLineProgram { System.out.printf("%s%n", msg); } + /** + * Do a cursory search for the given argument. + * @param clp Instance of the command-line program. + * @param parser Parser + * @return True if help is present; false otherwise. + */ + private static boolean isHelpPresent( CommandLineProgram clp, ParsingEngine parser ) { + return parser.isArgumentPresent("help"); + } + + /** + * Print help and exit. + * @param clp Instance of the command-line program. + * @param parser True if help is present; false otherwise. + */ + private static void printHelpAndExit( CommandLineProgram clp, ParsingEngine parser ) { + parser.printHelp( clp.getRunningInstructions() ); + System.exit(0); + } + /** * used to indicate an error occured * @param msg the message to display diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java index f2a0291f4..07d22e6b0 100755 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java @@ -95,6 +95,18 @@ public class ParsingEngine { argumentDefinitions.add( new ArgumentDefinitionGroup(sourceName, argumentsFromSource) ); } + /** + * Do a cursory search to see if an argument with the given name is present. + * @param argumentFullName full name of the argument. + * @return True if the argument is present. False otherwise. + */ + public boolean isArgumentPresent( String argumentFullName ) { + ArgumentDefinition definition = + argumentDefinitions.findArgumentDefinition(argumentFullName,ArgumentDefinitions.FullNameDefinitionMatcher); + return argumentMatches.hasMatch(definition); + + } + /** * Parse the given set of command-line arguments, returning * an ArgumentMatches object describing the best fit of these @@ -214,26 +226,34 @@ public class ParsingEngine { * @param object Object into which to add arguments. */ public void loadArgumentsIntoObject( Object object ) { - for( ArgumentMatch match: argumentMatches ) { - ArgumentDefinition definition = match.definition; + for( ArgumentMatch match: argumentMatches ) + loadArgumentIntoObject( match, object ); + } - // A null definition might be in the list if some invalid arguments were passed in but we - // want to load in a subset of data for better error reporting. Ignore null definitions. - if( definition == null ) - continue; + /** + * Loads a single argument into the object. + * @param argument Argument to load into the object. + * @param object Target for the argument. + */ + public void loadArgumentIntoObject( ArgumentMatch argument, Object object ) { + ArgumentDefinition definition = argument.definition; - if( definition.sourceClass.isAssignableFrom(object.getClass()) ) { - try { - definition.sourceField.setAccessible(true); - if( !isArgumentFlag(definition) ) - definition.sourceField.set( object, constructFromString( definition.sourceField, match.values() ) ); - else - definition.sourceField.set( object, true ); - } - catch( IllegalAccessException ex ) { - //logger.fatal("processArgs: cannot convert field " + field.toString()); - throw new StingException("processArgs: Failed conversion " + ex.getMessage(), ex); - } + // A null definition might be in the list if some invalid arguments were passed in but we + // want to load in a subset of data for better error reporting. Ignore null definitions. + if( definition == null ) + return; + + if( definition.sourceClass.isAssignableFrom(object.getClass()) ) { + try { + definition.sourceField.setAccessible(true); + if( !isArgumentFlag(definition) ) + definition.sourceField.set( object, constructFromString( definition.sourceField, argument.values() ) ); + else + definition.sourceField.set( object, true ); + } + catch( IllegalAccessException ex ) { + //logger.fatal("processArgs: cannot convert field " + field.toString()); + throw new StingException("processArgs: Failed conversion " + ex.getMessage(), ex); } } }