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

question: how to find what flags is been called in the command #434

Closed
zer09 opened this issue May 4, 2017 · 6 comments
Closed

question: how to find what flags is been called in the command #434

zer09 opened this issue May 4, 2017 · 6 comments
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc.

Comments

@zer09
Copy link

zer09 commented May 4, 2017

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. calling Flag.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.

@n10v
Copy link
Collaborator

n10v commented May 4, 2017

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")
}

@n10v n10v added area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc. labels May 4, 2017
@n10v
Copy link
Collaborator

n10v commented May 4, 2017

You can also iterate through all activated flags by versionCmd.Flags().Visit(...). More here

@zer09
Copy link
Author

zer09 commented May 4, 2017

@BoGeM thank you so much, I got it working. I like the FlagSet.Visit, it was clean.

@zer09 zer09 closed this as completed May 4, 2017
@masterxavierfox
Copy link

@zer09 Do you mind elaborating your solution with an example thanks.

@zer09
Copy link
Author

zer09 commented Jun 9, 2018

@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

func checkFlags(f *Flag){
  if(f.Name == "something"){
    // do Something
  }
}

func main(){
  flagset.Visit(checkFlags);
}

@dmjones
Copy link

dmjones commented Jul 11, 2018

See the solution at #453 (comment). Using Changed will indicate if the user has set the value (even if to the same value as the default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

4 participants