Skip to content

Commit eab938a

Browse files
committed
Polish error messages
- Polish mandatory option message by using same format as in 3.0.x. (old parser) - Remove prefixed code/position parts from all messages coming from a parser. (new parser) - Fixes #815
1 parent d053fab commit eab938a

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/parser/MessageResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public static MessageResult of(ParserMessage parserMessage, int position, Object
4040
* @return a formatted message
4141
*/
4242
public String getMessage() {
43-
return parserMessage.formatMessage(position, inserts);
43+
return parserMessage.formatMessage(inserts);
4444
}
4545
}

spring-shell-core/src/main/java/org/springframework/shell/command/parser/Parser.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.Set;
2525
import java.util.stream.Collectors;
26-
import java.util.stream.Stream;
2726

2827
import org.springframework.core.ResolvableType;
2928
import org.springframework.core.convert.ConversionService;
@@ -37,6 +36,7 @@
3736
import org.springframework.shell.command.parser.Parser.ParseResult.ArgumentResult;
3837
import org.springframework.shell.command.parser.Parser.ParseResult.OptionResult;
3938
import org.springframework.shell.command.parser.ParserConfig.Feature;
39+
import org.springframework.util.StringUtils;
4040

4141
/**
4242
* Interface to parse command line arguments.
@@ -439,12 +439,19 @@ private List<MessageResult> validateOptionNotMissing(CommandRegistration registr
439439

440440
return requiredOptions2.stream()
441441
.map(o -> {
442-
String ln = o.getLongNames() != null
443-
? Stream.of(o.getLongNames()).collect(Collectors.joining(","))
444-
: "";
445-
String sn = o.getShortNames() != null ? Stream.of(o.getShortNames()).map(n -> Character.toString(n))
446-
.collect(Collectors.joining(",")) : "";
447-
return MessageResult.of(ParserMessage.MANDATORY_OPTION_MISSING, 0, ln, sn);
442+
String ins0 = "";
443+
if (o.getLongNames().length > 0) {
444+
ins0 = "--" + o.getLongNames()[0];
445+
}
446+
else if (o.getShortNames().length > 0) {
447+
ins0 = "-" + o.getShortNames()[0];
448+
}
449+
450+
String ins1 = "";
451+
if (StringUtils.hasText(o.getDescription())) {
452+
ins1 = ", " + o.getDescription();
453+
}
454+
return MessageResult.of(ParserMessage.MANDATORY_OPTION_MISSING, 0, ins0, ins1);
448455
})
449456
.collect(Collectors.toList());
450457
}

spring-shell-core/src/main/java/org/springframework/shell/command/parser/ParserMessage.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
public enum ParserMessage {
4040

4141
ILLEGAL_CONTENT_BEFORE_COMMANDS(Type.ERROR, 1000, "Illegal content before commands ''{0}''"),
42-
MANDATORY_OPTION_MISSING(Type.ERROR, 2000, "Missing mandatory option, longnames=''{0}'', shortnames=''{1}''"),
42+
MANDATORY_OPTION_MISSING(Type.ERROR, 2000, "Missing mandatory option ''{0}''{1}"),
4343
UNRECOGNISED_OPTION(Type.ERROR, 2001, "Unrecognised option ''{0}''"),
4444
ILLEGAL_OPTION_VALUE(Type.ERROR, 2002, "Illegal option value ''{0}'', reason ''{1}''"),
4545
NOT_ENOUGH_OPTION_ARGUMENTS(Type.ERROR, 2003, "Not enough arguments for option ''{0}'', requires at least ''{1}''"),
@@ -64,18 +64,40 @@ public Type getType() {
6464
return type;
6565
}
6666

67-
public String formatMessage(int position, Object... inserts) {
67+
/**
68+
* Format message without code and position parts.
69+
*
70+
* @param inserts the inserts
71+
* @return formatted message
72+
*/
73+
public String formatMessage(Object... inserts) {
74+
return formatMessage(false, -1, inserts);
75+
}
76+
77+
/**
78+
* Format message.
79+
*
80+
* <p>For example code and position 2000E:(pos 0):
81+
*
82+
* @param useCode Add code part
83+
* @param position position info, not printed if negative
84+
* @param inserts the inserts
85+
* @return formatted message
86+
*/
87+
public String formatMessage(boolean useCode, int position, Object... inserts) {
6888
StringBuilder msg = new StringBuilder();
69-
msg.append(code);
70-
switch (type) {
71-
case WARNING:
72-
msg.append("W");
73-
break;
74-
case ERROR:
75-
msg.append("E");
76-
break;
89+
if (useCode) {
90+
msg.append(code);
91+
switch (type) {
92+
case WARNING:
93+
msg.append("W");
94+
break;
95+
case ERROR:
96+
msg.append("E");
97+
break;
98+
}
99+
msg.append(":");
77100
}
78-
msg.append(":");
79101
if (position != -1) {
80102
msg.append("(pos ").append(position).append("): ");
81103
}

0 commit comments

Comments
 (0)