package org.broadinstitute.sting.utils.cmdLine; import org.apache.commons.cli.*; import org.apache.log4j.Logger; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; /** * User: aaron * Date: Mar 19, 2009 * Time: 6:54:15 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. */ public class ArgumentParser { // what program are we parsing for private String programName; // our m_options private ArrayList m_option_names = new ArrayList(); // where we eventually want the values to land private HashMap m_storageLocations = new HashMap(); // create Options object protected Options m_options = new Options(); /** * our log, which we want to capture anything from org.broadinstitute.sting */ protected static Logger logger = Logger.getLogger(ArgumentParser.class); // the reference to the command line program to fill in Object prog; public ArgumentParser(String programName, Object prog) { this.programName = programName; this.prog = prog; } /** * print out the help information */ public void printHelp() { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(100, "java -Xmx4096m -jar dist/GenomeAnalysisTK.jar", "", m_options, "", true); } /** * addOptionalArg *

* Adds an optional argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname the field to set when we've parsed this option */ public void addOptionalArg(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.withLongOpt(name).withArgName(name) .hasArg() .withDescription(description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * Used locally to add to the options storage we have, for latter processing * * @param name the name of the option * @param letterform it's short form * @param fieldname what field it should be stuck into on the calling class * @param opt the option */ private void AddToOptionStorage(String name, String letterform, String fieldname, Option opt) { // add to the option list m_options.addOption(opt); // add the object with it's name to the storage location try { m_storageLocations.put(name, prog.getClass().getField(fieldname)); } catch (NoSuchFieldException e) { logger.fatal("Failed to find the field specified by the fieldname parameter."); throw new RuntimeException(e.getMessage()); } // add to the list of m_options m_option_names.add(letterform); } /** * addRequiredlArg *

* Adds a required argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname what field it should be stuck into on the calling class */ public void addRequiredlArg(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.isRequired() .withLongOpt(name) .withArgName(name) .hasArg() .withDescription("(Required Option) " + description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * addOptionalArg *

* Adds an optional argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname what field it should be stuck into on the calling class */ public void addOptionalArgList(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.withLongOpt(name).withArgName(name) .hasArgs() .withDescription(description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * addRequiredlArg *

* Adds a required argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname what field it should be stuck into on the calling class */ public void addRequiredlArgList(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.isRequired() .withLongOpt(name) .withArgName(name) .hasArgs() .withDescription("(Required Option) " + description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * addOptionalFlag *

* Adds an optional argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname what field it should be stuck into on the calling class */ public void addOptionalFlag(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.withLongOpt(name) .withDescription(description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * addRequiredlFlag *

* Adds a required argument to check on the command line * * @param name the name of the argument, the long name * @param letterform the short form * @param description the description of the argument * @param fieldname what field it should be stuck into on the calling class */ public void addRequiredlFlag(String name, String letterform, String description, String fieldname) { // we always want the help option to be available Option opt = OptionBuilder.isRequired() .withLongOpt(name) .withDescription("(Required Flag) " + description) .create(letterform); // add it to the option AddToOptionStorage(name, letterform, fieldname, opt); } /** * This function is called to validate all the arguments to the program. * If a required Arg isn't found, we generate the help message, and * exit the program * * @param args the command line arguments we recieved */ public void processArgs(String[] args) { CommandLineParser parser = new PosixParser(); try { Collection