] > command.py (Logilab.org)

File command.py

name
command.py
download

#!/usr/bin/python

# Copyright (c) 1999-2005 Red Hat, Inc. Distributed under GPL. # # Author: Adrian Likins <alikins@redhat.com>

"""command.py includes the base class for each of the command modes for up2date. These modes include things like "list", "query", "update", etc. """

import sys try: # Optik is deprecated since it is part of python (but it is called # optparse) from optparse import OptionParser, Option except ImportError: from optik import OptionParser, Option

# put this in it's own module? class CLIOptionParser(OptionParser): def __init__(self, *args, **kwargs): apply(OptionParser.__init__, (self, ) + args, kwargs)

def error(self, msg): self.print_usage(sys.stderr) sys.stderr.write("%s: error: %s\n" % (sys.argv[0], msg)) # Otherwise, just exit sys.exit(1)

class Command(object): """Base class for all the "command" objects (list, install, remove, query, etc) """

def __init__(self, passed_args, optionstable = []): # list of strings

self.passed_args = passed_args if optionstable: self.optionstable = optionstable # self.optionstable is set in the derived classes

self.optparser = CLIOptionParser(option_list=self.optionstable)

# FIXME: exception here? (self.options, self.args) = self.optparser.parse_args(self.passed_args)

def getHelpString(self): "return the help string for this particular command" pass

def getExamplesString(self): "return the string of examples for this particular command" pass

def getValidOptions(self): "return a list of options supported by this class" pass

def getValidArgs(self): "return a list of arguments supported by this class" pass

def getOptions(self): "return the parses options" return self.options

def getArgs(self): "return the parsed args" return self.args

download