Fail on unknown or malformed CLI options - #2
Merged
Conversation
InputParser silently ignored anything it did not recognise, so a typo (--output-htlm=report.html) or an option written with a space on an older release skipped the report with exit code 0, and a value passed as the next argument was swallowed as the environment name. Options are now declared in FLAGS / VALUE_OPTIONS tables and both --opt=value and --opt value are accepted. Unknown options (with a levenshtein "did you mean" hint), missing or empty values, non-numeric numbers, malformed --var and stray positional arguments are collected in ParsedInput::$errors; RunCommand prints them with the help text and returns ExitCode::UsageError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
InputParsersilently ignored anything it did not recognise. Three consequences,all of them "the report just isn't there and nothing tells you why":
--output-htlm=report.html(typo)0, no reportError: Unknown option: --output-htlm — did you mean --output-html?, exit3--output-html report.html(space)=-joined or the next argument--output-html(no value)0Error: Option --output-html requires a value, exit3http-smoke dev report.htmlError: Unexpected argument "report.html" — environment is already set to "dev"--concurrency=many0Error: Option --concurrency expects a whole number, got "many"--var=TOKENError: Option --var expects KEY=VALUE, got "TOKEN"Options are now declared in
FLAGS/VALUE_OPTIONStables instead of a chain ofstr_starts_with()calls with hand-countedsubstr()offsets. Problems arecollected in
ParsedInput::$errors;RunCommandprints them with the help textand returns
ExitCode::UsageError(3), so CI fails loudly instead of quietlyproducing no artefact.
No new dependency:
symfony/consolewould give this for free, but the packagekeeps
psr/containeras its only runtime dependency so it can drop into anyproject as a
--devtool without console-framework version constraints. Thattrade-off is now written down in
docs/claude/conventions.md, together with therule that pays for it — the parser must never silently no-op.
Also documented in the README CLI reference (
=vs space, hard-error behaviour).Tests:
tests/Unit/Console/InputParserTest.php(new, covers every option pluseach failure mode above). PHPStan max 0 errors, PHP-CS-Fixer clean, PHPUnit
117 tests / 342 assertions.