2020import clientapi .command .exception .CommandInitException ;
2121import clientapi .command .exception .UnknownSubCommandException ;
2222import clientapi .command .executor .ExecutionContext ;
23+ import clientapi .command .executor .parser .CommandInputParser ;
2324import clientapi .util .ClientAPIUtils ;
2425
2526import java .lang .reflect .Method ;
@@ -75,12 +76,10 @@ private void setup(String[] headers, String description) {
7576 int optionals = 0 ;
7677 for (int i = 0 ; i < parameters ; i ++) {
7778 if (method .getParameterTypes ()[i ].isAssignableFrom (Optional .class )) {
78- if (i != parameters - 1 ) {
79+ if (i != parameters - 1 )
7980 throw new RuntimeException (new CommandInitException (this , "Optionals must be defined as the last parameter" ));
80- }
81- if (++optionals > 1 ) {
81+ if (++optionals > 1 )
8282 throw new RuntimeException (new CommandInitException (this , "More than one optional parameter is not supported" ));
83- }
8483 }
8584 }
8685
@@ -94,18 +93,18 @@ private void setup(String[] headers, String description) {
9493
9594 @ Override
9695 public final void execute (ExecutionContext context , String [] arguments ) throws CommandException {
97- ChildCommand sub = findChild (arguments );
98- if (sub == null )
96+ Optional < ChildCommand > sub = CommandInputParser . INSTANCE . findChild (this , arguments );
97+ if (! sub . isPresent () )
9998 throw new UnknownSubCommandException (this , arguments );
10099
101100 // If the child was found by it's header, then remove the first argument.
102- if (sub .getHeaders ().length > 0 && arguments .length > 0 ) {
101+ if (sub .get (). getHeaders ().length > 0 && arguments .length > 0 ) {
103102 String [] newArgs = new String [arguments .length - 1 ];
104103 System .arraycopy (arguments , 1 , newArgs , 0 , arguments .length - 1 );
105104 arguments = newArgs ;
106105 }
107106
108- sub .execute (context , arguments );
107+ sub .get (). execute (context , arguments );
109108 }
110109
111110 @ Override
@@ -124,28 +123,4 @@ public final String getDescription() {
124123 public final List <ChildCommand > getChildren () {
125124 return this .children ;
126125 }
127-
128- /**
129- * Finds a child command from the specified arguments
130- *
131- * @param arguments The arguments
132- * @return The child command, {@code null} if not found
133- */
134- private ChildCommand findChild (String [] arguments ) {
135- String header = arguments .length == 0 ? null : arguments [0 ];
136-
137- // Find the command by the header defined by the first argument
138- ChildCommand child = this .children .stream ().filter (c -> (header == null && c .getHeaders ().length == 0 ) || ClientAPIUtils .containsIgnoreCase (c .getHeaders (), header )).findFirst ().orElse (null );
139- if (child != null ) {
140- return child ;
141- }
142-
143- // Find the command by the length of the arguments
144- child = this .children .stream ().filter (c -> c .getHeaders ().length == 0 && arguments .length == c .getArguments ().size ()).findFirst ().orElse (null );
145- if (child != null ) {
146- return child ;
147- }
148-
149- return null ;
150- }
151126}
0 commit comments