Skip to content

Instantly share code, notes, and snippets.

@thewickedaxe
Created August 2, 2017 16:50
Show Gist options
  • Select an option

  • Save thewickedaxe/738878f4f851f155c3be4a4d7d2ae131 to your computer and use it in GitHub Desktop.

Select an option

Save thewickedaxe/738878f4f851f155c3be4a4d7d2ae131 to your computer and use it in GitHub Desktop.
Dynamic args in argparse
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