Skip to content

cliflagv2: read each flag from the command that defines it - #453

Merged
knadh merged 3 commits into
knadh:masterfrom
abo3losh1:fix/cliflagv2-parent-flags
Oct 7, 2026
Merged

knadh merged 3 commits into
knadh:masterfrom
abo3losh1:fix/cliflagv2-parent-flags

Conversation

@abo3losh1

Copy link
Copy Markdown
Contributor

Follow-up to #451, same problem in the v2 provider.

processFlags reads every flag through p.ctx, the innermost command's context, and findFlag only looks at the app's flags and the innermost command's flags. With app > mid > leaf:

app := &cli.App{
	Name:  "app",
	Flags: []cli.Flag{&cli.StringFlag{Name: "name"}},
	Commands: []*cli.Command{{
		Name:  "mid",
		Flags: []cli.Flag{&cli.StringFlag{Name: "region"}, &cli.IntFlag{Name: "port"}},
		Subcommands: []*cli.Command{{
			Name:   "leaf",
			Flags:  []cli.Flag{&cli.StringFlag{Name: "name"}},
			Action: func(ctx *cli.Context) error { /* Provider(ctx, ".").Read() */ },
		}},
	}},
}
// app --name root mid --region eu --port 80 leaf --name child

master gives:

map[app:map[mid:map[leaf:map[name:child]] name:child]]
  • mid.region and mid.port are missing, because findFlag never looks at mid.
  • app.name is child, because p.ctx.String("name") finds the leaf's flag first.
  • With only leaf --name child, app.name is still set to child.

Read() already walks the lineage, and each entry is that command's own *cli.Context. The fix passes that context to processFlags and uses the flag we already hold for the type switch, so findFlag is gone. Same idea as #451.

Added TestCliFlagParentFlagsInSubcommand with the three cases above. All three fail on master. go test and go vet pass in providers/cliflagv2.

Flags were read through the innermost command's context. findFlag only
looked at the app and the innermost command, so flags on a middle command
were dropped, and a parent flag that shares a name with a child flag got
the child's value. Read each flag through its own command's context in
the lineage instead.
Copilot AI balanced review requested due to automatic review settings October 6, 2026 21:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@knadh
knadh merged commit 9232c82 into knadh:master Oct 7, 2026
4 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.

3 participants