Check for proper error output in case of boolean args with parameter specified.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@599 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
hanna 2009-05-05 23:08:48 +00:00
parent b0cdba8bb3
commit 2ee9374975
3 changed files with 33 additions and 2 deletions

View File

@ -216,6 +216,14 @@ class ArgumentDefinition {
Class argumentType = sourceField.getType();
return Collection.class.isAssignableFrom(argumentType) || sourceField.getType().isArray();
}
/**
* Is this argument a flag (meaning a boolean value whose presence indicates 'true').
* @return True if this argument is a flag.
*/
public boolean isFlag() {
return (sourceField.getType() == Boolean.class) || (sourceField.getType() == Boolean.TYPE);
}
}
/**

View File

@ -177,12 +177,13 @@ class ArgumentMatch {
/**
* Does this argument already have a value at the given site?
* Arguments are only allowed to be single-valued.
* Arguments are only allowed to be single-valued per site, and
* flags aren't allowed a value at all.
* @param index Index at which to check for values.
* @return True if the argument has a value at the given site. False otherwise.
*/
public boolean hasValueAtSite( int index ) {
return indices.get(index) != null && indices.get(index).size() >= 1;
return (indices.get(index) != null && indices.get(index).size() >= 1) || isArgumentFlag();
}
/**
@ -197,4 +198,12 @@ class ArgumentMatch {
}
return values;
}
/**
* Convenience method returning true if the definition is a flag.
* @return True if definition is known to be a flag; false if not known to be a flag.
*/
private boolean isArgumentFlag() {
return definition != null && definition.isFlag();
}
}

View File

@ -345,4 +345,18 @@ public class ParsingEngineTest extends BaseTest {
@Argument(doc="my arg")
Integer myArg;
}
@Test(expected=InvalidArgumentValueException.class)
public void booleanWithParameterTest() {
final String[] commandLine = new String[] {"--mybool", "true"};
parsingEngine.addArgumentSources( BooleanArgProvider.class );
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
parsingEngine.validate(argumentMatches);
}
private class BooleanArgProvider {
@Argument(doc="my bool")
boolean myBool;
}
}