From cdf665e4f183d0f553cfa602ba784ab5c3b19910 Mon Sep 17 00:00:00 2001 From: Jarred Li Date: Thu, 26 Apr 2012 15:20:38 +0800 Subject: [PATCH] update parser to accept specifiedDefaultValue in the option --- .../helloworld/commands/HelloWorldCommands.java | 4 ++-- .../springframework/roo/shell/SimpleParser.java | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/HelloWorldCommands.java b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/HelloWorldCommands.java index f4cf6ae5..4d696223 100644 --- a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/HelloWorldCommands.java +++ b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/HelloWorldCommands.java @@ -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); diff --git a/src/main/java/org/springframework/roo/shell/SimpleParser.java b/src/main/java/org/springframework/roo/shell/SimpleParser.java index 5f7f3819..9fcf6b60 100644 --- a/src/main/java/org/springframework/roo/shell/SimpleParser.java +++ b/src/main/java/org/springframework/roo/shell/SimpleParser.java @@ -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> optionsKeys = getOptionsKeys(cliOptions,true); for (List keys : optionsKeys) {