[Bug] LocalFlags().Visit / .NFlag failed, cannot count local flags have been set #1315
Labels
area/flags-args
Changes to functionality around command line flags and args
kind/bug
A bug in cobra; unintended behavior
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
andLocalNonPersistentFlags().NFlag
do not workTo Reproduce
While it seems intuitive to use the following API:
cmd.LocalNonPersistentFlags().NFlag()
relies on LocalFlags(), it's having the same behaviour, always return 0I also tried:
The only workaround I came up with is:
Expected behavior
LocalFlags().Visit()
should visit the actual FlagSet whose value has been set explicitlyLocalFlags().NFlag()
should return the count of local flags that have been se explicitlyExample output
Additional context
I am not sure
*pflag.FlagSet.actual
field is supposed to present whileLocalFlags()
andLocalNonPersistentFlags
are computed. It will be better if theNFlag
andVisit
API be consistent.The text was updated successfully, but these errors were encountered: