Created: 2011-07-09 00:14
Updated: 2014-10-10 20:30

Command Line Runner

A Python module for easily getting the arguments from the command line, and selecting which command to run. Way simpler than optparse or argparse modules, use it when you don't the full power of those libraries.


Subclass the CLIRunner class. Add methods to expose as commands. If you don't want to expose a particular method, prefix it by underscore. If the method name contains an underscore somewhere in the middle, the corresponding command will have a hypen instead.

Docstrings are used for help. Set the class docstring for the app/binary overview, and the method docstrings for the command help. Use only positional arguments (*args) in methods. The argument names will be shown (in uppercase) in the help output.

The help output is provided by the method. You can override it if you want to customise it further.

To activate the wrapper, just create a new object of your subclass.


The code:

from clirunner import CLIRunner

class HelloWorld(CLIRunner):
    "A simple Hello World application."

    def hello_world(self):
        "Print standard greeting."
        print "Hello World!"

    def hello(self, name):
        "Greets you by name."
        print "Hello, %s!" % name

if __name__ == '__main__':

End result:

$ python
HelloWorld - A simple Hello World application.
Usage: COMMAND [ARGS...]
  hello NAME - Greets you by name.
  hello-world - Print standard greeting.
  help - Show this help.

$ python hello
Command 'hello' needs 1 arguments, 0 given.
Usage: hello NAME

$ python hello-world
Hello World!


Licensed under MIT license. Forks and pull-requests (and bug reports) are welcome.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more