Skip to content

fix: propagate leading --help to subcommands with a local help flag - #7482

Merged
thaJeztah merged 1 commit into
docker:masterfrom
nicksieger:nicksieger/fix-help-run
Nov 5, 2026
Merged

thaJeztah merged 1 commit into
docker:masterfrom
nicksieger:nicksieger/fix-help-run

Conversation

@nicksieger

Copy link
Copy Markdown
Member

Summary

  • docker --help run <image> (and create) runs the container instead of printing usage, because a leading --help is parsed and consumed as the root's persistent help flag before the subcommand sees its args
  • This works fine for most subcommands, which share the same help flag object with the root, but run/create each register their own local help flag (without -h, to avoid a shorthand clash with --hostname), which shadows the root's and never observes the value
  • HandleGlobalFlags now propagates the parsed --help value into a subcommand's local help flag when one shadows the root's, so docker --help run <image> behaves like docker run --help

Fixes #7473

Test plan

  • Added TestHandleGlobalFlagsHelpPropagation in cli/cobra_test.go
  • Manually verified against a built binary: --help run <image>, --help create <image>, and --cloud --help run <image> now print help without pulling/running anything
  • Manually verified run --help, --help ps, bare --help, and plain run <image> are unaffected

- --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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. The Lookup guard 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 --help propagation (confidence: unverified low)
    When cmd.Find(args) returns a non-nil error the propagation is silently skipped. This is unreachable for run/create in practice, but a future subcommand with a local help shadow flag and no Args validator 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 -h shorthand path (confidence: unverified low)
    The test exercises --help run hello-world but 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's PersistentFlags.

@vvoland vvoland added this to the 29.10.0 milestone Nov 5, 2026
@thaJeztah
thaJeztah merged commit f9ad558 into docker:master Nov 5, 2026
101 of 102 checks passed
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.

docker --help run <image> runs the container instead of printing help

5 participants