Created
March 25, 2019 12:33
-
-
Save Shuhala/7b717b9bacf226e6161dee633a7d029a to your computer and use it in GitHub Desktop.
Dictionary type for argparse argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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