Hack CLI Library
, (*1)
This library provides basic command-line handling, including:
- parsing of ARGV
- interactive TTY detection
- color TTY detection, (*2)
It aims for as much code as possible to be in strict mode., (*3)
Installation
hhvm composer.phar require facebook/hh-clilib
Examples
In src/MyCLI.hh
:, (*4)
// 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;
}
}
In bin/mycli
:, (*5)
<?hh // not strict because of top-level statements.
require_once(__DIR__.'/../vendor/hh_autoload.php');
MyCLI::main();
Options
Options are optional, always have a long form (e.g. --foo
), may have a short form (e.g. -f
), and may
require a value (e.g. --foo=bar
or --foo bar
)., (*6)
You can specify supported options by implemented getSupportedOptions()
; --help
is always supported., (*7)
<<__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',
),
];
}
Arguments
Arguments do not have a name, and may be required. To support arguments, extend CLIWithArguments
or CLIWithRequiredArguments
., (*8)
Arguments are always strings, and can be retrieved via ->getArguments();
, (*9)
Contributing
See CONTRIBUTING.md., (*10)
License
hh-clilib is MIT-licensed., (*11)