diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-07-09 19:40:17 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-07-09 19:40:17 +0300 |
commit | f7bfdf58def6aadc922e1632f407d1418269a0d7 (patch) | |
tree | d7a0b2819e6a26c11d40ee0b27267ea827fbb345 /lib/cli.php |
initial
Diffstat (limited to 'lib/cli.php')
-rw-r--r-- | lib/cli.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/cli.php b/lib/cli.php new file mode 100644 index 0000000..3bda20f --- /dev/null +++ b/lib/cli.php @@ -0,0 +1,70 @@ +<?php + +class cli { + + protected ?array $commandsCache = null; + + public function __construct( + protected string $ns + ) {} + + protected function usage($error = null): void { + global $argv; + + if (!is_null($error)) + echo "error: {$error}\n\n"; + + echo "Usage: $argv[0] COMMAND\n\nCommands:\n"; + foreach ($this->getCommands() as $c) + echo " $c\n"; + + exit(is_null($error) ? 0 : 1); + } + + public function getCommands(): array { + if (is_null($this->commandsCache)) { + $funcs = array_filter(get_defined_functions()['user'], fn(string $f) => str_starts_with($f, $this->ns)); + $funcs = array_map(fn(string $f) => str_replace('_', '-', substr($f, strlen($this->ns.'\\'))), $funcs); + $this->commandsCache = array_values($funcs); + } + return $this->commandsCache; + } + + public function run(): void { + global $argv, $argc; + + if (PHP_SAPI != 'cli') + cli::die('SAPI != cli'); + + if ($argc < 2) + $this->usage(); + + $func = $argv[1]; + if (!in_array($func, $this->getCommands())) + self::usage('unknown command "'.$func.'"'); + + $func = str_replace('-', '_', $func); + call_user_func($this->ns.'\\'.$func); + } + + public static function input(string $prompt): string { + echo $prompt; + $input = substr(fgets(STDIN), 0, -1); + return $input; + } + + public static function silentInput(string $prompt = ''): string { + echo $prompt; + system('stty -echo'); + $input = substr(fgets(STDIN), 0, -1); + system('stty echo'); + echo "\n"; + return $input; + } + + public static function die($error): void { + fwrite(STDERR, "error: {$error}\n"); + exit(1); + } + +}
\ No newline at end of file |