Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active September 20, 2017 18:04
Show Gist options
  • Save vadimkantorov/d7f60b98f312a686f7f946acab272a12 to your computer and use it in GitHub Desktop.
Save vadimkantorov/d7f60b98f312a686f7f946acab272a12 to your computer and use it in GitHub Desktop.
# Example: python argparse_lookup_choices.py --lookup key1
# Namespace(lookup='1')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--lookup', choices = dict(key1 = '1', key2 = '2'), default = '1', action = type('', (argparse.Action, ), dict(__call__ = lambda a, p, n, v, o: setattr(n, a.dest, a.choices[v]))))
print(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment