#2491·cobra

Feature Request: Allow optional values in map flags (e.g., StringToStringOptional)

Author: thomas-btstCreated Aug 28, 2026Updated Aug 28, 2026

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:

  1. Adding a new flag type like StringToStringOptional (and its variations) where a missing = separator simply defaults the map value to an empty string "".
  2. Modifying the existing map flag implementations to accept an option that relaxes the strict key=value requirement.

Describe alternatives you've considered Currently, to achieve this behavior, developers have to reinvent the wheel by either:

  1. Falling back to a standard StringSliceVar and manually iterating over the slice to parse it with strings.Cut(val, "=") in their Run function.
  2. Writing a boilerplate custom pflag.Value implementation 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.