From d8ec63e26ec64a8ad571e69e862306bcab94f73c Mon Sep 17 00:00:00 2001 From: Jarred Li Date: Sat, 14 Apr 2012 16:06:37 +0800 Subject: [PATCH] update hint message for mandatory options --- .../roo/shell/SimpleParser.java | 96 +++++++++++-------- 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/springframework/roo/shell/SimpleParser.java b/src/main/java/org/springframework/roo/shell/SimpleParser.java index 6167d5a8..79c7ed0d 100644 --- a/src/main/java/org/springframework/roo/shell/SimpleParser.java +++ b/src/main/java/org/springframework/roo/shell/SimpleParser.java @@ -64,40 +64,26 @@ public class SimpleParser implements Parser { } /** - * get all mandatory options key. For the options with multiple keys, the - * first key will be returned. + * get all mandatory options key. For the options with multiple keys, the + * keys will be in one row. * * @param cliOptions options * @return mandatory options key */ - private String[] getMandatoryOptions(Set cliOptions) { - return getMandatoryOptions(cliOptions, false); - } - - /** - * get all mandatory options key. - * - * @param cliOptions options - * @param includeMultipleKey If the options has multiple keys, whether return - * all of them or only the first one - * @return mandatory options key - */ - private String[] getMandatoryOptions(Set cliOptions, boolean includeMultipleKey) { - List mandatoryOptions = new ArrayList(); + private List> getMandatoryOptions(Set cliOptions) { + List> mandatoryOptions = new ArrayList>(); for (CliOption option : cliOptions) { if (option.mandatory()) { - if (includeMultipleKey) { - mandatoryOptions.addAll(Arrays.asList(option.key())); - } - else { - mandatoryOptions.add(option.key()[0]); - } + List keys = new ArrayList(); + keys.addAll(Arrays.asList(option.key())); + mandatoryOptions.add(keys); } } - return mandatoryOptions.toArray(new String[0]); + return mandatoryOptions; } + public ParseResult parse(final String rawInput) { synchronized (mutex) { Assert.notNull(rawInput, "Raw input required"); @@ -188,24 +174,7 @@ public class SimpleParser implements Parser { LOGGER.warning(message.toString()); } else { - if (sourcedFrom != null) { - LOGGER.warning("You must specify value for option '" + cliOption.key()[0] - + "' for this command"); - } - else { - StringBuilder builder = new StringBuilder(); - builder.append("You must specify option ("); - String[] mandatoryOptions = getMandatoryOptions(cliOptions); - for (int i = 0, j = mandatoryOptions.length; i < j; i++) { - builder.append("--"); - builder.append(mandatoryOptions[i]); - if (i < j - 1) { - builder.append(", "); - } - } - builder.append(") for this command"); - LOGGER.warning(builder.toString()); - } + printHintMessage(cliOptions, options); } return null; } @@ -295,6 +264,51 @@ public class SimpleParser implements Parser { } } + /** + * @param cliOptions + * @param options + */ + private void printHintMessage(final Set cliOptions, Map options) { + boolean hintForOptions = true; + + StringBuilder optionBuilder = new StringBuilder(); + optionBuilder.append("You must specify option ("); + + StringBuilder valueBuilder = new StringBuilder(); + valueBuilder.append("You must specify value for option '"); + + List> mandatoryOptions = getMandatoryOptions(cliOptions); + for (List keys : mandatoryOptions) { + boolean found = false; + for (String key : keys) { + if (options.containsKey(key)) { + if (StringUtils.isBlank(options.get(key))) { + valueBuilder.append(key); + valueBuilder.append("' for this command"); + hintForOptions = false; + } + found = true; + break; + } + } + if (!found) { + optionBuilder.append("--"); + optionBuilder.append(keys.get(0)); + optionBuilder.append(", "); + } + } + //remove the ", " in the end. + String hintForOption = optionBuilder.toString(); + hintForOption = hintForOption.substring(0,hintForOption.length()-2); + if (hintForOptions) { + LOGGER.warning(hintForOption + ") for this command"); + } + else { + LOGGER.warning(valueBuilder.toString()); + } + + } + /** * Normalises the given raw user input string ready for parsing *