From 5288c87f17db8aa4e89a65c36075ff660930b2e2 Mon Sep 17 00:00:00 2001 From: Camille Lafitte Date: Mon, 29 Sep 2025 15:00:39 +0200 Subject: [PATCH 1/2] 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 --- src/Application.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Application.php b/src/Application.php index 34ae4f6..f12e595 100644 --- a/src/Application.php +++ b/src/Application.php @@ -259,10 +259,26 @@ public function group(string $group, callable $fn): self public function commandFor(array $argv): Command { $argv += [null, null, null]; + $command = null; + + //sort by key length + //find maximal command compatibility by arguments + $keys = array_map('strlen', array_keys($this->commands)); + array_multisort($keys, SORT_ASC, $this->commands); + + foreach ($this->commands as $command_name => $value) { + $nb_args = count(explode(' ', $command_name)); + $args = implode(' ', array_slice($argv, 1, $nb_args)); + if (!empty($this->commands[$args])) { + $command = $this->commands[$args]; + } + } return + //cmd found by multi arguments + $command // cmd - $this->commands[$argv[1]] + ?? $this->commands[$argv[1]] // cmd alias ?? $this->commands[$this->aliases[$argv[1]] ?? null] // default. From 08a1d19b3f4292d1ad37f60d8427d181572c7e57 Mon Sep 17 00:00:00 2001 From: Camille Lafitte Date: Tue, 30 Sep 2025 10:52:26 +0200 Subject: [PATCH 2/2] Retrieve related subCommand * When a command is added , add by cascad all subcommand managed by main command * Could be useful to load only 'Oject' command and retrieve all its related 'Verb' command --- src/Application.php | 5 +++++ src/Input/Command.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Application.php b/src/Application.php index f12e595..b46f3f6 100644 --- a/src/Application.php +++ b/src/Application.php @@ -200,6 +200,11 @@ public function add(Command $command, string $alias = '', bool $default = false) $this->commands[$name] = $command->version($this->version)->onExit($this->onExit)->bind($this); + if (method_exists($command, "getSubCommands")) { + foreach ($command->getSubCommands() as $subcommand) { + $this->add($subcommand); + } + } return $this; } diff --git a/src/Input/Command.php b/src/Input/Command.php index bcefe31..619a015 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -450,4 +450,19 @@ protected function progress(?int $total = null): ProgressBar { return new ProgressBar($total, $this->writer()); } + + + /** + * Get all related subCommands + * + * by default is an empty array + * could be populated by any way + * Return is an array[Command] + * + * @return array Command + */ + public function getSubCommands(): Array + { + return []; + } }