0

I'm moving my cobra command flags inside a function so I can use it in other commands. I can able to see the commands but when I type the flage it always returns false.

Following is my code:

func NewCommand(ctx context.Context) *cobra.Command {
    var opts ListOptions

    cmd := &cobra.Command{
        Use:   "list",
        Short: "List",
        RunE: func(cmd *cobra.Command, args []string) error {
            fmt.Println(args) // []
            opts.refs = args
            return List(ctx, gh, opts, os.Stdout)
        },
    }

    cmd = GetCommandFlags(cmd, opts)
    return cmd
}

// GetListCommandFlags for list
func GetCommandFlags(cmd *cobra.Command, opts ListOptions) *cobra.Command {
    flags := cmd.Flags()
    flags.BoolVar(&opts.IgnoreLatest, "ignore-latest", false, "Do not display latest")
    flags.BoolVar(&opts.IgnoreOld, "ignore-old", false, "Do not display old data")
    return cmd
}

So when I type the following command

data-check list --ignore-latest

The the flag value of --ignore-latest should be true but I get false as a value in RunE args. Am I missing something here?

GetCommandFlags is something I want to use it in other commands I don't want to repeat the same flags.

DonOfDen
  • 3,968
  • 11
  • 62
  • 112

3 Answers3

2

You are passing opts to GetCommandFlags by value. You should pass a pointer to it, so the addresses registered for the flags use the opts declared in the calling function.

func GetCommandFlags(cmd *cobra.Command, opts *ListOptions) *cobra.Command {
  ...
}
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • Sorry I tried it already and it's not working I get the `args` as empty still. – DonOfDen Oct 24 '19 at 14:31
  • 1
    You will not get ignore-latest as an argument. It is registered as a flag, so you will get it from the `opts.IgnoreLatest`. Arguments are not flags, you use them for things like `data-list list 10`, where `list` is a command and `10` is an argument to that command. You have to declare that the command expects 1 argument, and after that, your `args` will contain one element in it, which is "10". – Burak Serdar Oct 24 '19 at 14:36
  • But what if I decided to not use any flags which In command. I cant use `Expects 1 argument` here :( – DonOfDen Oct 24 '19 at 14:38
  • I do not understand this statement. In your example, `--ignore-latest` is a flag, not an argument to a command. – Burak Serdar Oct 24 '19 at 14:43
2

You should use func GetCommandFlags(cmd *cobra.Command, opts *ListOptions) and call the func like cmd = GetCommandFlags(cmd, &opts).

You can print opts.IgnoreLatest and opts.IgnoreOld to see the changed value.

Works fine for me. Hope it will work for you too.

func NewCommand(ctx context.Context) *cobra.Command {
    var opts ListOptions

    cmd := &cobra.Command{
        Use:   "list",
        Short: "List",
        RunE: func(cmd *cobra.Command, args []string) error {
            // fmt.Println(args) // []
            fmt.Println(opts.IgnoreLatest, ", ", opts.IgnoreOld)
            opts.refs = args
            return List(ctx, gh, opts, os.Stdout)
        },
    }

    cmd = GetCommandFlags(cmd, &opts)
    return cmd
}

// GetListCommandFlags for list
func GetCommandFlags(cmd *cobra.Command, opts *ListOptions) *cobra.Command {
    flags := cmd.Flags()
    flags.BoolVar(&opts.IgnoreLatest, "ignore-latest", false, "Do not display latest")
    flags.BoolVar(&opts.IgnoreOld, "ignore-old", false, "Do not display old data")
    return cmd
}
Masudur Rahman
  • 1,585
  • 8
  • 16
  • Yup!!!! I tried them before `*ListOptions` `&opts` but I tried printing the `args` too bad of me... It works perfectly... Thanks for ur help @Masudur – DonOfDen Oct 24 '19 at 14:49
0

You are passing value parameter not a pointer parameter.

Try someting like:

cmd = GetCommandFlags(cmd, &opts, "")
heat
  • 1,295
  • 9
  • 20