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

How to check if any flag was set ? #920

Closed
mydockergit opened this issue Jul 27, 2019 · 2 comments
Closed

How to check if any flag was set ? #920

mydockergit opened this issue Jul 27, 2019 · 2 comments
Labels
area/flags-args Changes to functionality around command line flags and args kind/support Questions, supporting users, etc.

Comments

@mydockergit
Copy link

I have CLI program I wrote in Go.
I have something like 20 flags.
Is there a way to check if non of the flags were assigned?
Something similar to len(args) < 0 ?

I saw that there is the Changed function:
flagset := cmd.Flags()
flagset.Changed()

But I need to provide the name of the flag and I don't want to specify all the 20 flags.
I saw that there is orderedActual that has the flags that were set so if I could just run len(orderedActual) it could be great, but I can't because it is private variable.

Capture

@umarcor
Copy link
Contributor

umarcor commented Jul 30, 2019

@mydockergit, I think that spf13/pflag or spf13/viper (spf13/viper#276, spf13/viper#657) would be the appropriate places to create an issue, because this functionality is not implemented in cobra.

@jharshman jharshman added area/flags-args Changes to functionality around command line flags and args kind/support Questions, supporting users, etc. labels Oct 3, 2019
@jharshman
Copy link
Collaborator

I think your answer can be found in the pflag repo. Take a look at the GoDocs.
https://godoc.org/github.com/spf13/pflag#NFlag

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/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

3 participants