Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Python library to build command line utilities with subcommands.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Readme.md
Octocat-spinner-32 example.py
Octocat-spinner-32 setup.py
Octocat-spinner-32 subcommand.py
Readme.md

subcommand: Python library for creating command line applicaitons with subcommands

Subcommand is a function decorated with subcommand.subcommand() decorator. Help message and options are specified in docstring.

Hello world example:

import subcommand

@subcommand.subcommand()
def hello(options, name='world'):
    """Display hello message.

    Usage: hello [options] [name]

    Options:

    -p [--prefix] prefix    : message prefix (default: hello)
    -n [--repeat] n         : repeat the message n times (type: int) (default: 1)
    """
    for i in range(options.n):
        print options.prefix + ",", name

if __name__ == "__main__":
    subcommand.main()

And here is the response.

$ python hello.py hello
hello, world
$ python hello.py hello subcommand
hello, subcommand
$ python hello.py hello -p bye subcommand
bye, subcommand
$ python hello.py hello --prefix bye subcommand
bye, subcommand
$ python hello.py hello -p bye -n 3 subcommand
bye, subcommand
bye, subcommand
bye, subcommand

$ python hello.py
usage: python hello.py <subcommand> [options] [args]

Available subcommands:
    hello       Displays hello message.
    help        Describe usage of this program and its subcommands.

anand@bodhi subcommand$ python hello.py help hello
hello: Displays hello message.

Usage: hello [options] [name]

Valid Options:
  -p [--prefix] prefix            : message prefix (default: hello)
  -n [--repeat] n                 : repeat the message n times (default: 1)
Something went wrong with that request. Please try again.