Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shorthand not working and throw panic while use name works #2082

Open
cybernastro opened this issue Nov 27, 2023 · 2 comments
Open

shorthand not working and throw panic while use name works #2082

cybernastro opened this issue Nov 27, 2023 · 2 comments

Comments

@cybernastro
Copy link

cybernastro commented Nov 27, 2023

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

@marckhouzam
Copy link
Collaborator

I believe your s variable must be global

@cybernastro
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants