Last active
January 23, 2016 11:22
-
-
Save evanunderscore/3a621f3f5c810b1444c7 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
"""Example of choices with begins. | |
Python code: | |
main() | |
main('bar') | |
main(arg='baz') | |
Command line equivalent: | |
test.py | |
test.py --arg bar | |
test.py --arg baz | |
Deficiencies: | |
* main(Choices.bar) doesn't work | |
* test.py -h doesn't show the choices | |
* @begin.convert(_automatic=True) doesn't work | |
""" | |
# enum was introduced in Python 3.4 | |
# For earlier versions, pip install enum34 | |
from enum import Enum | |
import begin | |
# Enum values can be abused | |
class Choices(Enum): | |
foo = 1 | |
bar = 2.0 | |
baz = '03' | |
# Choices must be accessed as Choices.foo or Choices['foo'] | |
# Enums should be a special case so we can just use arg=Choices | |
@begin.start | |
@begin.convert(arg=Choices.__getitem__) | |
def main(arg=Choices.foo): | |
# Up to you whether you care about the name or the value | |
print('you chose Choices.{} -> {}'.format(arg.name, arg.value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment