It is my fervent wish that this file guide every AI coding agent working with code in this repository.
Any distilled, agent-facing documentation for this package - how it works
internally and the rationale behind key design decisions - lives in docs/.
Consult it before non-trivial changes; it is the source of truth from which the
public manual is distilled.
Two small classes, mostly clear from signatures; the value is a few traps - the
help-text-as-schema parser, the parse() sentinel, and the two terminal checks.
Read docs/internals.md before editing them.
Nette Command Line is a tiny, zero-dependency library with two utilities:
Parser (argument/option parsing, including help-text-driven definitions) and
Console (terminal color output with capability detection).
- PHP Version: 8.2 - 8.5
- Package:
nette/command-line
# Run all tests
vendor/bin/tester tests -s # or: composer tester
vendor/bin/tester tests/Parser.fluent.phpt -s
# Static analysis (PHPStan level 8)
composer phpstan- Every file starts with
declare(strict_types=1);; tabs; everything typed; Nette Coding Standard. - Constants are modern PascalCase (
Parser::Optional) with deprecated UPPERCASE aliases kept for BC. - Tests are Nette Tester
.phptundertests/(requirebootstrap.php); usetest()/Assert::same/Assert::exception, no comment beforetest().
- Help text is the schema.
addFromHelp()parses formatted help with two regexes: the option name is the last flag on a line, the alias the first; a<file>/[type]spec sets required/optional,...marks repeatable,<a|b|c>an enum. The$defaultsarray merges over the parsed result (it suppliesRealPath,Normalizer, etc.);RealPathdesugars into aNormalizer. parse()uses anOptionPresent = truesentinel. A bare--flagyields the literaltrue; a value is taken from the next token only if it doesn't start with-. So an optional-value option used bare parses astrue, not its fallback - the fallback applies only when the option is absent entirely. A missing required positional argument throws; a missing required option becomesnull.parseOnly()is deliberately dumb - it parses only the named options, never validates, never throws (so--help/--versionwork despite a missing required argument). Don't add validation to it.Console::detectColors()anddetectTerminal()are separate on purpose. Gate color ondetectColors(honorsNO_COLOR/FORCE_COLOR), but gate interactive-only features (progress bars, prompts) ondetectTerminal(pure TTY)- a user may disable color yet still be on a real terminal.
- User-facing how-to (fluent
addSwitch/addOption/addArgument, the help-text format, color codes) is manual material and lives in the public web docs, not here.