facebook/hh-clilib

该包已被废弃,不再维护。未建议替代包。

维护者

详细信息

github.com/hhvm/hh-clilib

源代码

问题

安装量: 702,768

依赖者: 15

建议者: 0

安全: 0

星标: 8

关注者: 21

分支: 11

公开问题: 3

语言:Hack

v2.6.0 2020-12-14 21:29 UTC

README

Continuous Integration

此库提供基本的命令行处理,包括

  • 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',
		),
	];
}

参数

参数没有名称,可能是必需的。要支持参数,扩展 CLIWithArgumentsCLIWithRequiredArguments

参数始终是字符串,可以通过 ->getArguments(); 获取。

贡献

CONTRIBUTING.md

许可

hh-clilib 是 MIT 许可。