-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Help.How to use multiple command with multiple flags? #1673
Comments
Thinking about this out of curiousity for a bit more but the reason you're still getting this is that the root command isn't being selected (and that switch logic never being hit). The reason is that when cobra tries to figure out which command to run, it finds |
So just tinkering to give you some ideas but this is what I did:
This gets everything invoked the way you want but you'll still get the problem each time since cmd1 doesnt know about flags from cmd2:
I thought about setting the flag error func on the commands to return nil, but that doesn't actually cause the error to be ignored, just exit with no error. I think that actually could be an addition: if cobra had a special class of error you could do something like this:
I may create an issue for that and see if it can get picked up. However, I realized that since we added the flags to the root command, it already did all the parsing necessary so we can skip it entirely on the subcommands by setting Here is the new output:
and the updated code is at this playground link: https://go.dev/play/p/KGxUqo9P-wf but the only important change was at the end after defining the commands (I tweaked their output to better see what was parsed/ran):
So I think those few changes (adding flags to root, disabling children flag parsing, and not adding them as children) solves your problem? |
I also found instead of disabling the flag parsing on the children you can set:
so that only the unknown flag parsing errors are ignored. Other flag errors, presumably, still get thrown. |
Hi @johnSchnake Thanks for you wonderful answers.Now I know how to deal with it.I appreciate you helping me.🌹 |
Hi @johnSchnake |
What I want:
go run test.go comm1 -d xxx comm2 -a xxx
I want to use comm1 and comm2 with their own flags at the same time
But i got an issue:
my code:
I've ever checked #726 ,but did't solve the problem.😫
Thanks so much if someone can help me🤍
The text was updated successfully, but these errors were encountered: