private boolean relatedCommand(String input) {
// make sure the split afterwards has at least size one
if (input.matches(" .*")) {
return false;
}
final String command = input.split(" ".toString())[0];
return COMMAND_PACKAGE
.keySet()
.stream()
.map(Pattern::toString)
.anyMatch(patternText -> patternText.startsWith(command + " "));
}
public Command getCommand(final String input) throws InvalidInputException {
if (relatedCommand(input)) {
Terminal.printError("test");
throw new InvalidInputException("invalid arguments");
} else {
throw new InvalidInputException("unknown command");
}
...
}
I am having trouble with giving the user a more specific error message. For example I have the commands add track <argument1>
and add switch <argument1>
. If the user just types in "add" he shouldn't get the error message "invalid arguments". Instead it should be "invalid command: either use add track or add switch). Since relatedCommand() is a boolean. How do I implement this efficiently?