Skip to content

fix: report bad flags with the command's usage; add ruflet help <command> (CLI-12) - #78

Merged
AdamMusa merged 13 commits into
mainfrom
fix/cli-unknown-flags-and-help
Nov 20, 2026
Merged

AdamMusa merged 13 commits into
mainfrom
fix/cli-unknown-flags-and-help

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

WP-00 CLI-12, built on the command registry (#77, merged). Base is main; the branch merges main with merge commits, so the diff against main is this PR alone (8 lib files, 3 test files, the H1 corpus and its README, and 51 goldens).

What

Bad flags. A flag a command does not read prints ruflet <command>: <reason>, a blank line and that command's help on stderr, and exits 64 (EX_USAGE, what flutter uses). No backtrace. Each registry entry says who reports it (unknown_flags:):

Value Commands How
:reject version, create, new (and aliases), install, doctor Ruflet::CLI.run checks the arguments against the declared options, exactly, before the command runs, so nothing is fetched or written.
:command run, update, debug, emulators, help Each parses its own flags (help checks its one argument); the dispatcher reports the errors the same way.
:forward build, devices Unread flags still go to flutter build <target> / flutter devices.

Help. ruflet help <command> and <command> -h/--help print that command's help, rendered from the registry; nothing runs or is fetched. build apk --help and devices --help still reach Flutter. ruflet help, ruflet --help, help bogus and help -- print the command list as before.

Direct calls. ruflet_rails' rake tasks call command_build, command_update and command_install without Ruflet::CLI.run. Each handler that raises HelpRequested or UsageError (run, update, debug, emulators, build) rescues it itself with Commands.report_stop, so a direct call prints what ruflet <command> prints and returns 0 or 64. OptionParser's own errors still reach a direct caller, as on main.

Flag fixes.

  • ruflet build --release apk builds, as build apk --release does. When the first argument is a flag, the target is the one argument before any -- that names a target. Two such arguments (--flavor web apk) are refused with exit 64 rather than guessed.
  • emulators --start --emulator ID --cold runs flutter emulators --launch ID --cold.
  • D6: run -d stays --desktop (declared explicitly; --d/--de are hidden aliases). Device selection is the long --device ID only. run --device ID is reserved for CLI-01: it says it is not supported yet, points to Explorer and ruflet install --device ID, and exits 1. debug --device ID is declared beside --device-id, which -d and --dev still select. install --device=ID is read.
  • Kept as on main: emulators -c/--c reach --create; help -- lists the commands; build -- apk and build --self - apk are the unsupported targets -- and -; a help flag after -- reaches the command (run main.rb -- --help runs main.rb).

Review fixes in this round

  • rake "ruflet:update[--help]" and [-h] (also [--h], [--he]) print update's help and exit 0 again. The previous head raised HelpRequested (exit 1). Direct calls of run, debug and emulators with -h, and command_build(%w[--flavor web apk]), also stop with help or a usage error instead of raising.
  • build --self - apk is again "Unsupported build target: -" (exit 1): extract_build_target! uses Commands.flag?, so a bare - is an argument. The previous head built apk and passed - to Flutter.
  • The table below now covers new/create NAME -h|--help, new -x, the rake help forms and direct handler calls.
  • New unit tests: a help flag after -- reaches the command; a bare - is not a flag (help -, new my_app -); a prefix of a declared flag is unknown (install --dev X, install --verb, doctor --fi); and the direct calls above.

Behavior changes

All are error paths or input that was ignored (X1 exceptions, D6 bug fixes). Against main, 32 goldens change and 19 are added: 10 of the added goldens record new behavior and 9 match main. None of the 51 belongs to any of the 84 documented invocations in docs_coverage.rb.

Invocation Before After
run/update/debug/emulators with a flag their parser rejects (--bogus-flag, run --port, run --web=1) uncaught OptionParser error, exit 1 usage error, exit 64 (update and debug still fetch the template first)
run main.rb --device emulator-5554 (also --dev) uncaught InvalidOption, exit 1 "--device is not supported yet" and how to get the app onto a device, exit 1
emulators --cold (also --co) without --start uncaught InvalidOption, exit 1 "--cold needs --start --emulator ID", exit 64
emulators --start --emulator ID --cold uncaught InvalidOption, exit 1 flutter emulators --launch ID --cold
install --bogus-flag, doctor --bogus-flag (also prefixes such as install --dev ID, doctor --fi) flag ignored; the command ran usage error, exit 64, nothing fetched
new my_app --bogus-flag flag ignored; my_app created usage error, exit 64, nothing created
new -x (a flag where the name goes; also create) created a project named -x, exit 0 usage error, exit 64, nothing fetched or created
--version -v, -v --verbose, version --anything flag ignored; version printed, exit 0 usage error, exit 64 (version extra still prints the version)
install -d / install --device= with no value value ignored; installed with no device "missing argument", exit 64
install --device=emulator-5554 ignored: looked for outputs with no device (iOS on a Mac), "Could not find built app outputs", exit 1 installs on emulator-5554
build --release apk (any flag before the target) "Unsupported build target: --release", exit 1 builds, as build apk --release
build --flavor web apk (two target names after a flag) "Unsupported build target: --flavor", exit 1 "more than one argument names a build target (web, apk); put the target first", exit 64
build --release / build --release -- apk (no target before --), build --flavor prod (no target), rake ruflet:build[--help] "Unsupported build target: --release" / "--flavor" / "--help", exit 1 "Usage: …" / "Unsupported build target: prod", exit 1
run/update/debug/emulators --help, -h OptionParser's bare switch list, exit 0 (update and debug fetched the template) the command's help, exit 0, nothing fetched
rake ruflet:update[--help], [-h] OptionParser's bare switch list, exit 0 update's help, exit 0
build --help, -h "Unsupported build target: --help", exit 1 build's help, exit 0
new/create [NAME] -h/--help (new --help, new my_app -h, create my_app --help) fetched the template and created a project (--help, or my_app), exit 0 the command's help, exit 0, nothing fetched or created
install --help, doctor --help (and -h) flag ignored; the command ran the command's help, exit 0
version --help, -v -h printed the version version's help, exit 0
help <command word> (help build, --help build, help -v) the command list that command's help
help --bogus the command list usage error, exit 64
Ruby code calling a handler directly (no shipped caller passes these): command_run/command_debug/command_emulators with -h OptionParser's switch list, then exit (SystemExit 0; emulators looked up Flutter first) the command's help, returns 0
The same: command_build(%w[--flavor web apk]), command_emulators(%w[--cold]) returned 1 ("Unsupported build target: --flavor") / raised InvalidOption usage error, returns 64

Proof (head d8a08955, Ruby 4.0.5 unless noted)

  • H1: ruby test/compat/compat.rb --jobs 4: checked 309 goldens, exit 0. ruby -Ilib -Itest test/compat_goldens_test.rb: 317 runs, 702 assertions, 0 failures, 0 errors.
  • Test first. 64ea22d3 adds 9 H1 entries (rake ruflet:update[--help], [-h], rake ruflet:build[--help], new my_app -h, create my_app --help, new -x, help -, build --self - apk, run main.rb -- --help). Their goldens were generated on main a9b58473, and a full --regenerate there changed no other golden. Of its unit tests, 4 fail on the previous head's lib: the two direct-call tests, the new bare-- build test and the build-target table with its new - rows. The fix commit 97beb94c regenerates 6 of the 9 goldens, all listed above; help -, build --self - apk and run main.rb -- --help are unchanged from main.
  • Main's lib against this head's corpus and goldens: with main 43f75822's lib (and with a9b58473's), exactly 42 of the 309 fail: the 32 changed goldens and the 10 added ones that record new behavior. The other 9 added goldens pass on main.
  • 12 planted mutants (on 97beb94c) each fail cli_usage_test: the three review gaps, removing each handler's rescue, the two - checks in build, swapping report_stop's branches, and a handler that turns OptionParser errors into usage errors. Removing update's rescue also fails the two rake H1 goldens.
  • Every documented form is unchanged. compat.rb docs-check against ruflet_site main 7ca19e6: 87 documented, 84 covered. The 3 forms the coverage table does not list yet are ruflet run --web, build android --full and build ipa --full. Their goldens generated on main 43f75822 pass unchanged on this head (run through the harness, not committed).
  • packages/ruflet, one process per test file: 21 files, 378 runs, 0 failures, 0 errors. Ruby 3.4.5 (CI's): 21 files, 378 runs, 0 failures, 0 errors.
  • Ruby 3.2.9 (the gemspec allows >= 3.1): cli_usage_test, cli_commands_test, cli_dispatch_test, cli_command_test, extra_command_test, run_command_test and update_command_test pass, one run each.
  • Only packages/ruflet changes; ruflet_core, ruflet_server, ruflet_rails, the runtime, the template and the engine are untouched. Gem source grows by 15,001 bytes (packages/ruflet/lib 357,021 to 372,022).

Not in this PR

  • ruflet run --device ID running the app's own client on a device: CLI-01 (WP-06).
  • The debug row of ruflet help still reads [scriptname|path] and there is no "run ruflet help <command>" hint: both would change the documented ruflet help output. ruflet help debug shows the right synopsis.
  • build exits 1, not 64, when Flutter rejects a forwarded flag; ruflet cannot know Flutter's flags.
  • rake ruflet:update[--bogus-flag] still aborts with OptionParser's InvalidOption (exit 1), as on main (same golden on both, run through the harness, not committed): direct callers get OptionParser's errors unchanged.
  • Docs (ruflet_site cli_reference.md: ruflet help <command>, --cold, install --device=ID, exit 64) are a follow-up docs PR.

Adds test/cli_usage_test.rb, red on this commit: an unknown flag on every
command prints that command's usage and exits 64 before anything runs;
-h/--help after a command and `ruflet help <command>` print its help;
`build --release apk` finds the target; `emulators --cold` cold boots;
`run --device` is reserved while -d, --d and --de stay --desktop.

The tests that pass here pin what must not change: every spelling of
--desktop, debug's device spellings, and build/devices forwarding.

Adds three H1 invocations, generated on this code: `build apk --release`
for default_app and services_app, and services_app `build --release apk`
(today: Unsupported build target).
…command>`

CLI-12. Every command now reports a flag it does not read the same way:
`ruflet <command>: <reason>`, a blank line and that command's help, on
stderr, exit 64 (EX_USAGE, as flutter). No backtrace.

- Commands that read their flags by exact match (version, create, new,
  install, doctor) are checked by Ruflet::CLI.run against their declared
  options before anything is fetched or written.
- run, update, debug and emulators keep their OptionParser; its errors
  are rescued by the dispatcher and reported the same way.
- build and devices still pass the flags they do not read to Flutter.

-h/--help after a command, and `ruflet help <command>`, print that
command's usage, summary, options and help sections, rendered from the
registry. `ruflet help`, `ruflet --help` and `ruflet help <not a
command>` still print the command list unchanged. `build apk --help` and
`devices --help` still reach Flutter.

Flag fixes:
- `ruflet build --release apk` builds: when the first argument is a
  flag, the target is the one argument that names a target. Two such
  arguments after a flag are refused instead of guessed at.
- `emulators --start --emulator ID --cold` cold boots (flutter
  emulators --launch ID --cold); --cold without --start is a usage error
  instead of a crash. emulators parses before looking up Flutter.
- run: -d stays --desktop (D6), declared explicitly so the new
  `--device ID` does not make it ambiguous; --d and --de keep working.
  `run --device ID` says it is not supported yet instead of crashing.
- debug: `--device ID` is declared beside --device-id (it already
  reached it as an abbreviation).
- install: `--device=ID` is read; `-d` without a value is an error.

H1: 32 goldens change, all -h/--help, --bogus-flag, `help build`,
`--release apk`, `emulators --cold` or `run --device`; every documented
invocation is unchanged. The R2 dispatch pins that recorded the old
behavior are updated.
Conflict in cli/commands.rb's header comment only: main (#77) documents
the forms OptionParser completes beyond the declarations; this branch
documents that help and usage errors are rendered from them. Both kept.
…review)

Review findings on #78, written against the old behavior first:

- main's pin (#77) says `emulators -c` is --create and `debug -d` and
  `--dev` select --device-id. On this branch -c is ambiguous with --cold
  and the debug option's first long name is --device. The pin helper now
  builds its parser with the hidden switches too, as the switch-list
  check above it does.
- `help --` and `--help --` listed the commands (exit 0); `build -- apk`
  said "Unsupported build target: --". Unit tests and H1 entries for them.
- H1 entries for `emulators --c`, and for `--version -v`, `-v --verbose`
  and `version --anything`, whose change (exit 0 to 64) the next commit
  lists as a behavior change.

The 7 new goldens were generated on main c444cbd (where a full
--regenerate changed no other golden).
- `ruflet help --` and `--help --` list the commands again (exit 0):
  `--` ends the options, it is not a bad flag.
- `ruflet build -- apk` is again "Unsupported build target: --"; nothing
  after `--` is taken as the build target, so `build --release -- apk`
  does not build either.
- `emulators -c` and `--c` reach --create again: they are hidden
  switches of --create, as run's -d, --d and --de are of --desktop.
- debug declares --device-id first, so -d and --dev select it as main's
  pin says; its help reads "--device-id ID, --device ID".

Goldens regenerated for the listed behavior changes only: debug's help
label (3), `install --device=ID` now installing on ID (main's #77 golden
records it ignored), and the version words rejecting a flag (3).
…(CLI-12 review)

The nine new H1 goldens were generated on main a9b5847; a full
--regenerate there changed no other golden. The new handler tests fail on
the previous head: rake ruflet:update[--help] (command_update called
directly) raises HelpRequested, command_build(%w[--flavor web apk])
raises UsageError, and build treats a bare - as a flag.
… too

ruflet_rails' rake tasks call Ruflet::CLI.command_update and command_build
directly, so `rake "ruflet:update[--help]"` raised HelpRequested (exit 1)
where main printed update's switch list (exit 0). Each handler that raises
HelpRequested or UsageError now rescues it with Commands.report_stop, which
prints what `ruflet <command>` prints and returns 0 or 64. OptionParser's
own errors still reach a direct caller, as on main.

extract_build_target! treats a bare `-` as an argument (Commands.flag?), so
`build --self - apk` is again "Unsupported build target: -".

Regenerated 6 of the goldens added in the previous commit: the rake help
forms, `new my_app -h`, `create my_app --help` and `new -x`.
@AdamMusa
AdamMusa merged commit 27e2ffa into main Nov 20, 2026
12 checks passed
AdamMusa added a commit that referenced this pull request Nov 21, 2026
Port the build fixes onto the cli/build/* split (#75) and keep #77/#78's
command registry: usage errors stay main's (exit 64), and the dispatcher,
help heredoc and new-command checks this branch had are dropped in favour
of main's. appbundle now gets aab's identity check, package rename and
stale-output clearing without being renamed to aab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant