#!/usr/bin/env python
# Copyright (c) 2002-2007 ActiveState Software Inc.
# License: MIT (see LICENSE.txt for license details)
# Author: Trent Mick
# Home: http://trentm.com/projects/cmdln/
"""An improvement on Python's standard cmd.py module.
As with cmd.py, this module provides "a simple framework for writing
line-oriented command intepreters." This module provides a 'RawCmdln'
class that fixes some design flaws in cmd.Cmd, making it more scalable
and nicer to use for good 'cvs'- or 'svn'-style command line interfaces
or simple shells. And it provides a 'Cmdln' class that add
optparse-based option processing. Basically you use it like this:
import cmdln
class MySVN(cmdln.Cmdln):
name = "svn"
@cmdln.alias('stat', 'st')
@cmdln.option('-v', '--verbose', action='store_true'
help='print verbose information')
def do_status(self, subcmd, opts, *paths):
print "handle 'svn status' command"
#...
if __name__ == "__main__":
shell = MySVN()
retval = shell.main()
sys.exit(retval)
See the README.txt or <http://trentm.com/projects/cmdln/> for more
details.
"""
__version_info__ = (1, 1, 2)
__version__ = '.'.join(map(str, __version_info__))
import os
import sys
import re
import cmd
import optparse
from pprint import pprint
import sys
#---- globals
LOOP_ALWAYS, LOOP_NEVER, LOOP_IF_EMPTY = range(3)
# An unspecified optional argument when None is a meaningful value.
_NOT_SPECIFIED = ("Not", "Specified")
# Pattern to match a TypeError message from a call that
# failed because of incorrect number of arguments (see
# Python/getargs.c).
_INCORRECT_NUM_ARGS_RE = re.compile(
r"(takes [\w ]+ )(\d+)( arguments? \()(\d+)( given\))")
#---- exceptions
class CmdlnError(Exception):
"""A cmdln.py usage error."""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class CmdlnUserError(Exception):
"""An error by a user of a cmdln-based tool/shell."""
pass
#---- public methods and classes
def alias(*aliases):
"""Decorator to add aliases for Cmdln.do_* command handlers.
Example:
class MyShell(cmdln.Cmdln):
@cmdln.alias("!", "sh")
def do_shell(self, argv):
#...implement 'shell' command
"""
def decorate(f):
if not
|