Skip to content

Commit 7d2417b

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 7d2417b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Application.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,26 @@ public function group(string $group, callable $fn): self
259259
public function commandFor(array $argv): Command
260260
{
261261
$argv += [null, null, null];
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+
}
262276

263277
return
278+
//cmd found by multi arguments
279+
$command
264280
// cmd
265-
$this->commands[$argv[1]]
281+
?? $this->commands[$argv[1]]
266282
// cmd alias
267283
?? $this->commands[$this->aliases[$argv[1]] ?? null]
268284
// default.

0 commit comments

Comments
 (0)