Skip to content

Commit 4becec5

Browse files
committed
change app command list display style
1 parent 618fac2 commit 4becec5

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

src/Base/AbstractApplication.php

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ abstract class AbstractApplication implements ApplicationInterface
3838
* @var array
3939
*/
4040
protected static $internalOptions = [
41-
'--debug' => 'setting the application runtime debug level',
42-
'--no-color' => 'setting no color for message output',
43-
'--version' => 'Show application version information',
41+
'--debug' => 'Setting the application runtime debug level',
42+
'--profile' => 'Display timing and memory usage information',
43+
'--no-color' => 'Disable color/ANSI for message output',
44+
'-h, --help' => 'Display this help message',
45+
'-V, --version' => 'Show application version information',
4446
];
4547

4648
/**
@@ -50,6 +52,7 @@ abstract class AbstractApplication implements ApplicationInterface
5052
private $meta = [
5153
'name' => 'My Console',
5254
'debug' => false,
55+
'profile' => false,
5356
'version' => '0.5.1',
5457
'publishAt' => '2017.03.24',
5558
'updateAt' => '2017.03.24',
@@ -176,8 +179,8 @@ abstract public function runAction($name, $action, $believable = false, $standAl
176179
protected function afterRun()
177180
{
178181
// display runtime info
179-
if ($this->isDebug()) {
180-
$title = '---------- Runtime Stats(debug=true) ----------';
182+
if ($this->isProfile()) {
183+
$title = '---------- Runtime Stats(profile=true) ----------';
181184
$stats = $this->meta['_stats'];
182185
$this->meta['_stats'] = Helper::runtime($stats['startTime'], $stats['startMemory'], $stats);
183186
$this->output->write('');
@@ -251,7 +254,7 @@ public function handleException($e)
251254
$this->output->write($message, false);
252255
} else {
253256
// simple output
254-
$this->output->error('An error occurred! MESSAGE: ' . $e->getMessage());
257+
$this->output->error('An error occurred! MESSAGE: ' . $e->getMessage() . '. you can use --debug to see error details.');
255258
}
256259
}
257260

@@ -328,7 +331,7 @@ public function showHelpInfo($quit = true)
328331
$sep = $this->delimiter;
329332

330333
$this->output->helpPanel([
331-
'usage' => "$script {route|command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
334+
'usage' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
332335
'example' => [
333336
"$script test (run a independent command)",
334337
"$script home{$sep}index (run a command of the group)",
@@ -343,7 +346,7 @@ public function showHelpInfo($quit = true)
343346
*/
344347
public function showVersionInfo($quit = true)
345348
{
346-
$date = date('Y-m-d');
349+
$date = date('Y.m.d');
347350
$name = $this->getMeta('name', 'Console Application');
348351
$version = $this->getMeta('version', 'Unknown');
349352
$publishAt = $this->getMeta('publishAt', 'Unknown');
@@ -356,7 +359,8 @@ public function showVersionInfo($quit = true)
356359
'System Info' => "PHP version <info>$phpVersion</info>, on <info>$os</info> system",
357360
'Application Info' => "Update at <info>$updateAt</info>, publish at <info>$publishAt</info>(current $date)",
358361
], null, [
359-
'leftChar' => ''
362+
'leftChar' => '',
363+
'sepChar' => ' : '
360364
]);
361365

362366
$quit && $this->stop();
@@ -368,28 +372,33 @@ public function showVersionInfo($quit = true)
368372
*/
369373
public function showCommandList($quit = true)
370374
{
371-
$desPlaceholder = 'No description of the command';
372375
$script = $this->getScriptName();
376+
$hasGroup = $hasCommand = false;
373377
$controllerArr = $commandArr = [];
374-
375-
// built in commands
376-
$internalCommands = static::$internalCommands;
377-
ksort($internalCommands);
378+
$desPlaceholder = 'No description of the command';
378379

379380
// all console controllers
381+
$controllerArr[] = PHP_EOL . '- <cyan>Group Commands</cyan>';
380382
$controllers = $this->controllers;
381383
ksort($controllers);
382384

383385
foreach ($controllers as $name => $controller) {
386+
$hasGroup = true;
384387
/** @var AbstractCommand $controller */
385388
$controllerArr[$name] = $controller::getDescription() ?: $desPlaceholder;
386389
}
387390

391+
if (!$hasGroup) {
392+
$controllerArr[] = '... No register any group command(controller)';
393+
}
394+
388395
// all independent commands
389396
$commands = $this->commands;
397+
$commandArr[] = PHP_EOL . '- <cyan>Independent Commands</cyan>';
390398
ksort($commands);
391399
foreach ($commands as $name => $command) {
392400
$desc = $desPlaceholder;
401+
$hasCommand = true;
393402

394403
/** @var AbstractCommand $command */
395404
if (is_subclass_of($command, CommandInterface::class)) {
@@ -405,17 +414,36 @@ public function showCommandList($quit = true)
405414
$commandArr[$name] = $desc;
406415
}
407416

408-
// $this->output->write('There are all console controllers and independent commands.');
417+
if (!$hasCommand) {
418+
$commandArr[] = '... No register any group command(controller)';
419+
}
420+
421+
// built in commands
422+
$internalCommands = static::$internalCommands;
423+
ksort($internalCommands);
424+
array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
425+
409426
$this->output->mList([
410427
//'There are all console controllers and independent commands.',
411428
'Usage:' => "$script {route|command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
412-
'Group Commands:' => $controllerArr ?: '... No register any group command(controller)',
413-
'Independent Commands:' => $commandArr ?: '... No register any independent command',
414-
'Internal Commands:' => $internalCommands,
415-
'Internal Options:' => self::$internalOptions
429+
'Options:' => self::$internalOptions,
430+
'Available Commands:' => array_merge($controllerArr, $commandArr, $internalCommands),
431+
//'Independent Commands:' => $commandArr ?: '... No register any independent command',
432+
// 'Internal Commands:' => $internalCommands,
416433
]);
417434

435+
// $this->output->mList([
436+
// //'There are all console controllers and independent commands.',
437+
// 'Usage:' => "$script {route|command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
438+
// 'Options:' => self::$internalOptions,
439+
// 'Group Commands:' => $controllerArr ?: '... No register any group command(controller)',
440+
// 'Independent Commands:' => $commandArr ?: '... No register any independent command',
441+
// 'Internal Commands:' => $internalCommands,
442+
// ]);
443+
444+
unset($controllerArr, $commandArr, $internalCommands);
418445
$this->output->write("More command information, please use: <cyan>$script {command} -h</cyan>");
446+
419447
$quit && $this->stop();
420448
}
421449

@@ -589,6 +617,15 @@ public function isDebug()
589617
return $this->input->getOpt('debug', $this->meta['debug']);
590618
}
591619

620+
/**
621+
* is profile
622+
* @return boolean
623+
*/
624+
public function isProfile()
625+
{
626+
return (bool)$this->input->getOpt('profile', $this->getMeta('profile'));
627+
}
628+
592629
/**
593630
* @return array
594631
*/

0 commit comments

Comments
 (0)