Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 232cefe

Browse files
committed
Add method to find possible command children to CommandInputParser
1 parent f7f1310 commit 232cefe

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/java/clientapi/command/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private void setup(String[] headers, String description) {
9393

9494
@Override
9595
public final void execute(ExecutionContext context, String[] arguments) throws CommandException {
96-
Optional<ChildCommand> sub = CommandInputParser.INSTANCE.findChild(this, arguments);
96+
Optional<ChildCommand> sub = CommandInputParser.INSTANCE.findChild(this, arguments, true);
9797
if (!sub.isPresent())
9898
throw new UnknownSubCommandException(this, arguments);
9999

src/main/java/clientapi/command/executor/parser/CommandInputParser.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import clientapi.command.Command;
55
import clientapi.util.ClientAPIUtils;
66

7-
import java.util.ArrayList;
8-
import java.util.List;
9-
import java.util.Optional;
7+
import java.util.*;
108
import java.util.regex.Matcher;
119
import java.util.regex.Pattern;
10+
import java.util.stream.Collectors;
1211

1312
/**
1413
* Used to parse raw text into the individual arguments that will be used for execution
@@ -53,9 +52,33 @@ public final Optional<ParsedCommandInput> parseCommandInput(String raw) {
5352
*
5453
* @param command The command
5554
* @param arguments The arguments
55+
* @param strictArgs Whether or not non-header children have argument length checked
5656
* @return The child command if one that matches the arguments is found
5757
*/
58-
public final Optional<ChildCommand> findChild(Command command, String[] arguments) {
58+
public final Optional<ChildCommand> findChild(Command command, String[] arguments, boolean strictArgs) {
59+
List<ChildCommand> children = findPossibleChildren(command, arguments, strictArgs);
60+
switch (children.size()) {
61+
case 0: // There were no matches
62+
return Optional.empty();
63+
case 1: // There was a match by child command header
64+
return Optional.of(children.get(0));
65+
default: // There was a match based on argument length if strictArgs is true
66+
return strictArgs
67+
? Optional.of(children.get(0))
68+
// If argument count wasn't checked, find the closest argument length
69+
: children.stream().min(Comparator.comparingInt(c -> Math.abs(c.getHeaders().length - arguments.length)));
70+
}
71+
}
72+
73+
/**
74+
* Returns the possible target children given the input args
75+
*
76+
* @param command The command
77+
* @param arguments The arguments
78+
* @param strictArgs Whether or not non-header children have argument length checked
79+
* @return The possible children
80+
*/
81+
public final List<ChildCommand> findPossibleChildren(Command command, String[] arguments, boolean strictArgs) {
5982
String header = arguments.length == 0 ? null : arguments[0];
6083

6184
// Find the command by the header defined by the first argument
@@ -64,11 +87,11 @@ public final Optional<ChildCommand> findChild(Command command, String[] argument
6487
.findFirst().orElse(null);
6588

6689
if (child != null)
67-
return Optional.of(child);
90+
return Collections.singletonList(child);
6891

6992
// Find the command by the length of the arguments
7093
return command.getChildren().stream()
71-
.filter(c -> c.getHeaders().length == 0 && arguments.length == c.getArguments().size())
72-
.findFirst();
94+
.filter(c -> c.getHeaders().length == 0 && (!strictArgs || arguments.length == c.getArguments().size()))
95+
.collect(Collectors.toList());
7396
}
7497
}

0 commit comments

Comments
 (0)