Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -259,10 +264,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.
Expand Down
15 changes: 15 additions & 0 deletions src/Input/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}