Skip to content

Instantly share code, notes, and snippets.

@chuangbo
Created January 11, 2011 16:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chuangbo/774683 to your computer and use it in GitHub Desktop.
chuangbo's decorator
#!/usr/bin/python
# -*- coding: utf8 -*-
_actions = []
def action(f):
"""Decorator to register an action."""
_actions.append(f)
return f
@action
def install():
"""Setup everything."""
pass
@action
def help(name=None):
"""Show this help."""
a = name and find_action(name)
print "Project xxx Help"
print ""
if a:
print " %s\t%s" % (a.__name__, a.__doc__)
else:
print "Available actions"
for a in _actions:
print " %s\t%s" % (a.__name__, a.__doc__)
def find_action(name):
for action in _actions:
if action.__name__ == name:
return action
def run_action(name, args):
a = find_action(name)
if a:
a(*args)
else:
print >> sys.stderr, "unknown command", name
help()
if __name__ == '__main__':
import sys
run_action(sys.argv[1], sys.argv[2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment