Repository navigation
refactor: dispatch ruflet commands through a command registry (WP-00 R2) - #77
Merged
Merged
Conversation
Before the case/when in Ruflet::CLI.run becomes a command table, pin what it does: every command word and alias reaches the same command method with the caller's remaining argv, words match in any case, the command's result is the exit code, version and help ignore further arguments, an unknown word is reported downcased with the help and exit 1, only create/new/bootstrap/init/update/debug/build/install/doctor fetch the first-run assets (and a failed fetch never stops them), and the help text is byte-for-byte the current one. The commands are recorders on a private copy of the CLI, so the test runs nothing and does not touch Ruflet::CLI. It passes on the current code.
39 new H1 invocations pin the command words and help surface the command registry (R2) and CLI-12 touch: - mixed-case words (VERSION, -V, --Version, HELP, -H, Bogus-Command, CREATE, New my_app), the empty word, and version/help with extra arguments; - create, bootstrap and init without an app name (usage, exit 1, and the first-run asset fetch the aliases trigger); - --help and -h after every command, recording today's behavior as is: OptionParser's own summary for run/update/debug/emulators, a project named --help for new/create, "Unsupported build target" for build, flutter devices --help, and doctor/install ignoring the flag. Mixed-case invocations get explicit ids so their goldens cannot collide with the lowercase ones on a case-insensitive filesystem. The child now sets $0 to the installed executable, as running `ruflet` does, so OptionParser's usage line says "ruflet" instead of the harness script; no existing golden changes.
Ruflet::CLI::Commands is to be the table `ruflet` dispatches through and `ruflet help` renders. The test pins the built-in table against the dispatch pins (help order, every word and the method it runs, which commands fetch first-run assets, the rendered help, that only build and devices forward their remaining arguments), and the registration API later commands use: before:/after: positions, idempotent re-registration, and errors for taken names or aliases, repeated or non-lowercase words and bad positions. Each command's declared options must be exactly what it reads today: for run, update, debug and emulators the OptionParser the command builds is captured and its summary compared with one built from the declarations; for build, install and doctor the flags they delete or extract from argv are recorded. devices must forward every argument.
The case/when in Ruflet::CLI.run and the hand-written help heredoc are replaced by Ruflet::CLI::Commands (cli/commands.rb), an ordered table in which each command declares its name and aliases, the CLI method that runs it, its usage line, the options it reads, the tool its remaining arguments are forwarded to, its help sections and whether it fetches the first-run assets. - run downcases the first word and looks it up, as before; an unknown word still warns, prints help and returns 1. - print_help renders the table; the "Build profiles" section is the build command's help. - ensure_first_run_assets asks the table instead of a word list. - version and help become the private command_version/command_help. cli/commands.rb loads before the command files, so a later command (WP-07's logs, clean, ...) registers itself beside its code with before:/after: instead of editing a shared case statement. The declared options are not used for parsing yet; they are what CLI-12's per-command usage and unknown-flag errors are to be built from. No output changes: the dispatch pins, the rendered help and all 199 H1 goldens are identical.
This was referenced Nov 8, 2026
Merged
This was referenced Nov 17, 2026
run, update, debug and emulators parse with OptionParser, which also accepts a single letter that starts exactly one long option and any unique prefix of one. None of these forms is declared in the command table, and only `run -d` had a golden. 17 new H1 invocations record them as they behave today: run -p 9000, -n, -w, --desk and -v; update -c, all -c -p linux, all -f and -v; debug -d chrome, --dev chrome, -p macos, web -r and web -w canvaskit; emulators -c, -d and -s -e ID. `run -v` and `update -v` reach OptionParser's own --version and exit 1 with "ruflet: version unknown". The goldens were generated on main's code (dd924eb lib, this branch's corpus). Regenerating all 290 goldens there added these 17 and changed no existing golden.
The command table's options are the switches a command defines (or, for build, install and doctor, the tokens they look for), not every form it reads. run, update, debug and emulators parse with OptionParser, so they also take a single letter that starts exactly one long option and any unique prefix of one: `run -d` is --desktop (D6 keeps that meaning) and `debug -d ID` is --device-id ID. commands.rb now says so, and that a parser built from the declarations must keep accepting those forms. A new test pins every letter a-z each of the four commands takes and the option it selects, a few long prefixes, and that none of their parsers sets require_exact. Adding a --device switch to run (which makes -d ambiguous) or setting require_exact on run's parser fails it. Comment and test only; no code path changes.
AdamMusa
added a commit
that referenced
this pull request
Nov 20, 2026
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.
AdamMusa
added a commit
that referenced
this pull request
Nov 20, 2026
…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).
AdamMusa
added a commit
that referenced
this pull request
Nov 20, 2026
- `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).
This was referenced Nov 20, 2026
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.
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.
WP-00 R2. Based on
main(dd924eb3, which has #73, #74, #75 and #70), merged in with a merge commit.What
Ruflet::CLI.runpicked a command with a 30-linecase/when, andprint_helpprinted a hand-written heredoc. Both now come from one table,Ruflet::CLI::Commandsincli/commands.rb. Each command in it declares:build)name,aliasesbuild(newhasbootstrap,init)handlerRuflet::CLImethod that runs it, given the arguments after the word:command_buildusageruflet help(nilmeans not listed)ruflet build <apk|...> [--lite|--full|--self] [--verbose]optionsOptionParser#onstrings), or the tokens a hand-parsing command looks for--self,--lite,--full,-v/--verboseforwardsflutter build <target>helpruflet helpprints after the command listfirst_run_assetstruecli.rbkeeps only the dispatch:rundowncases the first word and looks it up, as before. An unknown word still warns, prints the help and returns 1.print_helprenders the table.ensure_first_run_assetsasks the table instead of a word list.versionandhelpbecome two private handlers,command_versionandcommand_help.extendorder (NewCommand,RunCommand,UpdateCommand,BuildCommand,ExtraCommand) is unchanged. test: CLI and page compatibility goldens (WP-00 H1) #73's notes say R2 must keep it.Registration.
cli/commands.rbloads before the command files, so a later command can register itself beside its own code. It passesbefore:orafter:when the end of the list is not the right place, as R1's build contributors do:ArgumentError: a name or alias that is already taken, a word repeated within one command, a word that is not lowercase or has a space, passing bothbefore:andafter:, and an unknown position.Options are declared, not used yet. Each command still parses its own arguments. The declarations do not cover every form the commands accept.
commands.rband the tests record the forms they leave out, which CLI-12 must keep accepting:run,update,debugandemulatorsparse with OptionParser. It also accepts any unique prefix of a long option, and a single letter that starts exactly one long option. For example,run -dis--desktop(D6 keeps that meaning),debug -d IDis--device-id ID,run -p 9000is--port 9000,update -cis--checkandemulators -s -e IDis--start --emulator ID. The commands read these forms, but the table does not declare them. Built from the declared switches, OptionParser accepts the same forms unlessrequire_exactis set.build,installanddoctorlook only for the exact tokens they declare. They accept no prefixes and no--flag=value:install --device=IDis not read as a device.Why
WP-06 and WP-07 add commands and flags (
logs,clean,flutter,screenshot,--device, ...), and CLI-12 changes how every command reports a bad flag. With acasestatement and a heredoc, each of those edits the same lines incli.rb. After this PR, a new command is one registration, and its help line, usage and flag list come from the same place.Proof of zero change
The only files outside
packages/ruflet/test/that differ frommainarelib/ruflet/cli.rbandlib/ruflet/cli/commands.rb.version extra,help bogus,create/bootstrap/initwith no name, and--help/-hafter all 12 commands. The other 17 pin OptionParser's short and prefix forms,run -vandupdate -vamong them.git diff origin/mainshows no change under anypackages/*/libthere. A fullcompat.rb --regeneratein that tree changed none of the existing goldens. It added the new ones;update -vwas added later with--only.compat.rbthen checked all 290 goldens there: exit 0, 0 changed.compat.rbchecks all 290 goldens: exit 0, 0 changed.test/cli_dispatch_test.rbis its own first commit (52b96c99). Run against main's library code, it passes all 9 tests. It checks:versionandhelpignore further arguments;Tests
All runs are on this head,
055b27ce.test/cli_commands_test.rb(20 tests, plus the 9 dispatch tests it loads) checks the following.first_run_assets, the rendered help,forwards, that handlers areRuflet::CLImethods taking argv, and that the table is frozen.run,update,debugandemulators, the test captures the OptionParser the command builds and compares its summary with one built from the declarations.build,installanddoctor, it records the tokens the command deletes from or extracts out of argv.devicesforwards every argument.-ato-zthe command takes and the option that letter selects, plus a few long prefixes. It also checks that none of their parsers setsrequire_exact.require_exactonrun's parser fails the test. So does adding a declared--deviceswitch torun, which makes-dambiguous.packages/ruflet, with eachtest/*_test.rbexceptcompat_goldens_test.rbrun in its own process: 19 files, 308 runs, 0 failures, 0 errors and 0 skips. The result is the same on Ruby 4.0.5 and on 3.4.5 (CI's.ruby-version, minitest 5.25.5).ruby -Ilib -Itest test/compat_goldens_test.rb: 298 runs, 0 failures, 0 errors, 0 skips.ruby test/compat/compat.rb --jobs 4: checked 290 goldens, exit 0, with no CHANGED, ERROR, MISSING or STALE lines.--only, each made on a scratch copy and then reverted:ruflet helprow (thedevicesusage):--only empty/helpchanges all 5 of its goldens.require_exactonrun's parser:--only run-main.rb--changes 6 of its 16 goldens, exactly-d,-p 9000,-n,-w,--deskand-v.require_exactondebug's parser:--only debug--changes 4 of 8, namely-h,-d chrome,--dev chromeand-p macos.initalias changesinit.txtandinit-my_app.txt.Behavior changes
None.
Today's behavior, now pinned (left for CLI-12)
The goldens record these as they are. Changing any of them is CLI-12's job, and it must list the change as a behavior change:
ruflet new --helpandnew -hcreate a project named--help/-h.run,update,debugandemulatorswith--help/-hprint OptionParser's bare switch list (Usage: ruflet [options]) and exit 0.run -vandupdate -vreach OptionParser's own--version: they print "ruflet: version unknown" and exit 1.update -vfetches the template first.run -dis protected by D6, but CLI-12 must not drop any of the others silently.build --helpexits 1 with "Unsupported build target: --help".devices --helprunsflutter devices --help.installanddoctorignore--help.help buildprints the general help, although the gem README says "Runruflet help <command>for all options".debugusage line says[scriptname|path], but its first argument is the platform.buildwith no target prints its own usage line. Its target order (apk|android|aab|ios|ipa|...) differs from the help line's (apk|android|ios|ipa|aab|...).Not in this PR
OptionParserblocks are untouched.extra_command.rbis not split into per-command files. That is for the lead to decide before WP-06 and WP-07.cli/commands.rbadds 11.6 KB, andcli.rbshrinks by 1.1 KB.