2009-03-23 05:06:22 +08:00
|
|
|
package org.broadinstitute.sting.utils.cmdLine;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.cli.*;
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
2009-03-26 04:11:31 +08:00
|
|
|
import java.lang.reflect.Array;
|
2009-03-23 05:06:22 +08:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2009-03-26 04:11:31 +08:00
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
|
import java.lang.reflect.ParameterizedType;
|
2009-03-23 05:06:22 +08:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.HashMap;
|
2009-03-26 11:11:56 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
2009-03-23 05:06:22 +08:00
|
|
|
|
2009-03-25 08:12:00 +08:00
|
|
|
import org.broadinstitute.sting.utils.Pair;
|
|
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
/**
|
|
|
|
|
* User: aaron
|
|
|
|
|
* Date: Mar 19, 2009
|
|
|
|
|
* Time: 6:54:15 PM
|
|
|
|
|
* <p/>
|
|
|
|
|
* 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.
|
|
|
|
|
* <p/>
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
// where we eventually want the values to land
|
2009-03-25 08:12:00 +08:00
|
|
|
private HashMap<String, Pair<Object,Field>> m_storageLocations = new HashMap<String, Pair<Object,Field>>();
|
2009-03-23 05:06:22 +08:00
|
|
|
|
|
|
|
|
// 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
|
2009-03-24 05:04:11 +08:00
|
|
|
Object prog;
|
2009-03-23 05:06:22 +08:00
|
|
|
|
2009-03-24 05:04:11 +08:00
|
|
|
public ArgumentParser(String programName, Object prog) {
|
2009-03-23 05:06:22 +08:00
|
|
|
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,
|
2009-03-25 05:16:12 +08:00
|
|
|
"java -Xmx4096m -jar dist/GenomeAnalysisTK.jar",
|
|
|
|
|
"",
|
|
|
|
|
m_options,
|
|
|
|
|
"",
|
|
|
|
|
true);
|
2009-03-23 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
/**
|
|
|
|
|
* addOptionalArg
|
|
|
|
|
* <p/>
|
|
|
|
|
* 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) {
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
// 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
|
2009-03-26 11:11:56 +08:00
|
|
|
AddToOptionStorage(opt, fieldname);
|
2009-03-23 05:06:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used locally to add to the options storage we have, for latter processing
|
|
|
|
|
*
|
|
|
|
|
* @param opt the option
|
2009-03-26 11:11:56 +08:00
|
|
|
* @param fieldname what field it should be stuck into on the calling class
|
2009-03-23 05:06:22 +08:00
|
|
|
*/
|
2009-03-26 11:11:56 +08:00
|
|
|
private void AddToOptionStorage(Option opt, String fieldname) {
|
|
|
|
|
AddToOptionStorage( opt, getField(prog, fieldname) );
|
2009-03-25 08:12:00 +08:00
|
|
|
}
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-25 08:12:00 +08:00
|
|
|
/**
|
|
|
|
|
* Used locally to add to the options storage we have, for latter processing
|
|
|
|
|
*
|
|
|
|
|
* @param opt the option
|
2009-03-26 11:11:56 +08:00
|
|
|
* @param field what field it should be stuck into on the calling class
|
2009-03-25 08:12:00 +08:00
|
|
|
*/
|
2009-03-26 11:11:56 +08:00
|
|
|
private void AddToOptionStorage(Option opt, Pair<Object,Field> field ) {
|
|
|
|
|
// first check to see if we've already added an option with the same name
|
|
|
|
|
if (m_options.hasOption( opt.getOpt() ))
|
|
|
|
|
throw new IllegalArgumentException(opt.getOpt() + " was already added as an option");
|
|
|
|
|
|
2009-03-26 11:22:30 +08:00
|
|
|
// Doesn't make much sense to have a single (ungrouped) required option. Force to unrequired.
|
|
|
|
|
if( !opt.hasArg() && opt.isRequired() )
|
|
|
|
|
opt.setRequired(false);
|
|
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
// add to the option list
|
|
|
|
|
m_options.addOption(opt);
|
|
|
|
|
|
|
|
|
|
// add the object with it's name to the storage location
|
2009-03-26 11:11:56 +08:00
|
|
|
m_storageLocations.put( opt.getLongOpt(), field );
|
|
|
|
|
}
|
2009-03-25 08:12:00 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
/**
|
|
|
|
|
* Used locally to add a group of mutually exclusive options to options storage.
|
|
|
|
|
* @param options A list of pairs of param, field to add.
|
|
|
|
|
*/
|
|
|
|
|
private void AddToOptionStorage( List<Pair<Option,Pair<Object,Field>>> options ) {
|
|
|
|
|
OptionGroup optionGroup = new OptionGroup();
|
|
|
|
|
boolean isRequired = true;
|
|
|
|
|
|
|
|
|
|
for( Pair<Option,Pair<Object,Field>> option: options ) {
|
|
|
|
|
if (m_options.hasOption(option.first.getOpt()) )
|
|
|
|
|
throw new IllegalArgumentException(option.first.getOpt() + " was already added as an option");
|
|
|
|
|
|
|
|
|
|
optionGroup.addOption(option.first);
|
|
|
|
|
m_storageLocations.put( option.first.getLongOpt(), option.second );
|
|
|
|
|
isRequired &= option.first.isRequired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optionGroup.setRequired(isRequired);
|
|
|
|
|
m_options.addOptionGroup(optionGroup);
|
2009-03-25 08:12:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Pair<Object,Field> getField( Object obj, String fieldName ) {
|
2009-03-23 05:06:22 +08:00
|
|
|
try {
|
2009-03-25 08:12:00 +08:00
|
|
|
return new Pair<Object,Field>( obj, obj.getClass().getField(fieldName) );
|
2009-03-23 05:06:22 +08:00
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
|
logger.fatal("Failed to find the field specified by the fieldname parameter.");
|
|
|
|
|
throw new RuntimeException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-25 08:12:00 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
/**
|
2009-03-25 08:12:00 +08:00
|
|
|
* addRequiredArg
|
2009-03-23 05:06:22 +08:00
|
|
|
* <p/>
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2009-03-25 08:12:00 +08:00
|
|
|
public void addRequiredArg(String name, String letterform, String description, String fieldname) {
|
2009-03-23 05:06:22 +08:00
|
|
|
// 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
|
2009-03-26 11:11:56 +08:00
|
|
|
AddToOptionStorage( opt, fieldname );
|
2009-03-23 05:06:22 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* addOptionalArg
|
|
|
|
|
* <p/>
|
|
|
|
|
* 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
|
2009-03-26 11:11:56 +08:00
|
|
|
AddToOptionStorage( opt, fieldname );
|
2009-03-23 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
/**
|
2009-03-25 08:12:00 +08:00
|
|
|
* addRequiredArg
|
2009-03-23 05:06:22 +08:00
|
|
|
* <p/>
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2009-03-25 08:12:00 +08:00
|
|
|
public void addRequiredArgList(String name, String letterform, String description, String fieldname) {
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
// 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
|
2009-03-26 11:11:56 +08:00
|
|
|
AddToOptionStorage(opt, fieldname);
|
2009-03-23 05:06:22 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* addOptionalFlag
|
|
|
|
|
* <p/>
|
|
|
|
|
* 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) {
|
2009-03-25 05:16:12 +08:00
|
|
|
|
|
|
|
|
// if they've passed a non-Boolean as a object, beat them
|
|
|
|
|
try {
|
|
|
|
|
if (!(prog.getClass().getField(fieldname).getType() == Boolean.class)) {
|
|
|
|
|
throw new IllegalArgumentException("Fields to addOptionalFlag must be of type Boolean");
|
|
|
|
|
}
|
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
|
throw new IllegalArgumentException("Fields to addOptionalFlag must exist!");
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
// we always want the help option to be available
|
|
|
|
|
Option opt = OptionBuilder.withLongOpt(name)
|
|
|
|
|
.withDescription(description)
|
|
|
|
|
.create(letterform);
|
|
|
|
|
|
2009-03-25 05:16:12 +08:00
|
|
|
|
2009-03-23 05:06:22 +08:00
|
|
|
// add it to the option
|
2009-03-26 11:11:56 +08:00
|
|
|
AddToOptionStorage( opt, fieldname );
|
2009-03-23 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2009-03-25 05:16:12 +08:00
|
|
|
*
|
2009-03-23 05:06:22 +08:00
|
|
|
* @param args the command line arguments we recieved
|
|
|
|
|
*/
|
2009-03-25 08:12:00 +08:00
|
|
|
public void processArgs(String[] args, boolean allowUnrecognized) throws ParseException {
|
|
|
|
|
OurPosixParser parser = new OurPosixParser();
|
|
|
|
|
Collection<Option> opts = m_options.getOptions();
|
2009-03-23 05:06:22 +08:00
|
|
|
|
|
|
|
|
try {
|
2009-03-25 08:12:00 +08:00
|
|
|
parser.parse(m_options, args, !allowUnrecognized);
|
|
|
|
|
}
|
|
|
|
|
catch (UnrecognizedOptionException e) {
|
|
|
|
|
// we don't care about unknown exceptions right now
|
2009-03-26 04:11:31 +08:00
|
|
|
if(!allowUnrecognized) {
|
|
|
|
|
logger.warn(e.getMessage());
|
2009-03-25 08:12:00 +08:00
|
|
|
throw e;
|
2009-03-26 04:11:31 +08:00
|
|
|
}
|
2009-03-25 08:12:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apache CLI can ignore unrecognized arguments with a boolean flag, but
|
|
|
|
|
// you can't get to the unparsed args. Override PosixParser with a class
|
|
|
|
|
// that can reach in and extract the protected command line.
|
|
|
|
|
// TODO: Holy crap this is wacky. Find a cleaner way.
|
|
|
|
|
CommandLine cmd = parser.getCmd();
|
|
|
|
|
|
|
|
|
|
// logger.info("We have " + opts.size() + " options");
|
|
|
|
|
for (Option opt : opts) {
|
|
|
|
|
if (cmd.hasOption(opt.getOpt())) {
|
2009-03-26 04:11:31 +08:00
|
|
|
//logger.info("looking at " + m_storageLocations.get(opt.getLongOpt()));
|
|
|
|
|
Object obj = m_storageLocations.get(opt.getLongOpt()).first;
|
|
|
|
|
Field field = m_storageLocations.get(opt.getLongOpt()).second;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (opt.hasArg())
|
|
|
|
|
field.set(obj, constructFromString(field, cmd.getOptionValues(opt.getOpt())));
|
|
|
|
|
else
|
2009-03-25 08:12:00 +08:00
|
|
|
field.set(obj, new Boolean(true));
|
2009-03-26 04:11:31 +08:00
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
logger.fatal("processArgs: cannot convert field " + field.toString());
|
|
|
|
|
throw new RuntimeException("processArgs: Failed conversion " + e.getMessage());
|
2009-03-23 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 04:11:31 +08:00
|
|
|
/**
|
|
|
|
|
* Simple class to wrap the posix parser and get access to the parsed cmd object.
|
|
|
|
|
*/
|
2009-03-25 08:12:00 +08:00
|
|
|
private class OurPosixParser extends PosixParser {
|
|
|
|
|
public CommandLine getCmd() { return cmd; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract arguments stored in annotations from fields of a given class.
|
2009-03-26 11:11:56 +08:00
|
|
|
* @param source Source of arguments, probably provided through Argument annotation.
|
2009-03-25 08:12:00 +08:00
|
|
|
*/
|
|
|
|
|
public void addArgumentSource( Object source ) {
|
|
|
|
|
Field[] fields = source.getClass().getFields();
|
2009-03-26 11:11:56 +08:00
|
|
|
|
|
|
|
|
for( Set<Field> optionGroup: groupExclusiveOptions(fields) ) {
|
|
|
|
|
List<Pair<Option,Pair<Object,Field>>> options = new ArrayList<Pair<Option,Pair<Object,Field>>>();
|
|
|
|
|
for( Field field: optionGroup ) {
|
|
|
|
|
Argument argument = field.getAnnotation(Argument.class);
|
|
|
|
|
Option option = createOptionFromField( source, field, argument );
|
|
|
|
|
options.add( new Pair<Option,Pair<Object,Field>>( option, new Pair<Object,Field>( source,field) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( options.size() == 1 )
|
|
|
|
|
AddToOptionStorage( options.get(0).first, new Pair<Object,Field>( source, options.get(0).second.second ) );
|
|
|
|
|
else {
|
|
|
|
|
AddToOptionStorage( options );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group mutually exclusive options together into sets. Non-exclusive options
|
|
|
|
|
* will be alone a set. Every option should appear in exactly one set.
|
|
|
|
|
* WARNING: Has no concept of nested dependencies.
|
|
|
|
|
* @param fields list of fields for which to check options.
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private List<Set<Field>> groupExclusiveOptions( Field[] fields ) {
|
|
|
|
|
List<Set<Field>> optionGroups = new ArrayList<Set<Field>>();
|
2009-03-25 08:12:00 +08:00
|
|
|
for(Field field: fields) {
|
2009-03-26 11:11:56 +08:00
|
|
|
Argument argument = field.getAnnotation(Argument.class);
|
|
|
|
|
if(argument == null)
|
2009-03-25 08:12:00 +08:00
|
|
|
continue;
|
|
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
String[] exclusives = argument.exclusive().split(",");
|
|
|
|
|
if( exclusives.length != 0 ) {
|
|
|
|
|
HashSet<Field> matchingFields = new HashSet<Field>();
|
|
|
|
|
|
|
|
|
|
// Find the set of all options exclusive to this one.
|
|
|
|
|
matchingFields.add( field );
|
|
|
|
|
for( Field candidate: fields ) {
|
|
|
|
|
for( String exclusive: exclusives ) {
|
|
|
|
|
if( candidate.getName().equals( exclusive.trim() ) )
|
|
|
|
|
matchingFields.add( candidate );
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-25 08:12:00 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
// Perform a set intersection to see whether this set of fields intersects
|
|
|
|
|
// with any existing set.
|
|
|
|
|
//
|
|
|
|
|
// If so, add any additional elements to the list.
|
|
|
|
|
boolean setExists = false;
|
|
|
|
|
for( Set<Field> optionGroup: optionGroups ) {
|
|
|
|
|
Set<Field> working = new HashSet<Field>(optionGroup);
|
|
|
|
|
working.retainAll( matchingFields );
|
|
|
|
|
if( working.size() > 0 ) {
|
|
|
|
|
optionGroup.addAll( matchingFields );
|
|
|
|
|
setExists = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-26 04:11:31 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
// Otherwise, add a new option group.
|
|
|
|
|
if( !setExists )
|
|
|
|
|
optionGroups.add( matchingFields );
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-26 04:11:31 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
return optionGroups;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a field with some annotations, create a command-line option. If not enough data is
|
|
|
|
|
* available to create a command-line option, return null.
|
|
|
|
|
* @param source Source class containing the field.
|
|
|
|
|
* @param field Field
|
|
|
|
|
* @return Option representing the field options.
|
|
|
|
|
*/
|
|
|
|
|
private Option createOptionFromField( Object source, Field field, Argument argument ) {
|
|
|
|
|
|
|
|
|
|
String fullName = (argument.fullName().length() != 0) ? argument.fullName() : field.getName().trim().toLowerCase();
|
|
|
|
|
String shortName = (argument.shortName().length() != 0) ? argument.shortName() : fullName.substring(0,1);
|
|
|
|
|
if(shortName.length() != 1)
|
|
|
|
|
throw new IllegalArgumentException("Invalid short name: " + shortName);
|
|
|
|
|
boolean isFlag = (field.getType() == Boolean.class) || (field.getType() == Boolean.TYPE);
|
|
|
|
|
boolean isCollection = field.getType().isArray() || Collection.class.isAssignableFrom(field.getType());
|
2009-03-25 08:12:00 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
if( isFlag && isCollection )
|
|
|
|
|
throw new IllegalArgumentException("Can't have an array of flags.");
|
2009-03-25 08:12:00 +08:00
|
|
|
|
2009-03-26 11:11:56 +08:00
|
|
|
String description = String.format("[%s] %s", source, argument.doc());
|
|
|
|
|
|
|
|
|
|
OptionBuilder ob = OptionBuilder.withLongOpt(fullName);
|
|
|
|
|
if( !isFlag ) {
|
|
|
|
|
ob = ob.withArgName(fullName);
|
|
|
|
|
ob = isCollection ? ob.hasArgs() : ob.hasArg();
|
2009-03-26 11:22:30 +08:00
|
|
|
}
|
|
|
|
|
if( argument.required() ) {
|
|
|
|
|
ob = ob.isRequired();
|
|
|
|
|
description = String.format("[%s] (Required Option) %s", source, argument.doc());
|
2009-03-25 08:12:00 +08:00
|
|
|
}
|
2009-03-26 11:11:56 +08:00
|
|
|
if( description.length() != 0 ) ob = ob.withDescription( description );
|
|
|
|
|
|
|
|
|
|
Option option = ob.create( shortName );
|
|
|
|
|
|
|
|
|
|
return option;
|
2009-03-25 08:12:00 +08:00
|
|
|
}
|
2009-03-23 05:06:22 +08:00
|
|
|
|
2009-03-26 04:11:31 +08:00
|
|
|
private Object constructFromString(Field f, String[] strs) {
|
|
|
|
|
Class type = f.getType();
|
|
|
|
|
|
|
|
|
|
if( Collection.class.isAssignableFrom(type) ) {
|
|
|
|
|
Collection collection = null;
|
|
|
|
|
Class containedType = null;
|
|
|
|
|
|
|
|
|
|
// If this is a parameterized collection, find the contained type. If blow up if only one type exists.
|
|
|
|
|
if( f.getGenericType() instanceof ParameterizedType ) {
|
|
|
|
|
ParameterizedType parameterizedType = (ParameterizedType)f.getGenericType();
|
|
|
|
|
if( parameterizedType.getActualTypeArguments().length > 1 )
|
|
|
|
|
throw new IllegalArgumentException("Unable to determine collection type of field: " + f.toString());
|
|
|
|
|
containedType = (Class)parameterizedType.getActualTypeArguments()[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
containedType = String.class;
|
|
|
|
|
|
|
|
|
|
// If this is a generic interface, pick a concrete implementation to create and pass back.
|
|
|
|
|
// Because of type erasure, don't worry about creating one of exactly the correct type.
|
|
|
|
|
if( Modifier.isInterface(type.getModifiers()) || Modifier.isAbstract(type.getModifiers()) )
|
|
|
|
|
{
|
|
|
|
|
if( java.util.List.class.isAssignableFrom(type) ) type = ArrayList.class;
|
|
|
|
|
else if( java.util.Queue.class.isAssignableFrom(type) ) type = java.util.ArrayDeque.class;
|
|
|
|
|
else if( java.util.Set.class.isAssignableFrom(type) ) type = java.util.TreeSet.class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
collection = (Collection)type.newInstance();
|
|
|
|
|
}
|
|
|
|
|
catch( Exception ex ) {
|
|
|
|
|
// Runtime exceptions are definitely unexpected parsing simple collection classes.
|
|
|
|
|
throw new IllegalArgumentException(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for( String str: strs )
|
|
|
|
|
collection.add( constructSingleElement(f,containedType,str) );
|
|
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
else if( type.isArray() ) {
|
|
|
|
|
Class containedType = type.getComponentType();
|
|
|
|
|
|
|
|
|
|
Object arr = Array.newInstance(containedType,strs.length);
|
|
|
|
|
for( int i = 0; i < strs.length; i++ )
|
|
|
|
|
Array.set( arr,i,constructSingleElement(f,containedType,strs[i]) );
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if( strs.length != 1 )
|
|
|
|
|
throw new IllegalArgumentException("Passed multiple arguments to an object expecting a single value.");
|
|
|
|
|
return constructSingleElement(f,type,strs[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds a single element of the given type.
|
|
|
|
|
* @param f Implies type of data to construct.
|
|
|
|
|
* @param str String representation of data.
|
|
|
|
|
* @return parsed form of String.
|
|
|
|
|
*/
|
|
|
|
|
private Object constructSingleElement(Field f, Class type, String str) {
|
2009-03-23 05:06:22 +08:00
|
|
|
// lets go through the types we support
|
|
|
|
|
if (type == Boolean.TYPE) {
|
|
|
|
|
boolean b = false;
|
|
|
|
|
if (str.toLowerCase().equals("true")) {
|
|
|
|
|
b = true;
|
|
|
|
|
}
|
|
|
|
|
Boolean bool = new Boolean(b);
|
|
|
|
|
return bool;
|
|
|
|
|
} else if (type == Integer.TYPE) {
|
|
|
|
|
Integer in = Integer.valueOf(str);
|
|
|
|
|
return in;
|
|
|
|
|
} else if (type == Float.TYPE) {
|
|
|
|
|
Float fl = Float.valueOf(str);
|
|
|
|
|
return fl;
|
2009-03-26 04:11:31 +08:00
|
|
|
}
|
|
|
|
|
else {
|
2009-03-23 05:06:22 +08:00
|
|
|
Constructor ctor = null;
|
|
|
|
|
try {
|
2009-03-26 04:11:31 +08:00
|
|
|
ctor = type.getConstructor(String.class);
|
2009-03-23 05:06:22 +08:00
|
|
|
return ctor.newInstance(str);
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
2009-03-24 03:03:59 +08:00
|
|
|
logger.fatal("constructFromString:NoSuchMethodException: cannot convert field " + f.toString());
|
|
|
|
|
throw new RuntimeException("constructFromString:NoSuchMethodException: Failed conversion " + e.getMessage());
|
2009-03-23 05:06:22 +08:00
|
|
|
} catch (IllegalAccessException e) {
|
2009-03-24 03:03:59 +08:00
|
|
|
logger.fatal("constructFromString:IllegalAccessException: cannot convert field " + f.toString());
|
|
|
|
|
throw new RuntimeException("constructFromString:IllegalAccessException: Failed conversion " + e.getMessage());
|
2009-03-23 05:06:22 +08:00
|
|
|
} catch (InvocationTargetException e) {
|
2009-03-24 03:03:59 +08:00
|
|
|
logger.fatal("constructFromString:InvocationTargetException: cannot convert field " + f.toString());
|
|
|
|
|
throw new RuntimeException("constructFromString:InvocationTargetException: Failed conversion " + e.getMessage());
|
2009-03-23 05:06:22 +08:00
|
|
|
} catch (InstantiationException e) {
|
2009-03-24 03:03:59 +08:00
|
|
|
logger.fatal("constructFromString:InstantiationException: cannot convert field " + f.toString());
|
|
|
|
|
throw new RuntimeException("constructFromString:InstantiationException: Failed conversion " + e.getMessage());
|
2009-03-23 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
ArgumentParser p = new ArgumentParser("CrapApp");
|
|
|
|
|
p.setupDefaultArgs();
|
2009-03-25 08:12:00 +08:00
|
|
|
p.addRequiredArg("Flag","F","a required arg");
|
|
|
|
|
p.addRequiredFlag("Sub","S","a required flag");
|
2009-03-23 05:06:22 +08:00
|
|
|
p.addOptionalArg("Boat","T","Maybe you want a boat?");
|
|
|
|
|
String[] str = {"--Flag","rrr","-T","ppp", "--Flag","ttt"};
|
|
|
|
|
p.processArgs(str);
|
|
|
|
|
Iterator<String> r = map.keySet().iterator();
|
|
|
|
|
while (r.hasNext()) {
|
|
|
|
|
String key = r.next();
|
|
|
|
|
String[] q = map.get(key);
|
|
|
|
|
if (q != null) {
|
|
|
|
|
for (String mystr : q) {
|
|
|
|
|
System.err.println("key: " + key + " val: " + mystr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-25 08:12:00 +08:00
|
|
|
*/
|