Created
August 2, 2017 16:50
-
-
Save thewickedaxe/738878f4f851f155c3be4a4d7d2ae131 to your computer and use it in GitHub Desktop.
Dynamic args in argparse
This file contains hidden or 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() | |
| parser.add_argument('-k','--kwarg',nargs=3,action='append') | |
| args=parser.parse_args('-k mass 100 inf -k spin 0.5 1.0'.split()) | |
| Namespace(kwarg=[['mass', '100', 'inf'], ['spin', '0.5', '1.0']]) | |
| they could be converted to a dictionary with an expression like: | |
| vargs={key:(float(v0),float(v1)) for key,v0,v1 in args.kwarg} | |
| which could be passed to your function as: | |
| foo(**vargs) | |
| {'spin': (0.5, 1.0), 'mass': (100.0, inf)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment