Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@evanunderscore
Last active January 23, 2016 11:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanunderscore/3a621f3f5c810b1444c7 to your computer and use it in GitHub Desktop.
Save evanunderscore/3a621f3f5c810b1444c7 to your computer and use it in GitHub Desktop.
#!/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