Overview
Zero-dependency CLI argument and flag parser for HolyPHP.
Supports boolean flags (--verbose, -v, combined -vf), key-value options (--port=8080, --port 8080, -p 8080, -p=8080), positional arguments, -- end-of-options separator, default values, required options, auto-generated --help text, and clear validation errors.
bundled — works offline
zero dependencies
flags
options
Quick start
import "cli";
$parser = new CliParser("serve", "Start a local HTTP server");
$parser
->flag("verbose", "v", "Enable verbose logging")
->option("host", "H", "Bind address", "127.0.0.1", false)
->option("port", "p", "Port number", "8080", true);
$result = $parser->parse(["-v", "--port=3000", "public/"]);
if ($result["help"]) {
println($parser->help());
} else {
println($result["options"]["host"]); // "127.0.0.1"
println($result["options"]["port"]); // "3000"
println($result["options"]["verbose"]); // true
println($result["args"][0]); // "public/"
}
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install cliargs
This adds the library to hphp.yaml, installs it into .hphp/packages/cliargs/ and pins the exact version in hphp.lock:
require:
cliargs: "^1.0"
Use it
import "cliargs";
cliargs is bundled with the compiler, so import "cliargs"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install cliargs # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install cliargs
Documentation
API Reference
class CliParser
new CliParser(string $name = "app", string $description = "")
Creates a new CLI parser with a program name and optional description.
->flag(string $long, string $short = "", string $description = ""): CliParser
Registers a boolean flag (defaults to false). Returns $this for method chaining.
->option(string $long, string $short = "", string $description = "", $default = null, bool $required = false): CliParser
Registers a key-value option. Returns $this for method chaining.
->allowUnknown(bool $allow = true): CliParser
When set to true, unknown flags/options are silently skipped instead of throwing an error.
->parse(array $argv): array
Parses an array of CLI argument strings and returns an associative array:
options: associative array mapping each registered long option name to its parsed or default value
args: array of positional arguments
help: true if -h or --help was passed (skips required-option validation)
->help(): string
Returns formatted help text including usage, description, all registered flags/options, defaults, (required) markers, and -h, --help.
---
cli_parse(array $argv, array $spec = []): array
Convenience function for one-shot parsing without manually instantiating CliParser.
$spec keys:
name (string, optional): program name
description (string, optional): program description
allowUnknown (bool, optional): ignore unknown flags
flags (array, optional): list of ["long" => "...", "short" => "...", "description" => "..."]
options (array, optional): list of ["long" => "...", "short" => "...", "description" => "...", "default" => ..., "required" => bool]
Returns the same structure as CliParser::parse() plus "helpText" containing the generated help string.
Error Handling
All validation and parsing errors throw Exception with a clear cli: ... message:
cli: program name cannot be empty
cli: invalid option name '...'
cli: duplicate option '--...'
cli: duplicate short option '-...'
cli: unknown option '--...'
cli: unknown short option '-...'
cli: option '--...' requires a value
cli: flag '--...' does not accept a value
cli: missing required option '--...'
Examples
01Quick start
Bundled with the compiler — import "cliargs"; works offline in any file, or add it to a project with hphp install cliargs.
import "cli";
$parser = new CliParser("serve", "Start a local HTTP server");
$parser
->flag("verbose", "v", "Enable verbose logging")
->option("host", "H", "Bind address", "127.0.0.1", false)
->option("port", "p", "Port number", "8080", true);
$result = $parser->parse(["-v", "--port=3000", "public/"]);
if ($result["help"]) {
println($parser->help());
} else {
println($result["options"]["host"]); // "127.0.0.1"
println($result["options"]["port"]); // "3000"
println($result["options"]["verbose"]); // true
println($result["args"][0]); // "public/"
}
Try ithphp install cliargs
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_cliargs.hphp.