Repository navigation
mport: restart getopt before every subcommand parses its options - #207
Conversation
Reviewer's GuideFix subcommands that incorrectly display usage or miss arguments after a global option by restarting getopt consistently before every command-level parse, and add CLI regressions covering version and info. Sequence diagram for resetting getopt before subcommand parsingsequenceDiagram
participant User
participant main
participant reset_getopt
participant getopt
participant Handler
User->>main: mport -U info package
main->>getopt: getopt(global options)
main->>Handler: dispatch subcommand
Handler->>reset_getopt: reset_getopt()
reset_getopt->>reset_getopt: optreset = 1 on MidnightBSD
reset_getopt->>reset_getopt: optind = 1
Handler->>getopt: getopt(command options)
getopt-->>Handler: package arguments
Handler-->>User: command result instead of usage
Flow diagram for global and subcommand option parsingflowchart LR
A[Global options parsed] --> B[Subcommand selected]
B --> C["reset_getopt()"]
C --> D["getopt(command options)"]
D --> E[Handler receives package arguments]
E --> F[Print command result]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Claude finished @laffer1's task in 16s —— View job Review of PR #207No blocking issues found.
I did not build or run the tests in this session. This review is from reading the diff and tracing the call sites. |
Each subcommand parses its own options with getopt(3) over the argument vector left after the global options. getopt keeps its position in optind, and on BSD internal state that only optreset clears, so it has to be restarted first. add, install, delete, audit, verify, query and which did that inline; annotate, info, version and which did not. After a global option such as -U, optind already pointed past the subcommand's arguments and the parse came up empty: `mport -U info bind920` printed the usage text instead of the package, and `mport -U version -t 1 1` printed nothing at all. Without a global option optind happened to be 1 and the same handlers worked by accident. Add reset_getopt() and call it in every handler that runs getopt, replacing the six inline copies. verify had set optind without optreset and uses the helper too. Tests: mport_cli_test gains a registry-free case running `version -t` behind -U and -q, which produced no output before, and a registry-gated case running `-U info` on an unknown package and checking that the usage text is not what comes back. AI-Assisted-by: Claude Fable 5.1 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Lucas Holt <luke@foolishgames.com>
8e01bc4 to
8faaa3b
Compare
|
Claude finished @laffer1's task in 23s —— View job Review of PR #207: restart getopt before every subcommand parseVerdict: no blocking issues found.
FindingsThe fix is correct. After the global
Minor notes (non-blocking)
Per |
Problem
mport -U info bind920printed the usage text instead of the package, andmport -U version -t 1 1printed nothing at all. Any global option beforeinfo,annotate,versionorwhichmisbehaves this way, including-q,-Vand the new--allow-old-release.Each subcommand parses its own options with
getopt(3)over the argument vector left after the global options.getoptkeeps its position inoptind(and on BSD, internal state that onlyoptresetclears), so it has to be restarted first.add,install,delete,audit,verifyandquerydid that inline;annotate,info,versionandwhichdid not. After a global option,optindalready pointed past the subcommand's arguments and the parse came up empty, so the handler saw no arguments. Without a global optionoptindhappened to be 1 and those handlers worked by accident.Change
A
reset_getopt()helper (optreset = 1under the__MidnightBSD__guard,optind = 1) called at the top of every handler that runsgetopt, replacing the six inline copies.verifyhad setoptindwithoutoptresetand now uses the helper too.The
if (local_argc > 1)guards around the per-command parses are left as they are; removing them is not needed for the fix.Tests
mport_cli_testgains:global_option_before_subcommand(no registry needed):mport -U version -t 1 1andmport -q -U version -t 2.0 2.0must print=. The installed 2.8.3 binary prints nothing for these today.global_option_before_info(registry required, skipped otherwise):mport -U info no-such-package-zzzmust not come back with the usage text.🤖 Generated with Claude Code
Summary by Sourcery
Reset getopt state before each subcommand parses options so global and subcommand options work together reliably.
Bug Fixes:
Enhancements:
Tests: