Let's make this a little easier on ourselves and just use a module...
Try: pip install compago
Then write:
import compago
app = compago.Application()
@app.command
def hello():
print "hi there!"
@app.command
def goodbye():
print "see ya later."
if __name__ == "__main__":
app.run()
Then use like so:
$ python test.py hello
hi there!
$ python test.py goodbye
see ya later.
Note: There's a bug in Python 3 at the moment, but works great with Python 2.
Edit: An even better option, in my opinion is the module fire by Google which makes it easy to also pass function arguments. It is installed with pip install fire
. From their GitHub:
Here's a simple example.
import fire
class Calculator(object):
"""A simple calculator class."""
def double(self, number):
return 2 * number
if __name__ == '__main__':
fire.Fire(Calculator)
Then, from the command line, you can run:
python calculator.py double 10 # 20
python calculator.py double --number=15 # 30