Skip to content

Commit 3317627

Browse files
committed
Find (sub)command when multi args are provided
* Find command when its name use spacing as separator Purpose is to support approach as namespace/object/action
1 parent 474dc3d commit 3317627

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Application.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,25 @@ public function group(string $group, callable $fn): self
259259
public function commandFor(array $argv): Command
260260
{
261261
$argv += [null, null, null];
262-
262+
$command = null;
263+
264+
//sort by key length
265+
//find maximal command compatibility by arguments
266+
$keys = array_map('strlen', array_keys($this->commands));
267+
array_multisort($keys, SORT_ASC, $this->commands);
268+
269+
foreach ($this->commands as $command_name => $value) {
270+
$nb_args = count(explode(" ", $command_name));
271+
$args = implode(" ", array_slice($argv, 1, $nb_args));
272+
if (! empty($this->commands[$args])) {
273+
$command = $this->commands[$args];
274+
}
275+
}
263276
return
277+
//cmd found by multi arguments
278+
$command
264279
// cmd
265-
$this->commands[$argv[1]]
280+
?? $this->commands[$argv[1]]
266281
// cmd alias
267282
?? $this->commands[$this->aliases[$argv[1]] ?? null]
268283
// default.

0 commit comments

Comments
 (0)