Skip to content

Commit cef2fc4

Browse files
committed
fix: #19 sub-command desc display error
1 parent 6b8010d commit cef2fc4

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

examples/Controller/HomeController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected static function commandAliases(): array
4242
'ml' => 'multiList',
4343
'sl' => 'splitLine',
4444
'dt' => 'dynamicText',
45+
'da' => 'defArg',
4546
];
4647
}
4748

@@ -50,6 +51,10 @@ protected function init(): void
5051
parent::init();
5152

5253
$this->addCommentsVar('internalFonts', implode(',', ArtFont::getInternalFonts()));
54+
55+
$this->setCommandMeta('defArg', [
56+
'desc' => 'the command args and opts config use defined configure, it like symfony console, please see defArgConfigure()',
57+
]);
5358
}
5459

5560
/**
@@ -75,6 +80,7 @@ protected function afterExecute(): void
7580
/**
7681
* this is a command's description message
7782
* the second line text
83+
*
7884
* @usage {command} [arg ...] [--opt ...]
7985
* @arguments
8086
* arg1 argument description 1
@@ -107,15 +113,13 @@ public function disabledCommand(): void
107113
protected function defArgConfigure(): void
108114
{
109115
$this->createDefinition()
110-
->setDescription('the command arg/opt config use defined configure, it like symfony console: argument define by position')
116+
// ->setDescription('the command args and opts config use defined configure, it like symfony console, please see defArgConfigure()')
111117
->addArgument('name', Input::ARG_REQUIRED, "description for the argument 'name'")
112118
->addOption('yes', 'y', Input::OPT_BOOLEAN, "description for the option 'yes'")
113119
->addOption('opt1', null, Input::OPT_REQUIRED, "description for the option 'opt1'");
114120
}
115121

116-
/**
117-
* the command arg/opt config use defined configure, it like symfony console: argument define by position
118-
*/
122+
// desc set at $this->commandMetas.
119123
public function defArgCommand(): void
120124
{
121125
$this->output->dump($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));

src/AbstractHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ abstract class AbstractHandler implements CommandHandlerInterface
101101
/**
102102
* @var InputDefinition|null
103103
*/
104-
private $definition;
104+
protected $definition;
105105

106106
/**
107107
* @var string
108108
*/
109-
private $processTitle = '';
109+
protected $processTitle = '';
110110

111111
/**
112112
* @var array

src/Controller.php

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Generator;
1212
use Inhere\Console\Contract\ControllerInterface;
1313
use Inhere\Console\IO\Input;
14+
use Inhere\Console\IO\InputDefinition;
1415
use Inhere\Console\IO\Output;
1516
use Inhere\Console\Util\FormatUtil;
1617
use Inhere\Console\Util\Helper;
@@ -20,8 +21,8 @@
2021
use ReflectionObject;
2122
use RuntimeException;
2223
use Toolkit\Cli\ColorTag;
23-
use Toolkit\Stdlib\Util\PhpDoc;
2424
use Toolkit\Stdlib\Str;
25+
use Toolkit\Stdlib\Util\PhpDoc;
2526
use function array_flip;
2627
use function array_keys;
2728
use function array_merge;
@@ -67,6 +68,7 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
6768

6869
/**
6970
* Action name, no suffix.
71+
* eg: updateCommand() -> action: 'update'
7072
*
7173
* @var string
7274
*/
@@ -104,6 +106,21 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
104106
*/
105107
private $disabledCommands = [];
106108

109+
/**
110+
* Metadata for sub-commands. such as: desc, alias
111+
* Notice: you must add metadata on `init()`
112+
*
113+
* [
114+
* 'command real name' => [
115+
* 'desc' => 'sub command description',
116+
* 'alias' => [],
117+
* ],
118+
* ],
119+
*
120+
* @var array
121+
*/
122+
protected $commandMetas = [];
123+
107124
/**
108125
* Define command alias mapping. please rewrite it on sub-class.
109126
*
@@ -177,11 +194,11 @@ public function run(string $command = '')
177194
}
178195

179196
$this->action = Str::camelCase($this->getRealCommandName($command));
180-
181197
if (!$this->action) {
182198
return $this->showHelp();
183199
}
184200

201+
// do running
185202
return parent::run($command);
186203
}
187204

@@ -190,14 +207,32 @@ public function run(string $command = '')
190207
*/
191208
protected function configure(): void
192209
{
193-
// eg. indexConfigure() for indexCommand()
210+
// eg. use `indexConfigure()` for `indexCommand()`
194211
$method = $this->action . self::CONFIGURE_SUFFIX;
195212

196213
if (method_exists($this, $method)) {
197214
$this->$method($this->input);
198215
}
199216
}
200217

218+
/**
219+
* @return InputDefinition
220+
*/
221+
protected function createDefinition(): InputDefinition
222+
{
223+
if (!$this->definition) {
224+
$this->definition = new InputDefinition();
225+
226+
// if have been set desc for the sub-command
227+
$cmdDesc = $this->commandMetas[$this->action]['desc'] ?? '';
228+
if ($cmdDesc) {
229+
$this->definition->setDescription($cmdDesc);
230+
}
231+
}
232+
233+
return $this->definition;
234+
}
235+
201236
/**
202237
* Run command action in the group
203238
*
@@ -271,6 +306,9 @@ protected function showHelp(): bool
271306
return $this->helpCommand() === 0;
272307
}
273308

309+
/**
310+
* @param array $help
311+
*/
274312
protected function beforeRenderCommandHelp(array &$help): void
275313
{
276314
$help['Group Options:'] = FormatUtil::alignOptions($this->groupOptions);
@@ -305,7 +343,7 @@ final public function helpCommand(): int
305343
{
306344
$action = $this->action;
307345

308-
// For all sub-commands of the controller
346+
// Not input action, for all sub-commands of the controller
309347
if (!$action && !($action = $this->getFirstArg())) {
310348
$this->showCommandList();
311349
return 0;
@@ -354,12 +392,15 @@ final public function showCommandList(): void
354392
$showDisabled = (bool)$this->getOpt('show-disabled', false);
355393
$defaultDes = 'No description message';
356394

395+
/**
396+
* @var $cmd string The command name.
397+
*/
357398
foreach ($this->getAllCommandMethods($ref) as $cmd => $m) {
358399
if (!$cmd) {
359400
continue;
360401
}
361402

362-
$desc = $defaultDes;
403+
$desc = $this->getCommandMeta('desc', $defaultDes, $cmd);
363404
if ($phpDoc = $m->getDocComment()) {
364405
$desc = PhpDoc::firstLine($phpDoc);
365406
}
@@ -630,4 +671,37 @@ public function setDelimiter(string $delimiter): void
630671
{
631672
$this->delimiter = $delimiter;
632673
}
674+
675+
/**
676+
* @return array
677+
*/
678+
public function getCommandMetas(): array
679+
{
680+
return $this->commandMetas;
681+
}
682+
683+
/**
684+
* @param string $command
685+
* @param array $meta eg: ['desc' => '', 'alias' => []]
686+
*/
687+
public function setCommandMeta(string $command, array $meta): void
688+
{
689+
if ($command) {
690+
$this->commandMetas[$command] = $meta;
691+
}
692+
}
693+
694+
/**
695+
* @param string $key
696+
* @param null $default
697+
* @param string $command if not set, will use $this->action
698+
*
699+
* @return mixed|null
700+
*/
701+
public function getCommandMeta(string $key, $default = null, string $command = '')
702+
{
703+
$action = $command ?: $this->action;
704+
705+
return $this->commandMetas[$action][$key] ?? $default;
706+
}
633707
}

0 commit comments

Comments
 (0)