44import clientapi .command .Command ;
55import clientapi .util .ClientAPIUtils ;
66
7- import java .util .ArrayList ;
8- import java .util .List ;
9- import java .util .Optional ;
7+ import java .util .*;
108import java .util .regex .Matcher ;
119import 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