Cobra: panic: shorthand redefinition when using PersistentFlags

Created on 15 Feb 2015  路  2Comments  路  Source: spf13/cobra

I'm getting a panic error when using PersistentFlags. I guess this is caused due to a bad closing in a goroutine process

flags := cmd.PersistentFlags()
flags.StringVarP(&host, "host", "h", "127.0.0.1", "path to config file")
flags.StringVarP(&port, "port", "p", "8000", "port to listen")
flags.StringVarP(&forward, "forward", "f", "http://httpbin.org:80", "port to listen")
flags.StringVarP(&config, "config", "c", "", "path to config file")
flags.BoolVarP(&verbose, "verbose", "v", false, "verbose output")
panic: shorthand redefinition

goroutine 1 [running]:
github.com/spf13/pflag.(*FlagSet).AddFlag(0xc208068100, 0xc20800a3c0)
    /Users/h2non/go/src/github.com/spf13/pflag/flag.go:391 +0xb03
github.com/spf13/pflag.(*FlagSet).VarP(0xc208068100, 0x7c41a0, 0xc208000fa8, 0x4a7970, 0x4, 0x4a7590, 0x1, 0xc20802ad80, 0xf)
    /Users/h2non/go/src/github.com/spf13/pflag/flag.go:362 +0x162
github.com/spf13/pflag.(*FlagSet).BoolVarP(0xc208068100, 0xc208000fa8, 0x4a7970, 0x4, 0x4a7590, 0x1, 0x0, 0xc20802ad80, 0xf)
    /Users/h2non/go/src/github.com/spf13/pflag/bool.go:45 +0xae
github.com/spf13/cobra.(*Command).Flags(0xc208000ea0, 0x0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:695 +0x299
github.com/spf13/cobra.func路011(0xc20800a300)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:830 +0x2f
github.com/spf13/pflag.(*FlagSet).VisitAll(0xc208068100, 0xc2080b3b18)
    /Users/h2non/go/src/github.com/spf13/pflag/flag.go:193 +0x88
github.com/spf13/cobra.func路012(0xc208000ea0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:833 +0xab
github.com/spf13/cobra.(*Command).mergePersistentFlags(0xc208000ea0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:840 +0x77
github.com/spf13/cobra.(*Command).ParseFlags(0xc208000ea0, 0xc20800a010, 0x5, 0x5, 0x0, 0x0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:806 +0x4b
github.com/spf13/cobra.(*Command).execute(0xc208000ea0, 0xc20800a010, 0x5, 0x5, 0x0, 0x0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:358 +0xd1
github.com/spf13/cobra.(*Command).findAndExecute(0xc208000ea0, 0xc20800a010, 0x5, 0x5, 0x0, 0x0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:350 +0xd4
github.com/spf13/cobra.(*Command).Execute(0xc208000ea0, 0x0, 0x0)
    /Users/h2non/go/src/github.com/spf13/cobra/command.go:436 +0xab3
main.NewCmd()
    /Users/h2non/projects/vinci/cli.go:44 +0x4bd

Most helpful comment

It seems this error relates to the shortcode "h" already being defined by Cobra in command.go:862

c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())

Changing the value in command.go to another letter or defining it without the shortcode appears to resolve the issue. That said, this may be a desired behavior of Cobra so proceed with caution.

All 2 comments

It seems this error relates to the shortcode "h" already being defined by Cobra in command.go:862

c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())

Changing the value in command.go to another letter or defining it without the shortcode appears to resolve the issue. That said, this may be a desired behavior of Cobra so proceed with caution.

You should be able to solve this now given #133. If you really need to use -h somewhere else, define your own "help" flag without the "h" shorthand.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdaymard picture mdaymard  路  5Comments

garthk picture garthk  路  3Comments

jjzcru picture jjzcru  路  3Comments

eine picture eine  路  5Comments

diwakergupta picture diwakergupta  路  5Comments