Feature Request: Allow optional values in map flags (e.g., StringToStringOptional)
Is your feature request related to a problem? Please describe.
Currently, when using StringToStringVar or StringToStringVarP, the user is strictly forced to provide arguments in the key=value format. If a user provides just key without the equal sign, the pflag parser immediately returns a csv string must be in the form key=value error.
In many CLI applications, developers need to accept a list of items where the value (like an alias, tag, or override) is optional.
For example, a daemon accepting network MAC addresses with an optional alias:
--bssids 7E:BC:07:E8:67:47,AA:BB:CC:DD:EE:FF=Work
Describe the solution you'd like It would be highly beneficial to have native support for optional values in map-based flags. This could be achieved by:
- Adding a new flag type like
StringToStringOptional(and its variations) where a missing=separator simply defaults the map value to an empty string"". - Modifying the existing map flag implementations to accept an option that relaxes the strict
key=valuerequirement.
Describe alternatives you've considered Currently, to achieve this behavior, developers have to reinvent the wheel by either:
- Falling back to a standard
StringSliceVarand manually iterating over the slice to parse it withstrings.Cut(val, "=")in theirRunfunction. - Writing a boilerplate custom
pflag.Valueimplementation just to bypass the strict parsing check.
Additional context
I understand that the actual parsing logic lives upstream in spf13/pflag, but since Cobra manages the flag integration and CLI ergonomics, I wanted to open the discussion here regarding the API surface and whether this is a feature the team would welcome a PR for.
Source: spf13/cobra