Skip to content

Commit d94eeee

Browse files
committed
breaking: update command register method sign
1 parent 045e0dd commit d94eeee

File tree

5 files changed

+28
-39
lines changed

5 files changed

+28
-39
lines changed

src/Concern/ApplicationHelpTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
use Inhere\Console\Contract\CommandInterface;
1616
use Inhere\Console\IO\Input;
1717
use Inhere\Console\IO\Output;
18-
use Inhere\Console\Util\FormatUtil;
1918
use Inhere\Console\Util\Show;
2019
use Toolkit\Cli\Color\ColorTag;
2120
use Toolkit\Cli\Style;
21+
use Toolkit\PFlag\FlagUtil;
2222
use function array_merge;
2323
use function basename;
2424
use function date;
@@ -138,7 +138,7 @@ public function showHelpInfo(string $command = ''): void
138138
$binName = $in->getScriptName();
139139
$helpInfo = [
140140
'Usage' => "$binName <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
141-
'Options' => FormatUtil::alignOptions($globalOptions),
141+
'Options' => FlagUtil::alignOptions($globalOptions),
142142
'Example' => [
143143
'- run a command/subcommand:',
144144
"$binName test run a independent command",
@@ -215,11 +215,11 @@ public function showCommandList(): void
215215

216216
$placeholder = 'No description of the command';
217217
foreach ($groups as $name => $info) {
218-
$options = $info['options'];
219218
$controller = $info['handler'];
220219
/** @var AbstractHandler $controller */
221220
$desc = $controller::getDesc() ?: $placeholder;
222-
$aliases = $options['aliases'];
221+
$config = $info['config'];
222+
$aliases = $config['aliases'];
223223
$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
224224

225225
// collect
@@ -231,22 +231,22 @@ public function showCommandList(): void
231231
}
232232

233233
foreach ($commands as $name => $info) {
234-
$desc = $placeholder;
235-
$options = $info['options'];
234+
$desc = $placeholder;
235+
$config = $info['config'];
236236
$command = $info['handler'];
237237

238238
/** @var AbstractHandler $command */
239239
if (is_subclass_of($command, CommandInterface::class)) {
240240
$desc = $command::getDesc() ?: $placeholder;
241-
} elseif ($msg = $options['desc'] ?? '') {
241+
} elseif ($msg = $config['desc'] ?? '') {
242242
$desc = $msg;
243243
} elseif (is_string($command)) {
244244
$desc = 'A handler : ' . $command;
245245
} elseif (is_object($command)) {
246246
$desc = 'A handler by ' . get_class($command);
247247
}
248248

249-
$aliases = $options['aliases'];
249+
$aliases = $config['aliases'];
250250
$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
251251

252252
$commandArr[$name] = $desc . $extra;
@@ -281,7 +281,7 @@ public function showCommandList(): void
281281

282282
Show::mList([
283283
'Usage:' => "$scriptName <info>{COMMAND}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
284-
'Options:' => FormatUtil::alignOptions($globOpts),
284+
'Options:' => FlagUtil::alignOptions($globOpts),
285285
'Internal Commands:' => $internalCommands,
286286
'Available Commands:' => array_merge($groupArr, $commandArr),
287287
], [

src/Concern/CommandHelpTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
use Inhere\Console\Handler\AbstractHandler;
1313
use Inhere\Console\Console;
14-
use Inhere\Console\Util\FormatUtil;
1514
use Toolkit\PFlag\FlagsParser;
15+
use Toolkit\PFlag\FlagUtil;
1616
use function implode;
1717
use function sprintf;
1818
use function strtr;
@@ -146,7 +146,7 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
146146

147147
$help['Usage:'] = "$path [--options ...] [arguments ...]";
148148

149-
$help['Options:'] = FormatUtil::alignOptions($fs->getOptsHelpLines());
149+
$help['Options:'] = FlagUtil::alignOptions($fs->getOptsHelpLines());
150150
$help['Argument:'] = $fs->getArgsHelpLines();
151151
$help['Example:'] = $fs->getExampleHelp();
152152

@@ -158,7 +158,7 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
158158

159159
// attached to console app
160160
if ($this->renderGlobalOption && ($app = $this->getApp())) {
161-
$help['Global Options:'] = FormatUtil::alignOptions($app->getFlags()->getOptsHelpLines());
161+
$help['Global Options:'] = FlagUtil::alignOptions($app->getFlags()->getOptsHelpLines());
162162
}
163163

164164
$this->output->mList($help, [

src/Concern/SubCommandsWareTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ trait SubCommandsWareTrait
4848
* [
4949
* 'name' => [
5050
* 'handler' => MyCommand::class, // allow: string|Closure|CommandInterface
51-
* 'options' => []
51+
* 'config' => []
5252
* ]
5353
* ]
5454
*/
@@ -83,14 +83,14 @@ protected function dispatchCommand(string $name): void
8383
*
8484
* @param string|CommandInterface $name
8585
* @param string|Closure|CommandInterface|null $handler
86-
* @param array $options
86+
* @param array $config
8787
* array:
8888
* - aliases The command aliases
8989
* - description The description message
9090
*
9191
* @throws InvalidArgumentException
9292
*/
93-
public function addSub(string $name, string|Closure|CommandInterface $handler = null, array $options = []): void
93+
public function addSub(string $name, string|Closure|CommandInterface $handler = null, array $config = []): void
9494
{
9595
if (!$handler && class_exists($name)) {
9696
/** @var Command $name name is an command class */
@@ -108,7 +108,7 @@ public function addSub(string $name, string|Closure|CommandInterface $handler =
108108
Helper::throwInvalidArgument("Command '$name' have been registered!");
109109
}
110110

111-
$options['aliases'] = isset($options['aliases']) ? (array)$options['aliases'] : [];
111+
$config['aliases'] = isset($config['aliases']) ? (array)$config['aliases'] : [];
112112

113113
if (is_string($handler)) {
114114
if (!class_exists($handler)) {
@@ -127,7 +127,7 @@ public function addSub(string $name, string|Closure|CommandInterface $handler =
127127

128128
// allow define aliases in Command class by Command::aliases()
129129
if ($aliases = $handler::aliases()) {
130-
$options['aliases'] = array_merge($options['aliases'], $aliases);
130+
$config['aliases'] = array_merge($config['aliases'], $aliases);
131131
}
132132
} elseif (!is_object($handler) || !method_exists($handler, '__invoke')) {
133133
Helper::throwInvalidArgument(
@@ -140,12 +140,12 @@ public function addSub(string $name, string|Closure|CommandInterface $handler =
140140
$this->commands[$name] = [
141141
'type' => Console::CMD_SINGLE,
142142
'handler' => $handler,
143-
'options' => $options,
143+
'config' => $config,
144144
];
145145

146146
// has alias option
147-
if (isset($options['aliases'])) {
148-
$this->setAlias($name, $options['aliases'], true);
147+
if (isset($config['aliases'])) {
148+
$this->setAlias($name, $config['aliases'], true);
149149
}
150150
}
151151

src/Contract/ApplicationInterface.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public function stop(int $code = 0): void;
5959
* Register a app group command(by controller)
6060
*
6161
* @param string $name The controller name
62-
* @param string|ControllerInterface|null $class The controller class
62+
* @param class-string|ControllerInterface|null $class The controller class
6363
* @param array{desc: string, aliases: array} $config config the controller.
64-
* - aliases The command aliases
65-
* - desc The description message
6664
*
6765
* @return static
6866
* @throws InvalidArgumentException
@@ -72,11 +70,9 @@ public function controller(string $name, ControllerInterface|string $class = nul
7270
/**
7371
* Register a app independent console command
7472
*
75-
* @param string|class-string $name
76-
* @param string|Closure|CommandInterface|null $handler
73+
* @param string $name
74+
* @param string|CommandInterface|null|Closure():void $handler
7775
* @param array{desc: string, aliases: array} $config config the command.
78-
* - aliases The command aliases
79-
* - desc The description message
8076
*
8177
* @return mixed
8278
* @throws InvalidArgumentException

src/Contract/RouterInterface.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Inhere\Console\Contract;
1111

1212
use Closure;
13-
use InvalidArgumentException;
1413

1514
/**
1615
* Interface RouterInterface
@@ -33,25 +32,19 @@ interface RouterInterface
3332
* @param string|class-string $name The controller name
3433
* @param string|ControllerInterface|null $class The controller class
3534
* @param array{aliases: array, desc: string} $config The options config.
36-
* - aliases The command aliases
37-
* - desc The description message
3835
*
3936
* @return static
40-
* @throws InvalidArgumentException
4137
*/
4238
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static;
4339

4440
/**
4541
* Register a app independent console command
4642
*
4743
* @param string|class-string $name
48-
* @param string|Closure|CommandInterface|null $handler
49-
* @param array{aliases: array, desc: string} $config The options config.
50-
* - aliases The command aliases
51-
* - desc The description message
44+
* @param string|CommandInterface|null|Closure():void $handler
45+
* @param array{aliases: array, desc: string, options: array, arguments: array} $config The config.
5246
*
5347
* @return static
54-
* @throws InvalidArgumentException
5548
*/
5649
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static;
5750

@@ -60,16 +53,16 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
6053
* return [
6154
* type => 1, // 1 group 2 command
6255
* handler => handler class/object/func ...
63-
* options => [
56+
* config => [
57+
* desc => '',
6458
* aliases => [],
65-
* description => '',
6659
* ],
6760
* ]
6861
* ```
6962
*
7063
* @param string $name The input command name
7164
*
72-
* @return array return route info array. If not found, will return empty array.
65+
* @return array{name:string, cmdId: string, config: array, handler: mixed} return route info. If not found, will return empty array.
7366
*/
7467
public function match(string $name): array;
7568
}

0 commit comments

Comments
 (0)