update parser to accept specifiedDefaultValue in the option

This commit is contained in:
Jarred Li
2012-04-26 15:20:38 +08:00
parent bd86827209
commit cdf665e4f1
2 changed files with 14 additions and 7 deletions

View File

@@ -18,9 +18,9 @@ public class HelloWorldCommands implements CommandMarker {
@CliCommand(value = "hw-echo", help = "Print a hello world message")
public void config(
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
@CliOption(key = { "name1","name11" }, mandatory = true, help = "The hello world name1 ") final String name1,
@CliOption(key = { "name1"}, mandatory = true, help = "The hello world name1 ") final String name1,
@CliOption(key = { "name2","name22" }, mandatory = true, help = "The hello world name2 ") final String name2,
@CliOption(key = { "time" }, mandatory = false, help = "The hello world time ") final String time,
@CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "The hello world time ") final String time,
@CliOption(key = { "location" }, mandatory = false, help = "The hello world location ") final String location) {
System.out.println("Hello world " + message + ", name1: " + name1 +
", name2:" + name2 + ". time:" + time + ".location: " + location);

View File

@@ -181,10 +181,17 @@ public class SimpleParser implements Parser {
// Ensure the user specified a value if the value is mandatory or
// key and value must appear in pair
boolean mandatory = StringUtils.isBlank(value) && cliOption.mandatory();
boolean inPair = StringUtils.isBlank(value) && options.containsKey(sourcedFrom);
if (mandatory || inPair) {
boolean specifiedKey = StringUtils.isBlank(value) && options.containsKey(sourcedFrom);
boolean specifiedKeyWithoutValue = false;
if(specifiedKey){
value = cliOption.specifiedDefaultValue();
if("__NULL__".equals(value)){
specifiedKeyWithoutValue = true;
}
}
if (mandatory || specifiedKeyWithoutValue) {
if ("".equals(cliOption.key()[0])) {
StringBuilder message = new StringBuilder("You must specify a default option ");
StringBuilder message = new StringBuilder("You should specify a default option ");
if (cliOption.key().length > 1) {
message.append("(otherwise known as option '").append(cliOption.key()[1]).append("') ");
}
@@ -290,10 +297,10 @@ public class SimpleParser implements Parser {
boolean hintForOptions = true;
StringBuilder optionBuilder = new StringBuilder();
optionBuilder.append("You must specify option (");
optionBuilder.append("You should specify option (");
StringBuilder valueBuilder = new StringBuilder();
valueBuilder.append("You must specify value for option '");
valueBuilder.append("You should specify value for option '");
List<List<String>> optionsKeys = getOptionsKeys(cliOptions,true);
for (List<String> keys : optionsKeys) {