cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Handle requiredFlags errors in the same way as errors originating from parsing flags.

Open nclaeys opened this issue 3 years ago • 6 comments

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.

nclaeys avatar Oct 13 '21 14:10 nclaeys

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 13 '21 14:10 CLAassistant

CLA assistant check 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

nclaeys avatar Oct 13 '21 14:10 nclaeys

This PR is being marked as stale due to a long period of inactivity

github-actions[bot] avatar Dec 21 '21 00:12 github-actions[bot]

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

marckhouzam avatar Mar 17 '22 22:03 marckhouzam

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):
  1. When not specifying a required flag Output:
required flag(s) "name" not set
exit status 1
  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.

nclaeys avatar Mar 22 '22 18:03 nclaeys

Any update on this?

pierre-borckmans avatar May 05 '22 16:05 pierre-borckmans