reorx
12/18/2015 - 5:14 AM

Simple boilerplate for command line management script

Simple boilerplate for command line management script

#!/usr/bin/env python
# coding: utf-8

"""
Simple boilerplate for command line management script.
Usage: Define global functions and add their names in `CMD_LIST`.
"""

import sys


CMD_LIST = [
    'run',
]


# Define command functions here #

def run():
    from myproject.app import app

    app.run()


# End command functions #


def main():
    cmd_list_str = ', '.join("'%s'" % i for i in CMD_LIST)
    try:
        cmd = sys.argv[1]
    except IndexError:
        print 'Please pass a command, available commands:'
        print '  ' + cmd_list_str
        sys.exit(1)
    if cmd not in CMD_LIST:
        print "'%s' is not a valid command, available commands:" % cmd
        print '  ' + cmd_list_str
        sys.exit(1)

    globals()[cmd]()


if __name__ == "__main__":
    main()