Fix situation where no command executor is chosen when there is ambiguity. Also fix subcommand default executor not being used as backup

This commit is contained in:
themode 2021-02-22 09:42:48 +01:00
parent b4d9cecdce
commit b4508692a5
2 changed files with 19 additions and 17 deletions

View File

@ -115,27 +115,15 @@ public class CommandDispatcher {
args = new String[0];
}
// Find the used syntax
ParsedCommand parsedCommand = findParsedCommand(command, args);
CommandResult result = new CommandResult();
result.input = commandString;
result.parsedCommand = parsedCommand;
if (parsedCommand != null && parsedCommand.executor != null) {
// Syntax found
result.type = CommandResult.Type.SUCCESS;
} else {
// Syntax not found, use the default executor (if any)
result.type = CommandResult.Type.INVALID_SYNTAX;
if (parsedCommand == null) { // Prevent overriding argument callback
result.parsedCommand = ParsedCommand.withDefaultExecutor(command);
}
}
// Find the used syntax and fill CommandResult#type and CommandResult#parsedCommand
findParsedCommand(command, args, result);
return result;
}
@Nullable
private ParsedCommand findParsedCommand(@NotNull Command command, @NotNull String[] args) {
private ParsedCommand findParsedCommand(@NotNull Command command, @NotNull String[] args, @NotNull CommandResult result) {
final boolean hasArgument = args.length > 0;
// Search for subcommand
@ -143,7 +131,7 @@ public class CommandDispatcher {
final String firstArgument = args[0];
for (Command subcommand : command.getSubcommands()) {
if (Command.isValidName(subcommand, firstArgument)) {
return findParsedCommand(subcommand, Arrays.copyOfRange(args, 1, args.length));
return findParsedCommand(subcommand, Arrays.copyOfRange(args, 1, args.length), result);
}
}
}
@ -158,6 +146,9 @@ public class CommandDispatcher {
if (defaultExecutor != null && !hasArgument) {
parsedCommand.executor = defaultExecutor;
parsedCommand.arguments = new Arguments();
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
@ -189,6 +180,9 @@ public class CommandDispatcher {
parsedCommand.syntax = syntax;
parsedCommand.executor = syntax.getExecutor();
parsedCommand.arguments = executorArgs;
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
@ -210,12 +204,16 @@ public class CommandDispatcher {
parsedCommand.callback = argument.getCallback();
parsedCommand.argumentSyntaxException = argumentSyntaxException;
result.type = CommandResult.Type.INVALID_SYNTAX;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
}
// No syntax found
return null;
result.type = CommandResult.Type.INVALID_SYNTAX;
result.parsedCommand = ParsedCommand.withDefaultExecutor(command);
return result.parsedCommand;
}
}

View File

@ -31,6 +31,10 @@ public class CommandParser {
final Argument<?> argument = commandArguments[argCount];
useRemaining = argument.useRemaining();
final boolean end = splitIndex == inputArguments.length;
if (end) // True if there is no input to analyze left
break;
// the parsed argument value, null if incorrect
Object parsedValue;
// the argument exception, null if the input is correct