reorx
10/7/2014 - 10:34 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 = [
    'test',
]


# Define command functions here #

def test():
    print 'command test'


# 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()