Skip to content

Commit 2490844

Browse files
committed
update soem for display helpr
1 parent b151992 commit 2490844

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

src/App.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class App
3939
{
4040
/** @var self */
4141
public static $i;
42-
42+
4343
private const COMMAND_CONFIG = [
4444
'desc' => '',
4545
'usage' => '',
@@ -49,6 +49,14 @@ class App
4949
/** @var string Current dir */
5050
private $pwd;
5151

52+
/**
53+
* @var array
54+
*/
55+
private $metas = [
56+
'name' => 'My application',
57+
'desc' => 'My command line application',
58+
];
59+
5260
/**
5361
* @var array Parsed from `arg0 name=val var2=val2`
5462
*/
@@ -87,18 +95,22 @@ class App
8795
/**
8896
* Class constructor.
8997
*
90-
* @param array|null $argv
98+
* @param array $config
99+
* @param array $argv
91100
*/
92-
public function __construct(array $argv = null)
101+
public function __construct(array $config = [], array $argv = null)
93102
{
94103
// save self
95104
self::$i = $this;
96-
105+
97106
// get current dir
98107
$this->pwd = getcwd();
99108

100109
// parse cli argv
101110
$argv = $argv ?? (array)$_SERVER['argv'];
111+
if ($config) {
112+
$this->setMetas($config);
113+
}
102114

103115
// get script file
104116
$this->script = array_shift($argv);
@@ -188,7 +200,7 @@ public function stop($code = 0): void
188200

189201
/**
190202
* @param string $command
191-
* @param $handler
203+
* @param mixed $handler
192204
*
193205
* @return mixed
194206
* @throws InvalidArgumentException
@@ -246,6 +258,10 @@ protected function handleException(Throwable $e): int
246258
*/
247259
public function addByConfig(callable $handler, array $config): void
248260
{
261+
if (empty($config['name']) || !$handler) {
262+
throw new InvalidArgumentException('Invalid arguments for add command');
263+
}
264+
249265
$this->addCommand($config['name'], $handler, $config);
250266
}
251267

@@ -324,17 +340,21 @@ public function displayHelp(string $err = ''): void
324340
}
325341

326342
// help
327-
$len = $this->keyWidth;
328-
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";
343+
$desc = ucfirst($this->metas['desc']);
344+
$usage = "<cyan>{$this->script} COMMAND -h</cyan>";
345+
346+
$help = "$desc\n<comment>Usage:</comment> $usage\n<comment>Commands:</comment>\n";
329347
$data = $this->messages;
330348
ksort($data);
331349

332350
foreach ($data as $command => $item) {
333-
$command = str_pad($command, $len, ' ');
351+
$command = str_pad($command, $this->keyWidth, ' ');
334352
$desc = $item['desc'] ? ucfirst($item['desc']) : 'No description for the command';
335-
$help .= " $command $desc\n";
353+
$help .= " <green>$command</green> $desc\n";
336354
}
337355

356+
$help .= "\nFor command usage please run: <cyan>{$this->script} COMMAND -h</cyan>";
357+
338358
echo Color::render($help) . PHP_EOL;
339359
exit(0);
340360
}
@@ -550,14 +570,6 @@ public function getMessages(): array
550570
return $this->messages;
551571
}
552572

553-
/**
554-
* @param array $messages
555-
*/
556-
public function setMessages(array $messages): void
557-
{
558-
$this->messages = $messages;
559-
}
560-
561573
/**
562574
* @return int
563575
*/
@@ -582,4 +594,19 @@ public function getPwd(): string
582594
return $this->pwd;
583595
}
584596

597+
/**
598+
* @return array
599+
*/
600+
public function getMetas(): array
601+
{
602+
return $this->metas;
603+
}
604+
605+
/**
606+
* @param array $metas
607+
*/
608+
public function setMetas(array $metas): void
609+
{
610+
$this->metas = array_merge($this->metas, $metas);
611+
}
585612
}

0 commit comments

Comments
 (0)