Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d00da3b

Browse files
committed
update some logic for cli utils
1 parent 5081383 commit d00da3b

File tree

1 file changed

+89
-17
lines changed

1 file changed

+89
-17
lines changed

‎libs/cli-utils/src/App.php

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use function function_exists;
1717
use function getcwd;
1818
use function is_array;
19+
use function is_object;
1920
use function is_string;
2021
use function method_exists;
2122
use function str_pad;
2223
use function strlen;
2324
use function trim;
25+
use function ucfirst;
2426

2527
/**
2628
* Class App - A lite CLI Application
@@ -29,6 +31,12 @@
2931
*/
3032
class App
3133
{
34+
private const COMMAND_CONFIG = [
35+
'desc' => '',
36+
'usage' => '',
37+
'help' => '',
38+
];
39+
3240
/** @var string Current dir */
3341
private $pwd;
3442

@@ -58,7 +66,7 @@ class App
5866
private $commands = [];
5967

6068
/**
61-
* @var array Description message for the commands
69+
* @var array Command messages for the commands
6270
*/
6371
private $messages = [];
6472

@@ -163,7 +171,7 @@ public function runHandler(string $command, $handler)
163171
}
164172

165173
// a \Closure OR $handler->__invoke()
166-
if (method_exists($handler, '__invoke')) {
174+
if (is_object($handler) && method_exists($handler, '__invoke')) {
167175
return $handler($this);
168176
}
169177

@@ -177,7 +185,7 @@ public function runHandler(string $command, $handler)
177185
*/
178186
protected function handleException(Throwable $e): int
179187
{
180-
$code = $e->getCode() !== 0 ? $e->getCode() : 133;
188+
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
181189

182190
printf("Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", $code, $e->getMessage(), $e->getFile(),
183191
$e->getLine(), $e->getTraceAsString());
@@ -186,24 +194,44 @@ protected function handleException(Throwable $e): int
186194
}
187195

188196
/**
189-
* @param string $command
190-
* @param callable $handler
191-
* @param string $description
192-
*
193-
* @throws InvalidArgumentException
197+
* @param string $command
198+
* @param callable $handler
199+
* @param null|array|string $config
194200
*/
195-
public function addCommand(string $command, callable $handler, string $description = ''): void
201+
public function add(string $command, callable $handler, $config = null): void
202+
{
203+
$this->addCommand($command, $handler, $config);
204+
}
205+
206+
/**
207+
* @param string $command
208+
* @param callable $handler
209+
* @param null|array|string $config
210+
*/
211+
public function addCommand(string $command, callable $handler, $config = null): void
196212
{
197213
if (!$command || !$handler) {
198-
throw new InvalidArgumentException('Invalid arguments');
214+
throw new InvalidArgumentException('Invalid arguments for add command');
199215
}
200216

201217
if (($len = strlen($command)) > $this->keyWidth) {
202218
$this->keyWidth = $len;
203219
}
204220

205221
$this->commands[$command] = $handler;
206-
$this->messages[$command] = trim($description);
222+
223+
if (is_string($config)) {
224+
$desc = trim($config);
225+
$config = self::COMMAND_CONFIG;
226+
227+
// append desc
228+
$config['desc'] = $desc;
229+
230+
// save
231+
$this->messages[$command] = $config;
232+
} elseif (is_array($config)) {
233+
$this->messages[$command] = \array_merge(self::COMMAND_CONFIG, $config);
234+
}
207235
}
208236

209237
/**
@@ -214,15 +242,15 @@ public function addCommand(string $command, callable $handler, string $descripti
214242
public function commands(array $commands): void
215243
{
216244
foreach ($commands as $command => $handler) {
217-
$des = '';
245+
$desc = '';
218246

219247
if (is_array($handler)) {
220248
$conf = array_values($handler);
221249
$handler = $conf[0];
222-
$des = $conf[1] ?? '';
250+
$desc = $conf[1] ?? '';
223251
}
224252

225-
$this->addCommand($command, $handler, $des);
253+
$this->addCommand($command, $handler, $desc);
226254
}
227255
}
228256

@@ -239,13 +267,13 @@ public function displayHelp(string $err = ''): void
239267
echo Color::render("<red>ERROR</red>: $err\n\n");
240268
}
241269

242-
$commandWidth = 12;
270+
$commandWidth = $this->keyWidth;
243271
// help
244272
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";
245273

246-
foreach ($this->messages as $command => $desc) {
274+
foreach ($this->messages as $command => $item) {
247275
$command = str_pad($command, $commandWidth, '');
248-
$desc = $desc ?: 'No description for the command';
276+
$desc = $item['desc'] ? ucfirst($item['desc']) : 'No description for the command';
249277
$help .= "$command$desc\n";
250278
}
251279

@@ -264,6 +292,28 @@ public function getArg($name, $default = null)
264292
return $this->args[$name] ?? $default;
265293
}
266294

295+
/**
296+
* @param string|int $name
297+
* @param int $default
298+
*
299+
* @return int
300+
*/
301+
public function getIntArg($name, int $default = 0): int
302+
{
303+
return (int)$this->getArg($name, $default);
304+
}
305+
306+
/**
307+
* @param string|int $name
308+
* @param string $default
309+
*
310+
* @return string
311+
*/
312+
public function getStrArg($name, string $default = ''): string
313+
{
314+
return (string)$this->getArg($name, $default);
315+
}
316+
267317
/**
268318
* @param string $name
269319
* @param mixed $default
@@ -275,6 +325,28 @@ public function getOpt(string $name, $default = null)
275325
return $this->opts[$name] ?? $default;
276326
}
277327

328+
/**
329+
* @param string $name
330+
* @param int $default
331+
*
332+
* @return int
333+
*/
334+
public function getIntOpt(string $name, int $default = 0): int
335+
{
336+
return (int)$this->getOpt($name, $default);
337+
}
338+
339+
/**
340+
* @param string $name
341+
* @param string $default
342+
*
343+
* @return string
344+
*/
345+
public function getStrOpt(string $name, string $default = ''): string
346+
{
347+
return (string)$this->getOpt($name, $default);
348+
}
349+
278350
/****************************************************************************
279351
* getter/setter methods
280352
****************************************************************************/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /