-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add StringMap flag #1590
Add StringMap flag #1590
Conversation
d32d928
to
841f091
Compare
@avorima the changes look good . Can you rebase off of 3.x since we are not adding new flags in 2.x anymore |
@dearchap What branch is that, v3-dev-main? |
main |
4b5281d
to
7466f42
Compare
Code generation is somewhat broken by the changes to documentation in 1.19 |
fe265f6
to
fe9471c
Compare
Yeah, so there definitely is a difference when generating code with 1.18 and 1.19. I went with 1.19 now. |
fe9471c
to
ef880bd
Compare
if reflect.TypeOf(t).Kind() == reflect.String { | ||
return fmt.Sprintf("%v", v) | ||
} | ||
return fmt.Sprintf("%T{%s}", v, i.ToString(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a simple test case to test map[string]int to enable codecov of this line ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this, but shouldn't that be added together with IntMap
or similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you could do that.
flag_map_impl.go
Outdated
if !ok { | ||
return fmt.Errorf("item %q is missing separator %q", item, defaultMapFlagKeyValueSeparator) | ||
} | ||
if err := i.value.Set(strings.TrimSpace(value)); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would we ever need to trimspace for this kind of flags ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's easy to do something like ./prog --myflag "foo=$BAR"
and BAR containing unwanted whitespace because it's a script output or whatever. On the other hand removing whitespace can also be delegated to the users of the library. How about I add a TrimSpace
flag field that controls this behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure that would be good. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I added it via the flag config. I had to add it to all string related flags to make the compiler happy
flag_impl.go
Outdated
@@ -192,7 +192,8 @@ func (f *FlagBase[T, C, V]) RunAction(ctx *Context) error { | |||
// IsSliceFlag returns true if the value type T is of kind slice | |||
func (f *FlagBase[T, C, VC]) IsSliceFlag() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to rename this to IsMultiValueFlag as we are overloading this function name now . I can make that change in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5dd5dd0
to
2dac090
Compare
StringMap wraps a `map[string]string` and parses inputs in the form of "key=value".
This config allows configuring to automatically trim whitespace of string value flags. This includes slices and maps. I had to plumb this down to the string flag level, because of compiler errors.
2dac090
to
e8f968a
Compare
What type of PR is this?
What this PR does / why we need it:
Add
StringMap
andStringMapFlag
types that are backed by amap[string]string
and parse comma-separated key=value pairs.This is an analogue of pflag's StringToString.
Which issue(s) this PR fixes:
Fixes #1580
Special notes for your reviewer:
I mostly repurposed code from StringSlice. The tests were especially confusing to me since it seems a lot of it is duplicated.
Testing
Created a sample app that uses the flag.
Release Notes