-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
question: how to find what flags is been called in the command #434
Comments
Like this? package main
import "github.com/spf13/cobra"
var number, buildDate bool // used for flags
var versionCmd = &cobra.Command{Use: "version",
Run: func(cmd *cobra.Command, args []string) {
if number && buildDate {
// print number and build date
} else if number {
// print only number
} else if buildDate {
// print only builddate
}
}}
func init() {
versionCmd.Flags().BoolVar(&number, "number", false, "print number")
versionCmd.Flags().BoolVar(&buildDate, "buildDate", false, "print build date")
} |
You can also iterate through all activated flags by |
@BoGeM thank you so much, I got it working. I like the |
@zer09 Do you mind elaborating your solution with an example thanks. |
@masterxavierfox I forgot about this, but it will just iterate all the supplied flags, then you can check the flag information https://godoc.org/github.com/spf13/pflag#Flag. Something like this
|
See the solution at #453 (comment). Using |
hello good day,
still learning cobra, is there a way to find out what flags is called when a command is executed?
I tried to use the
Flag.Changed
field to check if the default value is changed. but what if bool type of flags and have a default value of true. callingFlag.Value
even the flag is not set it will return true.Example I want to use it like this.
on a version command it has 2 flags 'number' and 'builddate'.
I would like to use it like this
app version --number
then only the version number will print.
app version --builddate
then only the build date will only print.
calling both of them will show both values.
thanks.
The text was updated successfully, but these errors were encountered: