facebook / hh-clilib
该包已被废弃,不再维护。未建议替代包。
v2.6.0
2020-12-14 21:29 UTC
Requires
- hhvm: ^4.76
- hhvm/hsl: ^4.0
- hhvm/hsl-experimental: ^4.58.0rc1
- hhvm/hsl-io: ^0.3.0
- hhvm/type-assert: ^4.0
Requires (Dev)
- facebook/fbexpect: ^2.6.1
- hhvm/hacktest: ^2.2.0rc1
- hhvm/hhvm-autoload: ^3.1.0
README
此库提供基本的命令行处理,包括
ARGV
的解析- 交互式 TTY 识别
- 彩色 TTY 识别
它旨在尽可能多的代码处于严格模式。
安装
hhvm composer.phar require facebook/hh-clilib
示例
在 src/MyCLI.hh
中
// MyCLI.hh <?hh // strict use type Facebook\CLILib\CLIBase; final class MyCLI extends CLIBase { <<__Override>> public async function mainAsync(): Awaitable<int> { $this->getStdout()->write("Hello, world!"); return 0; } }
在 bin/mycli
中
<?hh // not strict because of top-level statements. require_once(__DIR__.'/../vendor/hh_autoload.php'); MyCLI::main();
选项
选项是可选的,总是有长形式(例如 --foo
),可能有短形式(例如 -f
),并且可能需要值(例如 --foo=bar
或 --foo bar
)。
您可以通过实现 getSupportedOptions()
指定支持选项;--help
总是受支持。
<<__Override>> protected function getSupportedOptions(): vec<CLIOptions\CLIOption> { return vec[ CLIOptions\flag( () ==> { $this->verbosity++; }, "Increase output verbosity", '--verbose', '-v', ), CLIOptions\with_required_enum( OutputFormat::class, $f ==> { $this->format = $f; }, Str\format( "Desired output format (%s). Default: %s", Str\join(OutputFormat::getValues(), '|'), (string) $this->format, ), '--format', '-f', ), CLIOptions\with_required_string( $s ==> { $this->outputRoot = $s; }, "Directory for output files. Default: working directory", '--output', '-o', ), ]; }
参数
参数没有名称,可能是必需的。要支持参数,扩展 CLIWithArguments
或 CLIWithRequiredArguments
。
参数始终是字符串,可以通过 ->getArguments();
获取。
贡献
许可
hh-clilib 是 MIT 许可。