My favorites | Sign in
Logo
                
Code license: MIT License
Labels: python, cmd, cmdln, shell, script
Show all Featured downloads:
cmdln-1.1.2.zip
People details
Project owners:
  tre...@gmail.com
Project committers:
Sridhar....@gmail.com

cmdln.py fixes some of the design flaws in cmd.py and takes advantage of new Python stdlib modules (e.g. optparse) so that it is more useful (and convenient) for implementing command-line scripts/shells.

The main differences are:

Introduction

cmdln.py is an extension of Python's default cmd.py module that provides "a simple framework for writing line-oriented command interpreters". The idea (with both cmd.py and cmdln.py) is to be able to quickly build multi-sub-command tools (think cvs or svn) and/or simple interactive shells (think gdb or pdb). cmdln.py's extensions make it more natural to write sub-commands, integrate optparse for simple option processing, and make having good command documentation easier.

For example, here is most of the scaffolding for the svn status command. (Note: Some options were removed and the doc string truncated for brevity. See examples/svn.py for a more complete scaffold re-implementation of the svn command-line interface.)

#!/usr/bin/env python
import sys
import cmdln

class MySVN(cmdln.Cmdln):
    name = "svn"

    @cmdln.alias("stat", "st")
    @cmdln.option("-u", "--show-updates", action="store_true",
                  help="display update information")
    @cmdln.option("-v", "--verbose", action="store_true",
                  help="print extra information")
    def do_status(self, subcmd, opts, *paths):
        """${cmd_name}: print the status of working copy files and directories

        ${cmd_usage}
        ${cmd_option_list}
        """
        print "'svn %s' opts:  %s" % (subcmd, opts)
        print "'svn %s' paths: %s" % (subcmd, paths)


if __name__ == "__main__":
    svn = MySVN()
    sys.exit(svn.main())

The base cmdln.Cmdln class is providing a number of things for free here. (1) There is a reasonable default help string:

$ python svn.py
Usage:
    svn COMMAND [ARGS...]
    svn help COMMAND

commands:
    help (?)            give detailed help on a specific command
    status (st, stat)   print the status of working copy files and dire...

(2) A default help command is provided for getting detailed help on specific sub-commands. This is how many such tools already work (e.g. svn and p4, the command-line interface for the Perforce source control system).

$ python svn.py help status
status (stat, st): print the status of working copy files and directories.

Usage:
    svn status [PATHS...]

Options:
    -h, --help          show this help message and exit
    -v, --verbose       print extra information
    -u, --show-updates  display update information

(3) It makes parsing the command line easy (with optparse integration):

$ python svn.py status -v foo bar baz
'svn status' opts:  {'show_updates': None, 'verbose': True}
'svn status' paths: ('foo', 'bar', 'baz')

and (4) defining command aliases easy:

$ python svn.py st -v foo bar baz
'svn st' opts:  {'show_updates': None, 'verbose': True}
'svn st' paths: ('foo', 'bar', 'baz')

Read the GettingStarted page next.









Hosted by Google Code