From 498cd3d66bdce14e15a850a73e6b7ef564094748 Mon Sep 17 00:00:00 2001 From: inhere Date: Tue, 25 Jun 2019 00:52:53 +0800 Subject: [PATCH] update: add some new methods for cli utils --- libs/cli-utils/src/App.php | 45 +++++++++++++++++++++++++----- libs/cli-utils/src/Color.php | 14 ++++++++++ libs/cli-utils/src/Highlighter.php | 8 ++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/libs/cli-utils/src/App.php b/libs/cli-utils/src/App.php index 47ba7da..e541d21 100644 --- a/libs/cli-utils/src/App.php +++ b/libs/cli-utils/src/App.php @@ -10,11 +10,13 @@ use InvalidArgumentException; use Throwable; +use function array_merge; use function array_shift; use function array_values; use function class_exists; use function function_exists; use function getcwd; +use function implode; use function is_array; use function is_object; use function is_string; @@ -120,14 +122,18 @@ public function dispatch(bool $exit = true): void return; } - $status = 0; + if (!isset($this->commands[$command])) { + $this->displayHelp("The command {$command} not exists!"); + return; + } + + if (isset($this->opts['h']) || isset($this->opts['help'])) { + $this->displayCommandHelp($command); + return; + } try { - if (isset($this->commands[$command])) { - $status = $this->runHandler($command, $this->commands[$command]); - } else { - $this->displayHelp("The command {$command} not exists!"); - } + $status = $this->runHandler($command, $this->commands[$command]); } catch (Throwable $e) { $status = $this->handleException($e); } @@ -230,7 +236,7 @@ public function addCommand(string $command, callable $handler, $config = null): // save $this->messages[$command] = $config; } elseif (is_array($config)) { - $this->messages[$command] = \array_merge(self::COMMAND_CONFIG, $config); + $this->messages[$command] = array_merge(self::COMMAND_CONFIG, $config); } } @@ -281,6 +287,31 @@ public function displayHelp(string $err = ''): void exit(0); } + /** + * @param string $name + */ + public function displayCommandHelp(string $name): void + { + $fullCmd = $this->script . " $name"; + $config = $this->messages[$name] ?? []; + $usage = "$fullCmd [args ...] [--opts ...]"; + + if (!$config) { + $nodes = [ + 'No description for the command', + "Usage: \n $usage" + ]; + } else { + $nodes = [ + ucfirst($config['desc']), + "Usage: \n " . ($config['usage'] ?: $usage), + $config['help'] + ]; + } + + echo Color::render(implode("\n", $nodes)); + } + /** * @param string|int $name * @param mixed $default diff --git a/libs/cli-utils/src/Color.php b/libs/cli-utils/src/Color.php index 2269f2e..f6eea2f 100644 --- a/libs/cli-utils/src/Color.php +++ b/libs/cli-utils/src/Color.php @@ -213,6 +213,19 @@ public static function printf(string $format, ...$args): void echo self::render(sprintf($format, ...$args)); } + /** + * Print colored message to STDOUT + * + * @param string|array $messages + * @param string $style + */ + public function println($messages, string $style = 'info'): void + { + $string = is_array($messages) ? implode("\n", $messages) : (string)$messages; + + echo self::render($string, $style); + } + /******************************************************************************* * color render ******************************************************************************/ @@ -240,6 +253,7 @@ public static function render(string $text, $style = null): string // use defined style: 'green' if (is_string($style)) { $color = self::STYLES[$style] ?? '0'; + // custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE] } elseif (is_array($style)) { $color = implode(';', $style); diff --git a/libs/cli-utils/src/Highlighter.php b/libs/cli-utils/src/Highlighter.php index 8100f1e..6146423 100644 --- a/libs/cli-utils/src/Highlighter.php +++ b/libs/cli-utils/src/Highlighter.php @@ -14,6 +14,7 @@ use function defined; use function end; use function explode; +use function file_get_contents; use function function_exists; use function implode; use function is_array; @@ -122,6 +123,13 @@ public function highlight(string $source, bool $withLineNumber = false): string return implode(PHP_EOL, $lines); } + public function highlightFile(string $file, bool $withLineNumber = false): string + { + $source = file_get_contents($file); + + return $this->highlight($source, $withLineNumber); + } + /** * @param string $source * @param int $lineNumber