Repository navigation
fix: propagate leading --help to subcommands with a local help flag - #7482
Conversation
- --help before a subcommand is parsed as the root's persistent flag and stripped from args, which works for most subcommands since they share the same flag object with root - run/create register their own local help flag (no -h, to avoid a shorthand clash with --hostname), which shadows root's and never saw the value, so the container ran instead of printing usage - propagate the parsed value into a subcommand's local help flag when one shadows the root's, in HandleGlobalFlags Fixes docker#7473 Signed-off-by: Nick Sieger <nick@nicksieger.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The propagation logic in HandleGlobalFlags is conceptually correct and addresses the targeted bug. The cmd.Find(args) / Lookup("help") / Set("help", "true") chain is sound for the run/create subcommands. No high- or medium-severity issues were found.
Lower-confidence findings (not posted inline)
-
[low] cli/cobra.go:182 — Error from
sub.Flags().Set()is silently discarded (confidence: unverified low)
The_ = sub.Flags().Set("help", "true")call discards any error. TheLookupguard makes a failure extremely unlikely, but a debug log or comment noting why the error is intentionally ignored would improve maintainability. -
[low] cli/cobra.go:180 —
cmd.Find()error path silently skips--helppropagation (confidence: unverified low)
Whencmd.Find(args)returns a non-nil error the propagation is silently skipped. This is unreachable forrun/createin practice, but a future subcommand with a localhelpshadow flag and noArgsvalidator while also having subcommands could silently fail to propagate--help. A code comment noting this limitation would help future maintainers. -
[low] cli/cobra_test.go:128 — Test does not cover
-hshorthand path (confidence: unverified low)
The test exercises--help run hello-worldbut not-h run hello-world. Adding a parallel sub-test for the shorthand would catch any future regression that accidentally drops the shorthand binding from the root'sPersistentFlags.
Summary
docker --help run <image>(andcreate) runs the container instead of printing usage, because a leading--helpis parsed and consumed as the root's persistent help flag before the subcommand sees its argshelpflag object with the root, butrun/createeach register their own localhelpflag (without-h, to avoid a shorthand clash with--hostname), which shadows the root's and never observes the valueHandleGlobalFlagsnow propagates the parsed--helpvalue into a subcommand's localhelpflag when one shadows the root's, sodocker --help run <image>behaves likedocker run --helpFixes #7473
Test plan
TestHandleGlobalFlagsHelpPropagationincli/cobra_test.go--help run <image>,--help create <image>, and--cloud --help run <image>now print help without pulling/running anythingrun --help,--help ps, bare--help, and plainrun <image>are unaffected