diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/MessageResult.java b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/MessageResult.java index c3060cb4..1300efbe 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/MessageResult.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/MessageResult.java @@ -40,6 +40,6 @@ public record MessageResult(ParserMessage parserMessage, int position, Object[] * @return a formatted message */ public String getMessage() { - return parserMessage.formatMessage(position, inserts); + return parserMessage.formatMessage(inserts); } } diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/Parser.java b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/Parser.java index a24d22bd..4b2eac06 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/Parser.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/Parser.java @@ -23,7 +23,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; import org.springframework.core.ResolvableType; import org.springframework.core.convert.ConversionService; @@ -37,6 +36,7 @@ import org.springframework.shell.command.parser.Lexer.LexerResult; import org.springframework.shell.command.parser.Parser.ParseResult.ArgumentResult; import org.springframework.shell.command.parser.Parser.ParseResult.OptionResult; import org.springframework.shell.command.parser.ParserConfig.Feature; +import org.springframework.util.StringUtils; /** * Interface to parse command line arguments. @@ -439,12 +439,19 @@ public interface Parser { return requiredOptions2.stream() .map(o -> { - String ln = o.getLongNames() != null - ? Stream.of(o.getLongNames()).collect(Collectors.joining(",")) - : ""; - String sn = o.getShortNames() != null ? Stream.of(o.getShortNames()).map(n -> Character.toString(n)) - .collect(Collectors.joining(",")) : ""; - return MessageResult.of(ParserMessage.MANDATORY_OPTION_MISSING, 0, ln, sn); + String ins0 = ""; + if (o.getLongNames().length > 0) { + ins0 = "--" + o.getLongNames()[0]; + } + else if (o.getShortNames().length > 0) { + ins0 = "-" + o.getShortNames()[0]; + } + + String ins1 = ""; + if (StringUtils.hasText(o.getDescription())) { + ins1 = ", " + o.getDescription(); + } + return MessageResult.of(ParserMessage.MANDATORY_OPTION_MISSING, 0, ins0, ins1); }) .collect(Collectors.toList()); } diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/ParserMessage.java b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/ParserMessage.java index 5d62b282..9eb22488 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/parser/ParserMessage.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/parser/ParserMessage.java @@ -39,7 +39,7 @@ import java.text.MessageFormat; public enum ParserMessage { ILLEGAL_CONTENT_BEFORE_COMMANDS(Type.ERROR, 1000, "Illegal content before commands ''{0}''"), - MANDATORY_OPTION_MISSING(Type.ERROR, 2000, "Missing mandatory option, longnames=''{0}'', shortnames=''{1}''"), + MANDATORY_OPTION_MISSING(Type.ERROR, 2000, "Missing mandatory option ''{0}''{1}"), UNRECOGNISED_OPTION(Type.ERROR, 2001, "Unrecognised option ''{0}''"), ILLEGAL_OPTION_VALUE(Type.ERROR, 2002, "Illegal option value ''{0}'', reason ''{1}''"), NOT_ENOUGH_OPTION_ARGUMENTS(Type.ERROR, 2003, "Not enough arguments for option ''{0}'', requires at least ''{1}''"), @@ -64,18 +64,40 @@ public enum ParserMessage { return type; } - public String formatMessage(int position, Object... inserts) { + /** + * Format message without code and position parts. + * + * @param inserts the inserts + * @return formatted message + */ + public String formatMessage(Object... inserts) { + return formatMessage(false, -1, inserts); + } + + /** + * Format message. + * + *

For example code and position 2000E:(pos 0): + * + * @param useCode Add code part + * @param position position info, not printed if negative + * @param inserts the inserts + * @return formatted message + */ + public String formatMessage(boolean useCode, int position, Object... inserts) { StringBuilder msg = new StringBuilder(); - msg.append(code); - switch (type) { - case WARNING: - msg.append("W"); - break; - case ERROR: - msg.append("E"); - break; + if (useCode) { + msg.append(code); + switch (type) { + case WARNING: + msg.append("W"); + break; + case ERROR: + msg.append("E"); + break; + } + msg.append(":"); } - msg.append(":"); if (position != -1) { msg.append("(pos ").append(position).append("): "); }