-
Notifications
You must be signed in to change notification settings - Fork 274
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: min length, max length, and whitespace trimming #125
Comments
After additional investigation, it looks like Thanks! |
Hi there. Yes, the actions and Validate methods were poorly thought out. Fixing that is the first item in the v3 wish list (#90). I don't think adding functions for specific types of validation is the right approach though, but fixing the Validate functions is. I've added a link to this issue for details. Thanks for the report. |
I've had the same thoughts when trying to figure out how to use these, I'm glad it's not just me 😄 For instance, where does the I'll look forward to v3 for these 😄 |
The code that handled pset-based commandline arguments using `kingpin`'s `PreAction` callback was not working as intended. The context variable passed to the callback contains information on _all_ commandline arguments, and [doesn't provide any information specific to the particular flag being processed](alecthomas/kingpin#125 (comment)). There was also a problem with the callback construction because of [the way go implements closures](https://golang.org/doc/faq#closures_and_goroutines). The result is that the variables reflect the last value from the `for` loop, which is why, as pointed out in xo#97, there is no difference between the `-J` and `-C` options: the last iteration of the `for` loop was for the CSV entry, so regardless which of the `-A, -F, -H, -R, -t, -T, -x , -z, -0, -J, or -C` options are specified, they all end up setting `format=csv`. A new custom type `pset` was created to handle the string-based flags, and the existing for loop has been modified to properly handle the boolean flags.
The code that handled pset-based commandline arguments using `kingpin`'s `PreAction` callback was not working as intended. The context variable passed to the callback contains information on _all_ commandline arguments, and [doesn't provide any information specific to the particular flag being processed](alecthomas/kingpin#125 (comment)). There was also a problem with the callback construction because of [the way go implements closures](https://golang.org/doc/faq#closures_and_goroutines). The result is that the variables reflect the last value from the `for` loop, which is why, as pointed out in #97, there is no difference between the `-J` and `-C` options: the last iteration of the `for` loop was for the CSV entry, so regardless which of the `-A, -F, -H, -R, -t, -T, -x , -z, -0, -J, or -C` options are specified, they all end up setting `format=csv`. A new custom type `pset` was created to handle the string-based flags, and the existing for loop has been modified to properly handle the boolean flags.
The
.String()
type is versatile, but often it is more permissive than we want. For example, some flags might have a maximum or minimum length. Minimum length is not naturally expressed with.Required()
as that implies a minimum length of 1 and does not trim whitespace.Suggestion: Add some modifiers to FlagClause such as:
a.
MinLength
to enforce a min lengthb.
MaxLength
to enforce a max lengthc.
Trimmed
to always whitespace-trim the flag before processingUsing custom
flag.Value
types doesn't appear to be an elegant solution to this problem because:flag.Value
implementations.Set
are not displayed by kingpin with the flag name prefixed, which makes implementation of useful error messages awkward. For example, areturn fmt.Errorf("max length of %d exceeded", m.maxLength)
from aflag.Value
.Set()
printsIt would be nice if this also contained the name of the flag that was being validated.
If I misunderstood how Kingpin is to be used, please send me in the right direction. I see v1.3.4 added "Add post-app and post-cmd validation hooks. This allows arbitrary validation to be added." but I couldn't find any examples or additional docs on this.
Thanks!
The text was updated successfully, but these errors were encountered: