diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java index 495229f3c..51d32d002 100644 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineProgram.java @@ -1,6 +1,8 @@ package org.broadinstitute.sting.utils.cmdLine; import org.apache.log4j.*; +import org.broadinstitute.sting.utils.JVMUtils; +import org.broadinstitute.sting.utils.StingException; import java.io.IOException; import java.text.DateFormat; @@ -73,12 +75,43 @@ public abstract class CommandLineProgram { required=false) protected Boolean debugMode = false; + /** + * this is used to indicate if they've asked for help + */ + @Argument(fullName="help",shortName="h",doc="Generate this help message",required=false) + public Boolean help = false; + /** * our logging output patterns */ private static String patternString = "%p %m %n"; private static String debugPatternString = "%n[level] %p%n[date]\t\t %d{dd MMM yyyy HH:mm:ss,SSS} %n[class]\t\t %C %n[location]\t %l %n[line number]\t %L %n[message]\t %m %n"; + /** + * Retrieves text from the application regarding what parameters to put on the + * JVM command line to run this application. + * @return helpful instructions on the class / jar to run. + */ + protected String getRunningInstructions() { + // Default implementation to find a command line that makes sense. + // If the user is running from a jar, return '-jar '; otherwise + // return the full class name. + String runningInstructions = null; + try { + runningInstructions = JVMUtils.getLocationFor( getClass() ).getName(); + } + catch( IOException ex ) { + throw new StingException("Unable to determine running instructions", ex); + } + + if( runningInstructions.endsWith(".jar") ) + runningInstructions = String.format("-jar %s", runningInstructions); + else + runningInstructions = getClass().getName(); + + return runningInstructions; + } + /** * Will this application want to vary its argument list dynamically? * If so, parse the command-line options and then prompt the subclass to return @@ -109,12 +142,6 @@ public abstract class CommandLineProgram { protected abstract int execute(); - /** - * this is used to indicate if they've asked for help - */ - @Argument(fullName="help",shortName="h",doc="Generate this help message",required=false) - public Boolean help = false; - /** * This function is called to start processing the command line, and kick * off the execute message of the program. @@ -177,7 +204,7 @@ public abstract class CommandLineProgram { // they asked for help, give it to them if (clp.help) { - parser.printHelp(); + parser.printHelp( clp.getRunningInstructions() ); System.exit(1); } @@ -206,7 +233,7 @@ public abstract class CommandLineProgram { } catch (ParseException e) { logger.fatal("Unable to pass command line arguments: " + e.getMessage() ); - clp.parser.printHelp(); + clp.parser.printHelp( clp.getRunningInstructions() ); } catch (Exception e) { // we catch all exceptions here. if it makes it to this level, we're in trouble. Let's bail! diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/HelpFormatter.java b/java/src/org/broadinstitute/sting/utils/cmdLine/HelpFormatter.java index f440fc4c6..d42d13ff3 100755 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/HelpFormatter.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/HelpFormatter.java @@ -42,12 +42,12 @@ public class HelpFormatter { * Prints the help, given a collection of argument definitions. * @param argumentDefinitions Argument definitions for which help should be printed. */ - public void printHelp( ArgumentDefinitions argumentDefinitions ) { + public void printHelp( String runningInstructions, ArgumentDefinitions argumentDefinitions ) { SortedSet mainArguments = getMainArguments( argumentDefinitions ); Map> pluginsByGroup = getPluginArguments( argumentDefinitions ); System.out.printf("%s%s%n", - getSynopsis(mainArguments,pluginsByGroup), + getSynopsis(runningInstructions,mainArguments,pluginsByGroup), getDetailed(mainArguments,pluginsByGroup) ); } @@ -89,15 +89,18 @@ public class HelpFormatter { /** * Gets the synopsis: the actual command to run. * @param mainArguments Main program arguments. - * @praam pluginArgumentGroups Groups of plugin arguments + * @param runningInstructions Instructions on how to run hte application. + * @param pluginArgumentGroups Groups of plugin arguments * @return A synopsis line. */ - private String getSynopsis( SortedSet mainArguments, Map> pluginArgumentGroups ) { + private String getSynopsis( String runningInstructions, + SortedSet mainArguments, + Map> pluginArgumentGroups ) { // Build out the synopsis all as one long line. StringBuilder lineBuilder = new StringBuilder(); Formatter lineFormatter = new Formatter( lineBuilder ); - lineFormatter.format("java -jar dist/GenomeAnalysisTK.jar"); + lineFormatter.format("java %s", runningInstructions); List argumentDefinitions = new ArrayList(); argumentDefinitions.addAll( mainArguments ); diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java index c47cb2a69..80b5515f5 100755 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java @@ -218,8 +218,8 @@ public class ParsingEngine { /** * Prints out the help associated with these command-line argument definitions. */ - public void printHelp() { - new HelpFormatter().printHelp(argumentDefinitions); + public void printHelp( String runningInstructions ) { + new HelpFormatter().printHelp(runningInstructions,argumentDefinitions); } /**