Skip to content

Commit

Permalink
up: remove some depercated methods, modify command flag option
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 7, 2022
1 parent 4729979 commit 71c0b4a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
15 changes: 2 additions & 13 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ abstract class Command extends AbstractHandler implements CommandInterface
*/
protected ?Controller $group = null;

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

parent::init();
}

/**
* @return array
*/
Expand All @@ -60,12 +53,7 @@ protected 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);
// }
// $fs->setStopOnFistArg(false);
}

/**
Expand All @@ -77,6 +65,7 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
{
$this->debugf('load flags configure for command: %s', $this->getRealCName());
$this->configure();
$this->configFlags($fs);

$isEmpty = $this->flags->isEmpty();

Expand Down
34 changes: 2 additions & 32 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use ReflectionException;
use ReflectionMethod;
use ReflectionObject;
use RuntimeException;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\FlagUtil;
Expand Down Expand Up @@ -278,7 +277,6 @@ public function doRun(array $args): mixed

$command = $first;
array_shift($args);
// $this->input->popFirstArg();
}

// update subcommand
Expand Down Expand Up @@ -531,7 +529,7 @@ protected function beforeRenderCommandHelp(array &$help): void
* @param ReflectionClass|null $ref
* @param bool $onlyName
*
* @return Generator
* @return ?Generator
*/
protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyName = false): ?Generator
{
Expand All @@ -556,17 +554,6 @@ protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyN
}
}

/**
* @param string $name
*
* @return string
* @deprecated please use resolveAlias()
*/
public function getRealCommandName(string $name): string
{
return $this->resolveAlias($name);
}

/**
* @param string $alias
*
Expand Down Expand Up @@ -631,7 +618,7 @@ public function getDisabledCommands(): array
}

/**
* @param string|null $name
* @param string $name
*
* @return array
*/
Expand Down Expand Up @@ -752,23 +739,6 @@ public function setActionSuffix(string $actionSuffix): void
$this->actionSuffix = $actionSuffix;
}

/**
* @return bool
* @deprecated
*/
public function isExecutionAlone(): bool
{
throw new RuntimeException('please call isAttached() instead');
}

/**
* @deprecated
*/
public function setExecutionAlone(): void
{
throw new RuntimeException('please call setAttached() instead');
}

/**
* @return string
*/
Expand Down
30 changes: 28 additions & 2 deletions src/Decorate/SubCommandsWareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@

namespace Inhere\Console\Decorate;

use Closure;
use Inhere\Console\Command;
use Inhere\Console\Console;
use Inhere\Console\Contract\CommandInterface;
use Inhere\Console\Handler\AbstractHandler;
use Inhere\Console\Handler\CommandWrapper;
use Inhere\Console\Util\ConsoleUtil;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use Toolkit\Stdlib\Helper\Assert;
Expand Down Expand Up @@ -49,6 +47,13 @@ trait SubCommandsWareTrait
*/
private array $blocked = ['help', 'version'];

/**
* Command full path. eg: 'git remote set-url'
*
* @var string
*/
protected string $path = '';

/**
* The sub-commands of the command
*
Expand Down Expand Up @@ -268,6 +273,26 @@ public function isSub(string $name): bool
return isset($this->commands[$name]);
}

/**
* @param string $path
*/
public function setPath(string $path): void
{
$this->path = $path;
}

/**
* @param string $name
*/
public function addPath(string $name): void
{
if ($this->path) {
$this->path .= ' ' . $name;
} else {
$this->path = $name;
}
}

/**
* @param string $name
*
Expand Down Expand Up @@ -341,6 +366,7 @@ public function getSubsForHelp(): array
if ($sub instanceof Command) {
$subs[$name] = $sub->getRealDesc();
} elseif (is_string($sub)) {
/** @var Command $sub */
$subs[$name] = $sub::getDesc();
} else {
$subConf = $subInfo['config'];
Expand Down
25 changes: 16 additions & 9 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ abstract class AbstractHandler implements CommandHandlerInterface
*/
private bool $initialized = false;

/**
* Compatible mode run command.
*
* @var bool
*/
protected bool $compatible = true;

/**
* @var string
*/
Expand All @@ -96,7 +89,7 @@ abstract class AbstractHandler implements CommandHandlerInterface
protected ?DataObject $params = null;

/**
* The input command name. maybe is an alias name.
* The user input command name. maybe is an alias name.
*
* @var string
*/
Expand Down Expand Up @@ -184,6 +177,15 @@ protected function configure(): void
{
}

/**
* Config flags for the command/controller.
*
* @param FlagsParser $fs
*/
protected function configFlags(FlagsParser $fs): void
{
}

/**
* Provides parsable substitution variables for command annotations. Can be used in comments in commands
* 为命令注解提供可解析的替换变量. 可以在命令的注释中使用
Expand Down Expand Up @@ -232,6 +234,11 @@ protected function initForRun(): void
{
$this->commentsVars = $this->annotationVars();

if (!$this->commandName) {
$this->commandName = $this->getRealName();
}

$this->addPath($this->commandName);
}

/**
Expand Down Expand Up @@ -296,7 +303,7 @@ public function run(array $args): mixed

$this->initFlagsParser($this->input);

$this->log(Console::VERB_DEBUG, "begin run '$name' - parse options", ['args' => $args]);
$this->log(Console::VERB_DEBUG, "cmd: $name - parse flag options", ['args' => $args]);

// parse options
$this->flags->lock();
Expand Down

0 comments on commit 71c0b4a

Please sign in to comment.