1

I am using https://github.com/spf13/cobra to work with flags.

I want my CLI to have flag with two names: -t or --token.

I currently used it like that:

rootCmd.PersistentFlags().String("token", "", "Token to insert")  

But it prints me the flags like that:

Flags:

      -h, --help           help for myapp
          --token string   Token to insert  

I want it to be like that:

Flags:

      -h, --help           help for myapp
      -t, --token string   Token to insert   

How can I do it?
I didn't find it on google, I tried to search for multiple names for a flag but without success.

E235
  • 11,560
  • 24
  • 91
  • 141
  • 2
    It's in the examples in the readme at the page you linked: https://github.com/spf13/cobra#working-with-flags so e.g. use `StringVarP` in place of `StringVar` or `StringP` instead of `String`. – Adrian Jul 23 '19 at 15:45

1 Answers1

0

The answer is an example in the documentation. This example uses both --projectbase and -b:

rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56