How to get flags to work in Cobra using Local flags
package mainimport ("fmt""github.com/spf13/cobra")func main() {myCmd := &cobra.Command{Use: "myaction",Run: func(cmd *cobra.Command, args []string) {if len(args) == 1 {flags := cmd.Flags()var strTmp stringflags.StringVarP(&strTmp, "test", "t", "", "Source directory to read from")fmt.Println(strTmp)}},}myCmd.Execute()}
Error
go run main.go myaction --test="hello"Error: unknown flag: --testUsage:myaction [flags]Flags:-h, --help help for myaction
Best Answer
The Run
part of the command gets executed only after the Execute
is called on the command (you can also have a prerun-hook.)
So in your case when execute is called the runtime doesn't know about the flag. We should add the flags before the Execute
is called on the command to make the runtime aware of it.
Code
package mainimport ("fmt""github.com/spf13/cobra")func main() {var strTmp stringmyCmd := &cobra.Command{Use: "myaction",Run: func(cmd *cobra.Command, args []string) {if len(args) == 1 {fmt.Println(strTmp)}},}myCmd.Flags().StringVarP((&strTmp), "test", "t", "", "Source directory to read from")myCmd.Execute()}
Output
⇒ go run main.go myaction --test "hello"hello
But there is an alternate solution to this, when you want to add the flags based on a condition. You can set DisableFlagParsing
to true
and parse it later.
Code
package mainimport ("fmt""github.com/spf13/cobra")func main() {myCmd := &cobra.Command{Use: "myaction",RunE: func(cmd *cobra.Command, args []string) error {if len(args) > 1 {flags := cmd.Flags()var strTmp string// Add the flagflags.StringVarP(&strTmp, "test", "t", "", "Source directory to read from")// Enable the flag parsingcmd.DisableFlagParsing = false// Parse the flagsif err := cmd.ParseFlags(args); err != nil {return err}fmt.Println(strTmp)}return nil},}// Disable flag parsingmyCmd.DisableFlagParsing = truemyCmd.Execute()}
Output
⇒ go run main.go myaction --test "hello"hello