optdict2list

opthandler.optdict2list(optdict, convert_to_str=True, convert_from_str=False, skiped_opts=None, dumped_vals=None, **kwargs)[source]

Convert an option dictionary to an option list.

Parameters:
  • optdict (dict) – The option dictionary that should be converted to an option list.

  • convert_to_str (bool, optional) – If True, convert the values of the input dictionary to strings.

  • convert_from_str (bool, optional) – If True, apply convert_str() on the values of the input dictionary. This will convert strings to their corresponding Python data types. Must not be used together with convert_to_str.

  • skiped_opts (list or tuple or None, optional) – If not None, skip key-value pairs of the input dictionary whose value is contained in skiped_opts.

  • dumped_vals (list or tuple or None, optional) – If not None, don’t include the given values of the input dictionary in the returned option list.

  • kwargs (dict, optional) – Keyword arguments to parse to convert_str().

Returns:

optlist (list) – The resulting option list. Each key of optdict is prefixed with either a single hyphen ('-') or two hyphens, depending on whether the key consists of a single character or multiple characters.

See also

optdict2str()

Convert an option dictionary to an option string

optlist2dict()

Convert an option list to an option dictionary

Notes

If convert_to_str or convert_from_str is True, skiped_opts and dumped_vals will be compared to the converted values.

Examples

>>> optdict2list({'a': 0, 'Bc': 'xY'})
['-a', '0', '--Bc', 'xY']
>>> optdict2list({' a': 0, ' Bc ': 'xY '})
['-a', '0', '--Bc', 'xY']
>>> optdict2list({' a': 0, ' Bc ': 'xY '}, convert_to_str=False)
['-a', 0, '--Bc', 'xY ']
>>> optdict2list({'a': 0, 'Bc': 'xY Ab'})
['-a', '0', '--Bc', 'xY Ab']
>>> optdict2list({'a': 0, 'Bc': '', 'xy': 'z'})
['-a', '0', '--Bc', '--xy', 'z']
>>> optdict2list(
...     {'a': 0, 'Bc': None, 'xy': False},
...     skiped_opts=('None', 'False'),
... )
['-a', '0']
>>> optdict2list(
...     {'a': 0, 'Bc': None, 'xy': False},
...     skiped_opts=(None, False),
... )
['-a', '0', '--Bc', 'None', '--xy', 'False']
>>> # 0 is False, 1 is True
>>> optdict2list(
...     {'a': 0, 'Bc': None, 'xy': False},
...     skiped_opts=(None, False),
...     convert_to_str=False,
... )
[]
>>> optdict2list(
...     {'a': 0, 'Bc': None, 'xy': False},
...     skiped_opts=('None', 'False'),
...     convert_to_str=False,
... )
['-a', 0, '--Bc', None, '--xy', False]
>>> optdict2list(
...     {'a': 0, 'Bc': None, 'xy': True},
...     dumped_vals=('None', 'True'),
... )
['-a', '0', '--Bc', '--xy']