cobra
cobra copied to clipboard
Handle requiredFlags errors in the same way as errors originating from parsing flags.
At the moment there is a difference between the error messages + output when a required argument is not filled in compared to when an unexisting flag is passed.
I validate this by passing in a custom flagErrorFunc which prints both the error and the usage. This error function is correctly used when the flag parsing fails, but it is not taken into account when validating required arguments fails.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.
Signed the cla
This PR is being marked as stale due to a long period of inactivity
Thanks for the contribution @nclaeys.
I want to make sure we don't have a backwards-compatibility issue here. Could you provide an example of the bad behavior and of the improved behavior so that I can be sure I understand what you are aiming to fix?
Thanks
Hi @marckhouzam,
So the goal is that when you specify a custom flagErrorFunc it is always used when flag validation fails. There are two code paths that result in an error due to flag validation:
- a flag is used that does not exists (uses the errorFunc to print the error)
- a required flag is not set (does not use the errorFunc) Example usage at the moment when you specify a custom error function (in our case it contains: usage string + error):
- When not specifying a required flag Output:
required flag(s) "name" not set
exit status 1
- When specifying a flag that does not exist: Output:
go run main.go environment new --test bla
Usage:
datafy environment create [flags]
Aliases:
create, new
Flags:
--name string The name of the environment to create
Global Flags:
--debug Show debug output
unknown flag: --test
failed flag parsing
exit status 1
This PR should change the first behavior such that it becomes: Output:
go run main.go environment new
Usage:
datafy environment create [flags]
Aliases:
create, new
Flags:
--name string The name of the environment to create
Global Flags:
--debug Show debug output
required flag(s) "name" not set
failed flag parsing
exit status 1
Note: I updated the test to be more explicit, hopefully this helps you in understanding what I am saying.
Any update on this?