spf13/cobra

shorthand not working and throw panic while use name works

cybernastro opened this issue · 2 comments

so i have this

cli myCommand -myflag args[]string

and i have f for my shorthand
when i use the name it works
but when i was using

cli myCommand -f args[]string

it will directly throw panic
panic: runtime error: index out of range [0] with length 0

myCommand:

RUN: afunction
func init(){
var s string
rootCmd.AddCommand(myCommand)
myCommand.Flags().StringVarP(&s, "myflag", "f", "", "des")
err := myCommand.MarkFlagRequired("myflag")
...
}
func afunction(cmd *cobra.Command, args []string){
}

in the end, my solution is using myCommand.DisableFlagParsing=true, and use the args[1] as input

but i wonder why it throws panic while using shorthand

I believe your s variable must be global

I believe your s variable must be global

no, it isnt, and btw even if i used that variable as global, the name still works fine, the shorthand still not working