-
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
Question: how to make flags required #206
Comments
I am not sure that is implemented as it uses, pflag So there is no direct way unless you do it in |
Really its just a bad name since it was never implemented. It will cause bash completions and things to always suggest that flag, but we aren't enforcing it. You doing it in a PreRunE is a good idea. |
I didn't know about the PreRun function, I'll look into that. Yeah, the name is misleading .. so does that mean calling |
@tleyden use |
At this time its only effect is on the bash completions :-( I will try to write a PreRunE which enforces it.... |
|
just a reminder, if it is required it is an parameter, or argument. If it is and option or flag it is NOT required. That is the very nature of flags and options. Every command line library supporting "required" or mandatory options/flags I would considered broken by design. |
@crux like the |
From my point of view it it makes sense to use arguments if there are only a few (1-3) but as soon as you have more mandatory arguments, it makes sense to use flags. Otherwise it is hard to keep the overview. |
If you need it I wrote something for this purpose. It could be done better but this does the work. You just need to mark the flags as required and then:
Then add these in PreRunE:
|
Before this commit, running `kedge` subcommands without passing any files would do nothing. $ kedge deploy $ $ kedge generate $ $ kedge undeploy $ This commit adds a check that input files are passed using -f or --files flags, and if not, throws an error. $ kedge generate Error: Unable to validate input files: No files were passed. Please pass file(s) using '-f' or '--files' The flags have also been marked as required, however, that takes no effect, and there is an open issue on the spf13/cobra package tracking the same - spf13/cobra#206
Before this commit, running `kedge` subcommands without passing any files would do nothing. $ kedge deploy $ $ kedge generate $ $ kedge undeploy $ This commit adds a check that input files are passed using -f or --files flags, and if not, throws an error. $ kedge generate Error: Unable to validate input files: No files were passed. Please pass file(s) using '-f' or '--files' The flags have also been marked as required, however, that takes no effect, and there is an open issue on the spf13/cobra package tracking the same - spf13/cobra#206 Fixes kedgeproject#96
Before this commit, running `kedge` subcommands without passing any files would do nothing. $ kedge deploy $ $ kedge generate $ $ kedge undeploy $ This commit adds a check that input files are passed using -f or --files flags, and if not, throws an error. $ kedge generate Error: Unable to validate input files: No files were passed. Please pass file(s) using '-f' or '--files' The flags have also been marked as required, however, that takes no effect, and there is an open issue on the spf13/cobra package tracking the same - spf13/cobra#206 Fixes kedgeproject#96
Before this commit, running `kedge` subcommands without passing any files would do nothing. $ kedge deploy $ $ kedge generate $ $ kedge undeploy $ This commit adds a check that input files are passed using -f or --files flags, and if not, throws an error. $ kedge generate Error: Unable to validate input files: No files were passed. Please pass file(s) using '-f' or '--files' The flags have also been marked as required, however, that takes no effect, and there is an open issue on the spf13/cobra package tracking the same - spf13/cobra#206 Fixes kedgeproject#96
Is @0xC0D3D00D's solution still the way to go with this? I understand his snippet and it's pretty clever, but it seems that it should be a better way to mark flags as required (even when some could argue that it shouldn't be a flag in the first instance). |
@agonzalezro, At Kubernetes @dixudx @brendandburns made my snippet even better. You can find it in this merge request. On the other side, I think the current design of flags in cobra is impractical in many cases. I like the verb and noun concepts but the flags need to be required in some cases. For example:
The latter is a little harder to implement. I'm sure there are more cases which this feature is necessary for them. |
I submitted a PR #502 to enforce the required flag. PTAL. |
Hi guys, can this issue be closed since #502 has been merged. |
Fixed in #502 |
I added a usage example here |
Don't know if this is really fixed. When using the examples in the readme for creating a required flag but with Anyhow, a workaround, hope it help someone who ends up here:
|
Seems like this doesn't _actually_ work. Sadness spf13/cobra#206
Seems like this doesn't _actually_ work. Sadness spf13/cobra#206
Thanks |
@spaddex thank you for the code snippet - I was running into a similar issue when trying to use required persistent flags. I ended up using the following approach (harnessing var foo string
func init() {
pf := rootCmd.PersistentFlags()
pf.StringVarP(&foo, "foo", "f", "", "example flag")
cobra.MarkFlagRequired(pf, "foo")
} |
It's unfortunate that enforcing required flags natively is solely dependent on shell completion scripts. This just passes the buck to the user, who now has to inject the script into their shell in order for the app to run as it's designed. |
@copejon I'm not sure I understand what you mean. What scenario is causing you problems? |
I'm defining some flags like this:
and even explicitly calling
ParseFlags
(out of desperation):and calling:
with no
--url
flag, but cobra isn't throwing errors.I was expecting that if I set
MarkPersistentFlagRequired
but failed to pass the flag on the command line then the command would fail with an error and a usage example.The text was updated successfully, but these errors were encountered: