Skip to content

Instantly share code, notes, and snippets.

@Shuhala
Created March 25, 2019 12:33
Show Gist options
  • Save Shuhala/7b717b9bacf226e6161dee633a7d029a to your computer and use it in GitHub Desktop.
Save Shuhala/7b717b9bacf226e6161dee633a7d029a to your computer and use it in GitHub Desktop.
Dictionary type for argparse argument
parser = argparse.ArgumentParser(description="input `a=b&c=d` becomes `dict({'a':'b', 'c':'d'})`")
parser.add_argument(
"-p", "--params",
help="Convert parameters in the form `--params a=b&c=d` to a dictionary",
action=type('', (argparse.Action,), dict(
__call__=lambda a, _, n, v, __: setattr(n, a.dest, dict([kv.split('=') for kv in v.split('&')]))
)),
default={},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment