Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] LocalFlags().Visit / .NFlag failed, cannot count local flags have been set #1315

Open
Roytangrb opened this issue Jan 13, 2021 · 4 comments
Labels
area/flags-args Changes to functionality around command line flags and args kind/bug A bug in cobra; unintended behavior

Comments

@Roytangrb
Copy link

Describe the bug
I wanted to count the number of local flags that have been set explicitly, ignoring persisted global flags.

Yet the API LocalFlags().Visit, LocalFlags().NFlag, LocalNonPersistentFlags().Visit and LocalNonPersistentFlags().NFlag do not work

To Reproduce
While it seems intuitive to use the following API:

Run: func(cmd *cobra.Command, args []string) {
	flagCount := cmd.LocalFlags().NFlag() // always return 0
}

cmd.LocalNonPersistentFlags().NFlag() relies on LocalFlags(), it's having the same behaviour, always return 0

I also tried:

flagCount := 0
cmd.LocalFlags().Visit(func(f *pflag.Flag) {
	flagCount++
}
// flagCount outputs 0

The only workaround I came up with is:

flagCount := 0
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
	if f.Changed {
		flagCount++
	}
})
// correct count of flags set

Expected behavior
LocalFlags().Visit() should visit the actual FlagSet whose value has been set explicitly
LocalFlags().NFlag() should return the count of local flags that have been se explicitly

Example output

cmd.LocalNonPersistentFlags().NFlag() 0
cmd.LocalFlags().NFlag() 0
// flag.Name(), flag.Change
body , changed:  false
breaking , changed:  false
description , changed:  true
footers , changed:  false
help , changed:  false
scope , changed:  true
type , changed:  true
FlagCount:  3

Additional context
I am not sure *pflag.FlagSet.actual field is supposed to present while LocalFlags() and LocalNonPersistentFlags are computed. It will be better if the NFlag and Visit API be consistent.

Roytangrb added a commit to Roytangrb/gitwok that referenced this issue Jan 13, 2021
temp fix using LocalFlags.VisitAll API, submitted issue to cobra at
spf13/cobra#1315
@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@Virtual-felix
Copy link

Any news ? I'm having the same issue, NFlag on local flags always return 0

@johnSchnake
Copy link
Collaborator

I agree that something wonky is happening here. Needs to be resolved.

@johnSchnake johnSchnake added kind/bug A bug in cobra; unintended behavior area/flags-args Changes to functionality around command line flags and args and removed kind/stale labels Mar 18, 2022
@theclapp
Copy link

This is still a problem. As near as I can tell, cobra.Command.LocalFlags() tries to add all Flags() and PersistentFlags() to the local FlagSet, lflags. To do this it calls lflags.AddFlag(). But AddFlag doesn't touch lflags.actual (or any other *Actual fields), which is what Visit looks at. And indeed the only workaround I found was to use VisitAll and check flag.Changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants