Handle arity errors
- Introduce new error TooManyArgumentsOptionException and NotEnoughArgumentsOptionException. - Parser not tracks arity min/max and imposes if num of option arguments. - CommandParserExceptionResolver contains better error message handling for these containing more context for a user. - Fixes #614
This commit is contained in:
@@ -332,6 +332,9 @@ public interface CommandParser {
|
||||
return option.stream().flatMap(o -> {
|
||||
List<String> subArgs = lr.subList(1, lr.size());
|
||||
ConvertArgumentsHolder holder = convertArguments(o, subArgs);
|
||||
if (holder.error != null) {
|
||||
return Stream.of(ParserResult.of(o, subArgs, null, holder.error));
|
||||
}
|
||||
Object value = holder.value;
|
||||
if (conversionService != null && o.getType() != null && value != null) {
|
||||
if (conversionService.canConvert(value.getClass(), o.getType().getRawClass())) {
|
||||
@@ -433,6 +436,22 @@ public interface CommandParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (arityMax > 1 && arityMin > -1 && arityMax >= arityMin && (arguments.size() < arityMin || arguments.size() > arityMax)) {
|
||||
String ln = option.getLongNames() != null
|
||||
? Stream.of(option.getLongNames()).collect(Collectors.joining(","))
|
||||
: "";
|
||||
String sn = option.getShortNames() != null ? Stream.of(option.getShortNames())
|
||||
.map(n -> Character.toString(n)).collect(Collectors.joining(",")) : "";
|
||||
if (arguments.size() < arityMin) {
|
||||
String msg = String.format("Not enough arguments, longnames='%s', shortnames='%s'", ln, sn);
|
||||
return new ConvertArgumentsHolder(value, unmapped, new NotEnoughArgumentsOptionException(msg, option));
|
||||
}
|
||||
if (arguments.size() > arityMax) {
|
||||
String msg = String.format("Too many arguments, longnames='%s', shortnames='%s'", ln, sn);
|
||||
return new ConvertArgumentsHolder(value, unmapped, new TooManyArgumentsOptionException(msg, option));
|
||||
}
|
||||
}
|
||||
|
||||
if (type != null && type.isAssignableFrom(boolean.class)) {
|
||||
if (arguments.size() == 0) {
|
||||
value = true;
|
||||
@@ -469,12 +488,18 @@ public interface CommandParser {
|
||||
private class ConvertArgumentsHolder {
|
||||
Object value;
|
||||
final List<String> unmapped = new ArrayList<>();
|
||||
CommandParserException error;
|
||||
|
||||
ConvertArgumentsHolder(Object value, List<String> unmapped) {
|
||||
this(value, unmapped, null);
|
||||
}
|
||||
|
||||
ConvertArgumentsHolder(Object value, List<String> unmapped, CommandParserException error) {
|
||||
this.value = value;
|
||||
if (unmapped != null) {
|
||||
this.unmapped.addAll(unmapped);
|
||||
}
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -509,6 +534,34 @@ public interface CommandParser {
|
||||
}
|
||||
}
|
||||
|
||||
public static class OptionException extends CommandParserException {
|
||||
|
||||
private CommandOption option;
|
||||
|
||||
public OptionException(String message, CommandOption option) {
|
||||
super(message);
|
||||
this.option = option;
|
||||
}
|
||||
|
||||
public CommandOption getOption() {
|
||||
return option;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TooManyArgumentsOptionException extends OptionException {
|
||||
|
||||
public TooManyArgumentsOptionException(String message, CommandOption option) {
|
||||
super(message, option);
|
||||
}
|
||||
}
|
||||
|
||||
public static class NotEnoughArgumentsOptionException extends OptionException {
|
||||
|
||||
public NotEnoughArgumentsOptionException(String message, CommandOption option) {
|
||||
super(message, option);
|
||||
}
|
||||
}
|
||||
|
||||
public static class MissingOptionException extends CommandParserException {
|
||||
|
||||
private CommandOption option;
|
||||
|
||||
@@ -21,6 +21,8 @@ import org.jline.utils.AttributedStyle;
|
||||
|
||||
import org.springframework.shell.command.CommandExecution.CommandParserExceptionsException;
|
||||
import org.springframework.shell.command.CommandParser.MissingOptionException;
|
||||
import org.springframework.shell.command.CommandParser.NotEnoughArgumentsOptionException;
|
||||
import org.springframework.shell.command.CommandParser.TooManyArgumentsOptionException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -44,6 +46,18 @@ public class CommandParserExceptionResolver implements CommandExceptionResolver
|
||||
handleShort(builder, option);
|
||||
}
|
||||
}
|
||||
else if (e instanceof NotEnoughArgumentsOptionException neaoe) {
|
||||
CommandOption option = neaoe.getOption();
|
||||
if (option.getLongNames().length > 0) {
|
||||
handleLongNotEnough(builder, option);
|
||||
}
|
||||
}
|
||||
else if (e instanceof TooManyArgumentsOptionException tmaoe) {
|
||||
CommandOption option = tmaoe.getOption();
|
||||
if (option.getLongNames().length > 0) {
|
||||
handleLongTooMany(builder, option);
|
||||
}
|
||||
}
|
||||
else {
|
||||
builder.append(new AttributedString(e.getMessage(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)));
|
||||
}
|
||||
@@ -78,4 +92,30 @@ public class CommandParserExceptionResolver implements CommandExceptionResolver
|
||||
buf.append(".");
|
||||
builder.append(new AttributedString(buf.toString(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)));
|
||||
}
|
||||
|
||||
private static void handleLongNotEnough(AttributedStringBuilder builder, CommandOption option) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("Not enough arguments --");
|
||||
buf.append(option.getLongNames()[0]);
|
||||
buf.append(" requires at least " + option.getArityMin());
|
||||
if (StringUtils.hasText(option.getDescription())) {
|
||||
buf.append(", ");
|
||||
buf.append(option.getDescription());
|
||||
}
|
||||
buf.append(".");
|
||||
builder.append(new AttributedString(buf.toString(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)));
|
||||
}
|
||||
|
||||
private static void handleLongTooMany(AttributedStringBuilder builder, CommandOption option) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("Too many arguments --");
|
||||
buf.append(option.getLongNames()[0]);
|
||||
buf.append(" requires at most " + option.getArityMax());
|
||||
if (StringUtils.hasText(option.getDescription())) {
|
||||
buf.append(", ");
|
||||
buf.append(option.getDescription());
|
||||
}
|
||||
buf.append(".");
|
||||
builder.append(new AttributedString(buf.toString(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user