Skip to content
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

[Feature Request] A way to specify required parameters else error #128

Open
philwinder opened this issue Jan 27, 2024 · 8 comments
Open

Comments

@philwinder
Copy link

Hi Peter,
Great work as always. I find myself having to duplicate boilerplate to check for empty parameters.

Can I propose that ff.NewFlagSet has some way of specifying that it should error if no parameter has been provided?

And ideally the error message should include a nice user friendly description?

Thanks, Phil

@peterbourgon
Copy link
Owner

peterbourgon commented Jan 27, 2024

Cheers! Not sure exactly what you mean by "if no parameter has been provided" — can you say a bit more?

Maybe you mean if name == ""? Alternatively, can you paste an example of the boilerplate that you're having to repeat?

@peterbourgon
Copy link
Owner

@philwinder Friendly ping :)

@shidil
Copy link

shidil commented Mar 9, 2024

I think @philwinder is referring to marking certain flags as required, and ff.Parse handling the validation checks while printing help text explaining that flag --xx is required(but no value was provided).

Example https://cobra.dev/#required-flags

@mfridman
Copy link

Assuming this issue is asking for required flags, then +1.

It'd be neat if the Flag interface could expanded to include a setter like SetRequired (or maybe there's a better implementation), and then all the error states and help text would behave consistently.

@akupila
Copy link

akupila commented Apr 2, 2024

+1 for having a way to specify required flags.

For now, i've used this to get similar behavior, but it would be nice if there was a more "native" way to do it:

func checkRequired(cmd *ff.Command, flag ...ff.Flag) {
	missing := make([]ff.Flag, 0, len(flag))
	for _, f := range flag {
		if !f.IsSet() {
			missing = append(missing, f)
		}
	}
	if len(missing) > 0 {
		fmt.Fprintln(os.Stderr, ffhelp.Command(cmd))
		fmt.Fprintln(os.Stderr, "Required flags were not provided:")
		for _, f := range missing {
			fmt.Fprintln(os.Stderr, ffhelp.FormatFlag(f, "  %+s"))
		}
		os.Exit(2)
	}
}

@peterbourgon
Copy link
Owner

Just so I understand: is the idea here that Parse should fail if a flag marked as required (however that might work) was not explicitly specified (as reported by IsSet)?

@mfridman
Copy link

mfridman commented Apr 9, 2024

I believe so. I wanted a bag of reusable but required flags (defined once) across multiple commands and so ended up with a global 😬 like this (in case you're interested to see how it's used in an OSS project).

https://github.com/pressly/goose/pull/741/files#diff-30f30550c03f971732cfeb7b92d7d64e64447acf969bfa5a55832429cf18cd01R50-R71

ps. Ideally, I could pass these down from the root command as a parent but I didn't want all parent flags to be inherited (but that's outside the scope of this issue). I'm still figuring out the best way to compose this, so don't mind the mess in that PR for now.

@philwinder
Copy link
Author

Sorry for the delay, I missed the pings. Yes, you are both correct.

I would like some way of specifying that a particular flag is required and the program will error out if it is not provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants