argv_to_dict

grizli.utils.argv_to_dict(argv, defaults={}, dot_dict=True)[source]

Convert a list of (simple) command-line arguments to a dictionary.

Parameters:
argv : list of strings

E.g., sys.argv[1:].

defaults : dict

Default dictionary

dot_dict : bool

If true, then intepret keywords with ‘.’ as nested dictionary keys, e.g., --d.key=val >> {‘d’: {‘key’: ‘val’}}

Examples

# $ myfunc arg1 –p1=1 –l1=1,2,3 –pdict.k1=1 -flag >>> argv = ‘arg1 –p1=1 –l1=1,2,3 –pdict.k1=1 -flag’.split() >>> args, kwargs = argv_to_dict(argv) >>> print(args) [‘arg1’] >>> print(kwargs) {‘p1’: 1, ‘l1’: [1, 2, 3], ‘pdict’: {‘k1’: 1}, ‘flag’: True}

# With defaults defaults = {‘pdict’:{‘k2’:2.0}, ‘p2’:2.0} >>> args, kwargs = argv_to_dict(argv, defaults=defaults) >>> print(kwargs) {‘pdict’: {‘k2’: 2.0, ‘k1’: 1}, ‘p2’: 2.0, ‘p1’: 1, ‘l1’: [1, 2, 3], ‘flag’: True}