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
This commit is contained in:
parent
d35e20ce21
commit
e6ce80c8e3
|
|
@ -166,17 +166,31 @@ public abstract class CommandLineProgram {
|
||||||
if( clp.canAddArgumentsDynamically() ) {
|
if( clp.canAddArgumentsDynamically() ) {
|
||||||
// if the command-line program can toss in extra args, fetch them and reparse the arguments.
|
// if the command-line program can toss in extra args, fetch them and reparse the arguments.
|
||||||
parser.parse(args);
|
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 );
|
parser.loadArgumentsIntoObject( clp );
|
||||||
|
|
||||||
Class[] argumentSources = clp.getArgumentSources();
|
Class[] argumentSources = clp.getArgumentSources();
|
||||||
for( Class argumentSource: argumentSources )
|
for( Class argumentSource: argumentSources )
|
||||||
parser.addArgumentSource( clp.getArgumentSourceName(argumentSource), argumentSource );
|
parser.addArgumentSource( clp.getArgumentSourceName(argumentSource), argumentSource );
|
||||||
parser.parse(args);
|
parser.parse(args);
|
||||||
|
|
||||||
|
if( isHelpPresent( clp, parser ) )
|
||||||
|
printHelpAndExit( clp, parser );
|
||||||
|
|
||||||
parser.validate();
|
parser.validate();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
parser.parse(args);
|
parser.parse(args);
|
||||||
|
|
||||||
|
if( isHelpPresent( clp, parser ) )
|
||||||
|
printHelpAndExit( clp, parser );
|
||||||
|
|
||||||
parser.validate();
|
parser.validate();
|
||||||
parser.loadArgumentsIntoObject( clp );
|
parser.loadArgumentsIntoObject( clp );
|
||||||
}
|
}
|
||||||
|
|
@ -202,12 +216,6 @@ public abstract class CommandLineProgram {
|
||||||
//logger.removeAllAppenders();
|
//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 they specify a log location, output our data there
|
||||||
if (clp.toFile != null) {
|
if (clp.toFile != null) {
|
||||||
FileAppender appender = null;
|
FileAppender appender = null;
|
||||||
|
|
@ -337,6 +345,26 @@ public abstract class CommandLineProgram {
|
||||||
System.out.printf("%s%n", msg);
|
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
|
* used to indicate an error occured
|
||||||
* @param msg the message to display
|
* @param msg the message to display
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,18 @@ public class ParsingEngine {
|
||||||
argumentDefinitions.add( new ArgumentDefinitionGroup(sourceName, argumentsFromSource) );
|
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
|
* Parse the given set of command-line arguments, returning
|
||||||
* an ArgumentMatches object describing the best fit of these
|
* an ArgumentMatches object describing the best fit of these
|
||||||
|
|
@ -214,19 +226,28 @@ public class ParsingEngine {
|
||||||
* @param object Object into which to add arguments.
|
* @param object Object into which to add arguments.
|
||||||
*/
|
*/
|
||||||
public void loadArgumentsIntoObject( Object object ) {
|
public void loadArgumentsIntoObject( Object object ) {
|
||||||
for( ArgumentMatch match: argumentMatches ) {
|
for( ArgumentMatch match: argumentMatches )
|
||||||
ArgumentDefinition definition = match.definition;
|
loadArgumentIntoObject( match, object );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
// A null definition might be in the list if some invalid arguments were passed in but we
|
// 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.
|
// want to load in a subset of data for better error reporting. Ignore null definitions.
|
||||||
if( definition == null )
|
if( definition == null )
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
if( definition.sourceClass.isAssignableFrom(object.getClass()) ) {
|
if( definition.sourceClass.isAssignableFrom(object.getClass()) ) {
|
||||||
try {
|
try {
|
||||||
definition.sourceField.setAccessible(true);
|
definition.sourceField.setAccessible(true);
|
||||||
if( !isArgumentFlag(definition) )
|
if( !isArgumentFlag(definition) )
|
||||||
definition.sourceField.set( object, constructFromString( definition.sourceField, match.values() ) );
|
definition.sourceField.set( object, constructFromString( definition.sourceField, argument.values() ) );
|
||||||
else
|
else
|
||||||
definition.sourceField.set( object, true );
|
definition.sourceField.set( object, true );
|
||||||
}
|
}
|
||||||
|
|
@ -236,7 +257,6 @@ public class ParsingEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints out the help associated with these command-line argument definitions.
|
* Prints out the help associated with these command-line argument definitions.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue