-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Mandatory Flags not verified before Prerun. #655
Comments
#747 Issues are related |
I recently got into same error |
I have a similar issue in that the configured default values for flags are not set if they are missing from the CLI. Parsing fails with errors like the one above, "Error: required flag(s) "test" not set". I would have expected the default value to be assumed for the flag if it's missing from the CLI. I have to call Have I missed something obvious? |
This is old but still persists in the latest release. Marking a flag as required is effectively useless if you intend to use the flag value in the PreRun functions. |
This issue is being marked as stale due to a long period of inactivity |
Hey folks, was wondering if this is by design (apologies if I have missed another issue in case it discusses the same). I came across this issue recently. To circumvent this issue, moved the flow added in |
Yes, I think this is a duplicate issue but also one that comes up often. The flag parsing doesnt happen until later in the flow and I think this is by design (it was always in that position, prerun wasn't added later or anything). This allows you to see/alter flag data/settings before flag validation would takeover and potentially error out. There was another ticket that I think should be taken up which is simply to add another hook after flags parse but before run for clarity. That way you can more clearly understand where to put the logic that needs all the flag info but may be before the core 'run' logic. Seeing as how there are multiple other issues of a similar vein and there are workarounds given in this thread, I'm going to go ahead and close this issue. Feel free to reopen if you think this is done in error. |
It makes sense to me that flag validation would happen after PersistentPreRun. That is where we can manipulate the flags if needed. I use the PersistentPreRun hook to work around spf13/viper#397. |
Presence of Mandatory Flags is not verified before Prerun unlike Args Check which happens before Prerun.
Hence can't safely use Mandatory Flags in Prerun without additional manual checks.
Sample Code Demonstrating.
Output
Value for Mandator TestFlag is Nothing
Error: required flag(s) "test" not set
Usage:
test [flags]
Flags:
-h, --help help for test
-t, --test string Test Flag (default "Nothing")
The text was updated successfully, but these errors were encountered: