Skip to content

Commit

Permalink
feat: allow attch multi level sub-commands to command,group
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 26, 2021
1 parent cce9850 commit d1f9829
Show file tree
Hide file tree
Showing 18 changed files with 510 additions and 279 deletions.
2 changes: 1 addition & 1 deletion examples/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestCommand extends Command

protected static string $desc = 'this is a test independent command';

protected function commands(): array
protected function subCommands(): array
{
return [
'sub' => static function ($fs, $out): void {
Expand Down
40 changes: 40 additions & 0 deletions examples/Controller/Attach/DemoSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Inhere\Console\Examples\Controller\Attach;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use function vdump;

/**
* class DemoSubCommand
*
* @author inhere
*/
class DemoSubCommand extends Command
{
protected static string $name = 'sub1';
protected static string $desc = 'alone sub command on an group';

public function getOptions(): array
{
return [
'str1' => 'string option1',
's2,str2' => 'string option2',
];
}

/**
* Do execute command
*
* @param Input $input
* @param Output $output
*
* @return void|mixed
*/
protected function execute(Input $input, Output $output)
{
vdump(__METHOD__);
}
}
25 changes: 24 additions & 1 deletion examples/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Inhere\Console\Component\Symbol\ArtFont;
use Inhere\Console\Controller;
use Inhere\Console\Examples\Controller\Attach\DemoSubCommand;
use Inhere\Console\Handler\CommandWrapper;
use Inhere\Console\Util\Interact;
use Inhere\Console\Util\ProgressBar;
use Inhere\Console\Util\Show;
Expand All @@ -23,6 +25,7 @@
use Toolkit\Stdlib\Php;
use function sleep;
use function trigger_error;
use function vdump;

/**
* default command controller. there are some command usage examples(1)
Expand Down Expand Up @@ -74,13 +77,30 @@ protected function init(): void
/**
* @return array
*/
protected function options(): array
protected function getOptions(): array
{
return [
'-c, --common' => 'This is a common option for all sub-commands',
];
}

protected function subCommands(): array
{
return [
DemoSubCommand::class,
CommandWrapper::wrap(static function ($fs) {
vdump(__METHOD__, $fs->getOpts());
}, [
'name' => 'sub2',
'desc' => 'an sub command in group controller',
'options' => [
'int1' => 'int;an int option1',
'i2, int2' => 'int;an int option2',
]
]),
];
}

protected function disabledCommands(): array
{
return ['disabled'];
Expand Down Expand Up @@ -207,6 +227,9 @@ public function colorCheckCommand(): void
* --font Set the art font name(allow: {internalFonts}).
* --italic bool;Set the art font type is italic.
* --style Set the art font style.
*
* @param FlagsParser $fs
*
* @return int
*/
public function artFontCommand(FlagsParser $fs): int
Expand Down
2 changes: 1 addition & 1 deletion examples/Controller/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function commandAliases(): array
/**
* @return array
*/
protected function options(): array
protected function getOptions(): array
{
return [
'-c, --common' => 'This is a common option for all sub-commands',
Expand Down
52 changes: 21 additions & 31 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Inhere\Console\Handler\AbstractHandler;
use ReflectionException;
use Toolkit\PFlag\FlagsParser;
use function array_shift;

/**
* Class Command
Expand All @@ -38,22 +39,17 @@ abstract class Command extends AbstractHandler implements CommandInterface
*/
protected ?Controller $group = null;

/**
* @var Command|null
*/
protected ?Command $parent = null;

protected function init(): void
{
$this->commandName = self::getName();
$this->commandName = $this->getRealName();

parent::init();
}

/**
* @return array
*/
public function getArguments(): array
protected function getArguments(): array
{
return [];
}
Expand All @@ -63,12 +59,13 @@ public function getArguments(): array
*/
protected function beforeInitFlagsParser(FlagsParser $fs): void
{
$fs->addArgsByRules($this->getArguments());
$fs->setStopOnFistArg(false);

// old mode: options and arguments at method annotations
if ($this->compatible) {
$fs->setSkipOnUndefined(true);
}
// if ($this->compatible) {
// $fs->setSkipOnUndefined(true);
// }
}

/**
Expand All @@ -93,31 +90,24 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
}

/**
* @param Command $parent
*/
public function setParent(Command $parent): void
{
$this->parent = $parent;
}

/**
* @return $this
* @param array $args
*
* @return mixed
*/
public function getRoot(): Command
protected function doRun(array $args): mixed
{
if ($this->parent) {
return $this->parent->getRoot();
// if input sub-command name
if (isset($args[0])) {
$first = $args[0];
$rName = $this->resolveAlias($first);

if ($this->isSub($rName)) {
array_shift($args);
return $this->dispatchSub($rName, $args);
}
}

return $this;
}

/**
* @return Command|null
*/
public function getParent(): ?Command
{
return $this->parent;
return parent::doRun($args);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Component/PharCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Exception;
use FilesystemIterator;
use Generator;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use Iterator;
use Phar;
Expand Down
13 changes: 12 additions & 1 deletion src/Component/ReadlineCompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public function isSupported(): bool

/**
* @param callable $completer
*
* @return ReadlineCompleter
*/
public function setCompleter(callable $completer): void
public function setCompleter(callable $completer): static
{
$this->completer = $completer;
return $this;
}

/**
Expand Down Expand Up @@ -113,4 +116,12 @@ public function setHistorySize(int $historySize): void
{
$this->historySize = $historySize;
}

/**
* @return callable
*/
public function getCompleter(): callable
{
return $this->completer;
}
}
17 changes: 2 additions & 15 deletions src/Component/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Inhere\Console\Contract\RouterInterface;
use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\ConsoleUtil;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use Toolkit\PFlag\FlagsParser;
Expand Down Expand Up @@ -206,7 +207,7 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
if ($aliases = $handler::aliases()) {
$config['aliases'] = array_merge($config['aliases'], $aliases);
}
} elseif (!is_object($handler) || !$this->isValidCmdObject($handler)) {
} elseif (!is_object($handler) || !ConsoleUtil::isValidCmdObject($handler)) {
Helper::throwInvalidArgument(
'The command handler must is an subclass of %s OR a Closure OR a sub-object of %s',
Command::class,
Expand All @@ -230,20 +231,6 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
return $this;
}

/**
* @param object $handler
*
* @return bool
*/
private function isValidCmdObject(object $handler): bool
{
if ($handler instanceof Command) {
return true;
}

return method_exists($handler, '__invoke');
}

/**
* @param array $commands
*
Expand Down
5 changes: 5 additions & 0 deletions src/Contract/CommandHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function getGroupName(): string;
*/
public function getRealName(): string;

/**
* @return string
*/
public function getRealDesc(): string;

/**
* The real group name.
*
Expand Down
15 changes: 10 additions & 5 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,26 @@ public function doRun(array $args): mixed
// convert 'boo-foo' to 'booFoo'
$this->action = $action = Str::camelCase($command);
$this->debugf("will run the '%s' group action: %s, subcommand: %s", $name, $action, $command);
$this->actionMethod = $method = $this->getMethodName($action);
$method = $this->getMethodName($action);

// fire event
$this->fire(ConsoleEvent::COMMAND_RUN_BEFORE, $this);
$this->beforeRun();

// check method not exist
// - if command method not exists.
if (!method_exists($this, $method)) {
if ($this->isSub($command)) {
return $this->dispatchSub($command, $args);
}

// if command not exists.
return $this->handleNotFound($name, $action, $args);
}

// init flags for subcommand
$fs = $this->newActionFlags();

$this->actionMethod = $method;
$this->input->setFs($fs);
$this->debugf('load flags by configure method, subcommand: %s', $command);
$this->configure();
Expand Down Expand Up @@ -485,9 +490,9 @@ protected function newActionFlags(string $action = ''): FlagsParser
});

// old mode: options and arguments at method annotations
if ($this->compatible) {
$fs->setSkipOnUndefined(true);
}
// if ($this->compatible) {
// $fs->setSkipOnUndefined(true);
// }

// save
$this->subFss[$action] = $fs;
Expand Down
Loading

0 comments on commit d1f9829

Please sign in to comment.