split_kv¶
- lsst.daf.butler.cli.utils.split_kv(context: ~click.core.Context, param: ~click.core.Option, values: list[str], *, choice: ~click.types.Choice | None = None, multiple: bool = True, normalize: bool = False, separator: str = '=', unseparated_okay: bool = False, return_type: type[dict] | type[tuple] = <class 'dict'>, default_key: str | None = '', reverse_kv: bool = False, add_to_default: bool = False) dict[str | None, str] | tuple[tuple[str | None, str], ...] ¶
Process a tuple of values that are key-value pairs separated by a given separator. Multiple pairs may be comma separated. Return a dictionary of all the passed-in values.
This function can be passed to the ‘callback’ argument of a click.option to allow it to process comma-separated values (e.g. “–my-opt a=1,b=2”).
- Parameters:
- context
click.Context
orNone
The current execution context. Unused, but Click always passes it to callbacks.
- param
click.core.Option
orNone
The parameter being handled. Unused, but Click always passes it to callbacks.
- values[
str
] All the values passed for this option. Strings may contain commas, which will be treated as delimiters for separate values.
- choice
click.Choice
, optional If provided, verify each value is a valid choice using the provided
click.Choice
instance. If None, no verification will be done. By defaultNone
.- multiple
bool
, optional If true, the value may contain multiple comma-separated values. By default True.
- normalize
bool
, optional If True and
choice.case_sensitive == False
, normalize the string the user provided to match the choice’s case. By defaultFalse
.- separatorstr, optional
The character that separates key-value pairs. May not be a comma or an empty space (for space separators use Click’s default implementation for tuples;
type=(str, str)
). By default “=”.- unseparated_okay
bool
, optional If True, allow values that do not have a separator. They will be returned in the values dict as a tuple of values in the key ‘’, that is:
values[''] = (unseparated_values, )
. By default False.- return_type
type
, must bedict
ortuple
The type of the value that should be returned. If
dict
then the returned object will be a dict, for each item in values, the value to the left of the separator will be the key and the value to the right of the separator will be the value. Iftuple
then the returned object will be a tuple. Each item in the tuple will be 2-item tuple, the first item will be the value to the left of the separator and the second item will be the value to the right. By defaultdict
.- default_key
str
orNone
The key to use if a value is passed that is not a key-value pair.
None
can imply no separator depending on how the results are handled. (Passing values that are not key-value pairs requiresunseparated_okay
to beTrue
).- reverse_kvbool
If true then for each item in values, the value to the left of the separator is treated as the value and the value to the right of the separator is treated as the key. By default
False
.- add_to_default
bool
, optional If True, then passed-in values will not overwrite the default value unless the
return_type
isdict
and passed-in value(s) have the same key(s) as the default value.
- context
- Returns:
- Raises:
click.ClickException
Raised if the separator is not found in an entry, or if duplicate keys are encountered.