Skip to content

Commit

Permalink
[Experimantal] Add extra suggestions if the last argument parsed succ…
Browse files Browse the repository at this point in the history
…essfully
  • Loading branch information
TBlueF committed Jan 29, 2024
1 parent dc1dde0 commit 0e258f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import de.bluecolored.bluecommands.parsers.ArgumentParser;

import java.util.Collection;
import java.util.List;

public class ArgumentCommand<C, T> extends Command<C, T> {

private final String argumentId;
Expand Down Expand Up @@ -74,6 +77,21 @@ void parse(ParseData<C, T> data) {
throw new CommandSetupException("The ArgumentParser '" + argumentParser + "' did not consume the full token. (expected next char to be a space or end of string)");
}

// if we reached the end of input, check if there is any additional suggestions and add an extra failure if there are any
if (next == -1) {
input.setPosition(position); // reset position for suggestions
List<Suggestion> extraSuggesions = argumentParser.suggest(context, input);
if (!extraSuggesions.isEmpty()) {
data.getResult().addFailure(new ParseFailure<>(
position,
"Alternative Usages",
data.getCommandStack(),
extraSuggesions
));
}
input.readRemaining(); // reset position
}

data.getCurrentSegment().setValue(argument);
super.parse(data);
} catch (CommandParseException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void parse(ParseData<C, T> data) {
data.popSegment();
}
}
} else {
} else if (!subCommands.isEmpty()) {
data.getResult().addFailure(new ParseFailure<>(inputPosition, "Not enough arguments!", data.getCommandStack()));
}
}
Expand Down

0 comments on commit 0e258f6

Please sign in to comment.