package org.broadinstitute.sting.utils.cmdLine; import org.apache.log4j.*; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.EnumSet; import java.util.Enumeration; /** * User: aaron * Date: Mar 19, 2009 * Time: 3:54:56 PM *
* The Broad Institute * SOFTWARE COPYRIGHT NOTICE AGREEMENT * This software and its documentation are copyright 2009 by the * Broad Institute/Massachusetts Institute of Technology. All rights are reserved. * * This software is supplied without any warranty or guaranteed support whatsoever. Neither * the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. * * * This class is our implementation of the command line parser, similar to Pickard's. We instead * support GNU style command line arguements, and use this class to setup the global parser. */ public abstract class CommandLineProgram { /** * The command-line program and the arguments it returned. */ private ParsingEngine parser = null; /** * our log, which we want to capture anything from org.broadinstitute.sting */ private static Logger logger = Logger.getRootLogger();// .getLogger(CommandLineProgram.class); /** * the default log level */ @Argument(fullName="logging_level", shortName="l", doc="Set the minimum level of logging, i.e. setting INFO get's you INFO up to FATAL, setting ERROR gets you ERROR and FATAL level logging. (DEBUG, INFO, WARN, ERROR, FATAL, OFF). ", required=false) protected String logging_level = "ERROR"; /** * where to send the output of our logger */ @Argument(fullName="log_to_file", shortName="log", doc="Set the logging location", required=false) protected String toFile = null; /** * do we want to silence the command line output */ @Argument(fullName="quiet_output_mode", shortName="quiet", doc="Set the logging to quiet mode, no output to stdout", required=false) protected Boolean quietMode = false; /** * do we want to generate debugging information with the logs */ @Argument(fullName="debug_mode", shortName="debug", doc="Set the logging file string to include a lot of debugging information (SLOW!)", 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 = "%-5p %d{HH:mm:ss,SSS} %C{1} - %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"; /** * Allows a given application to return a brief description of itself. * @return An ApplicationDetails object describing the current application. Should not be null. */ protected ApplicationDetails getApplicationDetails() { return new ApplicationDetails( ApplicationDetails.createDefaultHeader(getClass()), ApplicationDetails.createDefaultRunningInstructions(getClass()) ); } /** * Will this application want to vary its argument list dynamically? * If so, parse the command-line options and then prompt the subclass to return * a list of argument providers. * @return Whether the application should vary command-line arguments dynamically. */ protected boolean canAddArgumentsDynamically() { return false; } /** * Provide a list of object to inspect, looking for additional command-line arguments. * @return A list of objects to inspect. */ protected Class[] getArgumentSources() { return new Class[] {}; } /** * Allows arguments to be hijacked by subclasses of the program before being placed * into plugin classes. * @param source Source class for the argument. * @param targetInstance Instance into which the value should be ultimately injected. * @param value Value to inject. * @return True if the particular field has been hijacked; false otherwise. */ protected boolean intercept( ArgumentSource source, Object targetInstance, Object value ) { return false; } /** * Name this argument source. Provides the (full) class name as a default. * @param source The argument source. * @return a name for the argument source. */ protected String getArgumentSourceName( Class source ) { return source.toString(); } /** * this is the function that the inheriting class can expect to have called * when all the argument processing is done * * @return the return code to exit the program with */ protected abstract int execute(); static { // setup a basic log configuration BasicConfigurator.configure(); } /** * This function is called to start processing the command line, and kick * off the execute message of the program. * * @param clp the command line program to execute * @param args the command line arguments passed in */ public static void start(CommandLineProgram clp, String[] args) { try { // setup our log layout PatternLayout layout = new PatternLayout(); // setup the parser ParsingEngine parser = clp.parser = new ParsingEngine(clp); parser.addArgumentSource( clp.getClass() ); // process the args if( clp.canAddArgumentsDynamically() ) { // if the command-line program can toss in extra args, fetch them and reparse the arguments. parser.parse(args); // 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 ); } // if we're in debug mode, set the mode up if (clp.debugMode) { //logger.info("Setting debug"); layout.setConversionPattern(debugPatternString); } else { //logger.info("not Setting debug"); layout.setConversionPattern(patternString); } // now set the layout of all the loggers to our layout Enumeration