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 498cd3d

Browse files
committed
update: add some new methods for cli utils
1 parent d00da3b commit 498cd3d

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
use InvalidArgumentException;
1212
use Throwable;
13+
use function array_merge;
1314
use function array_shift;
1415
use function array_values;
1516
use function class_exists;
1617
use function function_exists;
1718
use function getcwd;
19+
use function implode;
1820
use function is_array;
1921
use function is_object;
2022
use function is_string;
@@ -120,14 +122,18 @@ public function dispatch(bool $exit = true): void
120122
return;
121123
}
122124

123-
$status = 0;
125+
if (!isset($this->commands[$command])) {
126+
$this->displayHelp("The command {$command} not exists!");
127+
return;
128+
}
129+
130+
if (isset($this->opts['h']) || isset($this->opts['help'])) {
131+
$this->displayCommandHelp($command);
132+
return;
133+
}
124134

125135
try {
126-
if (isset($this->commands[$command])) {
127-
$status = $this->runHandler($command, $this->commands[$command]);
128-
} else {
129-
$this->displayHelp("The command {$command} not exists!");
130-
}
136+
$status = $this->runHandler($command, $this->commands[$command]);
131137
} catch (Throwable $e) {
132138
$status = $this->handleException($e);
133139
}
@@ -230,7 +236,7 @@ public function addCommand(string $command, callable $handler, $config = null):
230236
// save
231237
$this->messages[$command] = $config;
232238
} elseif (is_array($config)) {
233-
$this->messages[$command] = \array_merge(self::COMMAND_CONFIG, $config);
239+
$this->messages[$command] = array_merge(self::COMMAND_CONFIG, $config);
234240
}
235241
}
236242

@@ -281,6 +287,31 @@ public function displayHelp(string $err = ''): void
281287
exit(0);
282288
}
283289

290+
/**
291+
* @param string $name
292+
*/
293+
public function displayCommandHelp(string $name): void
294+
{
295+
$fullCmd = $this->script . "$name";
296+
$config = $this->messages[$name] ?? [];
297+
$usage = "$fullCmd [args ...] [--opts ...]";
298+
299+
if (!$config) {
300+
$nodes = [
301+
'No description for the command',
302+
"<comment>Usage:</comment> \n$usage"
303+
];
304+
} else {
305+
$nodes = [
306+
ucfirst($config['desc']),
307+
"<comment>Usage:</comment> \n" . ($config['usage'] ?: $usage),
308+
$config['help']
309+
];
310+
}
311+
312+
echo Color::render(implode("\n", $nodes));
313+
}
314+
284315
/**
285316
* @param string|int $name
286317
* @param mixed $default

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ public static function printf(string $format, ...$args): void
213213
echo self::render(sprintf($format, ...$args));
214214
}
215215

216+
/**
217+
* Print colored message to STDOUT
218+
*
219+
* @param string|array $messages
220+
* @param string $style
221+
*/
222+
public function println($messages, string $style = 'info'): void
223+
{
224+
$string = is_array($messages) ? implode("\n", $messages) : (string)$messages;
225+
226+
echo self::render($string, $style);
227+
}
228+
216229
/*******************************************************************************
217230
* color render
218231
******************************************************************************/
@@ -240,6 +253,7 @@ public static function render(string $text, $style = null): string
240253
// use defined style: 'green'
241254
if (is_string($style)) {
242255
$color = self::STYLES[$style] ?? '0';
256+
243257
// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
244258
} elseif (is_array($style)) {
245259
$color = implode(';', $style);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function defined;
1515
use function end;
1616
use function explode;
17+
use function file_get_contents;
1718
use function function_exists;
1819
use function implode;
1920
use function is_array;
@@ -122,6 +123,13 @@ public function highlight(string $source, bool $withLineNumber = false): string
122123
return implode(PHP_EOL, $lines);
123124
}
124125

126+
public function highlightFile(string $file, bool $withLineNumber = false): string
127+
{
128+
$source = file_get_contents($file);
129+
130+
return $this->highlight($source, $withLineNumber);
131+
}
132+
125133
/**
126134
* @param string $source
127135
* @param int $lineNumber

0 commit comments

Comments
(0)

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