import os
import sys
from optparse import OptionParser

class Command:
    description = None
    usage = "Usage: %prog [options] [args]"

    def __init__(self):
        self.parser = OptionParser(usage=self.usage, description=self.description)

    def parse(self, args):
        self.options, self.args = self.parser.parse_args(args)

    def run(self):
        pass

    def main(self, args=None):
        self.parse(args)
        self.run()

class CompoundCommand(Command):
    "A Command with multiple sub-commands"
    description = None
    usage = "%prog COMMAND [options] [args]"
    commands = []
    aliases = {}

    def __init__(self):
        Command.__init__(self)

        lines = [
            "\n\nCommands:"
        ]
        for cmd, cmdclass in self.commands:
            lines.append("    %-10s %s" % (cmd, cmdclass.description))

        self.parser.usage += "\n".join(lines)

    def main(self, args=None):
        if args is None:
            args = sys.argv[1:]

        command = None
        for arg in args:
            if arg[0] != "-":
                command = arg
                args.remove(command)
                break

        if command is None and ("-h" in args or "--help" in args):
            self.parser.print_help()
            exit(1)

        if command is None:
            self.parser.error("Specify COMMAND")
            exit(1)

        commandmap = dict(self.commands)

        if command in self.aliases:
            command = self.aliases[command]

        if command not in commandmap:
            sys.stderr.write("Invalid command: %s\n" % command)
            exit(1)

        cmdclass = commandmap[command]
        cmd = cmdclass()
        cmd.main(args)


@@ -14,16 +14,31 @@
 ort sys%0A
+import logging%0A
 from opt
@@ -64,16 +64,51 @@
 Parser%0A%0A
+log = logging.getLogger(__name__)%0A%0A
 class Co
@@ -112,16 +112,24 @@
  Command
+(object)
 :%0A    de
@@ -478,16 +478,16 @@
 e(args)%0A
-
         
@@ -498,16 +498,940 @@
 .run()%0A%0A
+class LoggingCommand(Command):%0A    def __init__(self):%0A        Command.__init__(self)%0A        self.parser.add_option(%22-v%22, %22--verbose%22, action=%22count%22, default=0, dest=%22verbosity%22,%0A                               help=%22Increase logging verbosity, repeatable%22)%0A%0A    def main(self, args=None):%0A        self.parse(args)%0A%0A        verbosity = self.options.verbosity%0A%0A        if verbosity == 0:%0A            log_level = logging.WARNING%0A        elif verbosity == 1:%0A            log_level = logging.INFO%0A        elif verbosity %3E= 2:%0A            log_level = logging.DEBUG%0A%0A        logging.basicConfig(level=log_level)%0A        logging.getLogger().setLevel(log_level)%0A%0A        try:%0A            self.run()%0A        except Exception, e:%0A            # Only log stack grace if -v has been used%0A            if verbosity %3E= 1:%0A                log.exception(e)%0A            else:%0A                sys.stderr.write(%22%25s%5Cn%22 %25 e)%0A            exit(1)%0A%0A
 class Co

Add LoggingCommand