Skip to content

Commit 71c0b4a

Browse files
committed
up: remove some depercated methods, modify command flag option
1 parent 4729979 commit 71c0b4a

File tree

4 files changed

+48
-56
lines changed

4 files changed

+48
-56
lines changed

src/Command.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ abstract class Command extends AbstractHandler implements CommandInterface
3939
*/
4040
protected ?Controller $group = null;
4141

42-
protected function init(): void
43-
{
44-
$this->commandName = $this->getRealName();
45-
46-
parent::init();
47-
}
48-
4942
/**
5043
* @return array
5144
*/
@@ -60,12 +53,7 @@ protected function getArguments(): array
6053
protected function beforeInitFlagsParser(FlagsParser $fs): void
6154
{
6255
$fs->addArgsByRules($this->getArguments());
63-
$fs->setStopOnFistArg(false);
64-
65-
// old mode: options and arguments at method annotations
66-
// if ($this->compatible) {
67-
// $fs->setSkipOnUndefined(true);
68-
// }
56+
// $fs->setStopOnFistArg(false);
6957
}
7058

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

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

src/Controller.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use ReflectionException;
2222
use ReflectionMethod;
2323
use ReflectionObject;
24-
use RuntimeException;
2524
use Throwable;
2625
use Toolkit\PFlag\FlagsParser;
2726
use Toolkit\PFlag\FlagUtil;
@@ -278,7 +277,6 @@ public function doRun(array $args): mixed
278277

279278
$command = $first;
280279
array_shift($args);
281-
// $this->input->popFirstArg();
282280
}
283281

284282
// update subcommand
@@ -531,7 +529,7 @@ protected function beforeRenderCommandHelp(array &$help): void
531529
* @param ReflectionClass|null $ref
532530
* @param bool $onlyName
533531
*
534-
* @return Generator
532+
* @return ?Generator
535533
*/
536534
protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyName = false): ?Generator
537535
{
@@ -556,17 +554,6 @@ protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyN
556554
}
557555
}
558556

559-
/**
560-
* @param string $name
561-
*
562-
* @return string
563-
* @deprecated please use resolveAlias()
564-
*/
565-
public function getRealCommandName(string $name): string
566-
{
567-
return $this->resolveAlias($name);
568-
}
569-
570557
/**
571558
* @param string $alias
572559
*
@@ -631,7 +618,7 @@ public function getDisabledCommands(): array
631618
}
632619

633620
/**
634-
* @param string|null $name
621+
* @param string $name
635622
*
636623
* @return array
637624
*/
@@ -752,23 +739,6 @@ public function setActionSuffix(string $actionSuffix): void
752739
$this->actionSuffix = $actionSuffix;
753740
}
754741

755-
/**
756-
* @return bool
757-
* @deprecated
758-
*/
759-
public function isExecutionAlone(): bool
760-
{
761-
throw new RuntimeException('please call isAttached() instead');
762-
}
763-
764-
/**
765-
* @deprecated
766-
*/
767-
public function setExecutionAlone(): void
768-
{
769-
throw new RuntimeException('please call setAttached() instead');
770-
}
771-
772742
/**
773743
* @return string
774744
*/

src/Decorate/SubCommandsWareTrait.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
namespace Inhere\Console\Decorate;
1111

12-
use Closure;
1312
use Inhere\Console\Command;
1413
use Inhere\Console\Console;
1514
use Inhere\Console\Contract\CommandInterface;
1615
use Inhere\Console\Handler\AbstractHandler;
1716
use Inhere\Console\Handler\CommandWrapper;
18-
use Inhere\Console\Util\ConsoleUtil;
1917
use Inhere\Console\Util\Helper;
2018
use InvalidArgumentException;
2119
use Toolkit\Stdlib\Helper\Assert;
@@ -49,6 +47,13 @@ trait SubCommandsWareTrait
4947
*/
5048
private array $blocked = ['help', 'version'];
5149

50+
/**
51+
* Command full path. eg: 'git remote set-url'
52+
*
53+
* @var string
54+
*/
55+
protected string $path = '';
56+
5257
/**
5358
* The sub-commands of the command
5459
*
@@ -268,6 +273,26 @@ public function isSub(string $name): bool
268273
return isset($this->commands[$name]);
269274
}
270275

276+
/**
277+
* @param string $path
278+
*/
279+
public function setPath(string $path): void
280+
{
281+
$this->path = $path;
282+
}
283+
284+
/**
285+
* @param string $name
286+
*/
287+
public function addPath(string $name): void
288+
{
289+
if ($this->path) {
290+
$this->path .= ' ' . $name;
291+
} else {
292+
$this->path = $name;
293+
}
294+
}
295+
271296
/**
272297
* @param string $name
273298
*
@@ -341,6 +366,7 @@ public function getSubsForHelp(): array
341366
if ($sub instanceof Command) {
342367
$subs[$name] = $sub->getRealDesc();
343368
} elseif (is_string($sub)) {
369+
/** @var Command $sub */
344370
$subs[$name] = $sub::getDesc();
345371
} else {
346372
$subConf = $subInfo['config'];

src/Handler/AbstractHandler.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ abstract class AbstractHandler implements CommandHandlerInterface
7878
*/
7979
private bool $initialized = false;
8080

81-
/**
82-
* Compatible mode run command.
83-
*
84-
* @var bool
85-
*/
86-
protected bool $compatible = true;
87-
8881
/**
8982
* @var string
9083
*/
@@ -96,7 +89,7 @@ abstract class AbstractHandler implements CommandHandlerInterface
9689
protected ?DataObject $params = null;
9790

9891
/**
99-
* The input command name. maybe is an alias name.
92+
* The user input command name. maybe is an alias name.
10093
*
10194
* @var string
10295
*/
@@ -184,6 +177,15 @@ protected function configure(): void
184177
{
185178
}
186179

180+
/**
181+
* Config flags for the command/controller.
182+
*
183+
* @param FlagsParser $fs
184+
*/
185+
protected function configFlags(FlagsParser $fs): void
186+
{
187+
}
188+
187189
/**
188190
* Provides parsable substitution variables for command annotations. Can be used in comments in commands
189191
* 为命令注解提供可解析的替换变量. 可以在命令的注释中使用
@@ -232,6 +234,11 @@ protected function initForRun(): void
232234
{
233235
$this->commentsVars = $this->annotationVars();
234236

237+
if (!$this->commandName) {
238+
$this->commandName = $this->getRealName();
239+
}
240+
241+
$this->addPath($this->commandName);
235242
}
236243

237244
/**
@@ -296,7 +303,7 @@ public function run(array $args): mixed
296303

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

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

301308
// parse options
302309
$this->flags->lock();

0 commit comments

Comments
 (0)