From: Frank Brehm Date: Sun, 17 Apr 2011 08:53:02 +0000 (+0000) Subject: Option parsing abgeschlossen X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=c44216eb72e40ee27b2c83735ed1c635cb9f5ac1;p=my-stuff%2Fpy-logrotate.git Option parsing abgeschlossen git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@200 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/LogRotateGetopts.py b/LogRotateGetopts.py index 06f82e7..930504c 100755 --- a/LogRotateGetopts.py +++ b/LogRotateGetopts.py @@ -34,6 +34,14 @@ __version__ = '0.0.1 ' + revision __license__ = 'GPL3' +#======================================================================== + +class LogrotateOptParserError(Exception): + ''' + Class for exceptions in this module, escpacially + due to false commandline options. + ''' + #======================================================================== class LogrotateOptParser(object): @@ -69,7 +77,7 @@ class LogrotateOptParser(object): @type: str ''' - self.description = 'rotates and compresses system logs' + self.description = 'Rotates and compresses system logs.' ''' @ivar: description of the program @type: str @@ -189,6 +197,18 @@ class LogrotateOptParser(object): # Deprecated options for compatibilty to logrotate group = OptionGroup(self.parser, "Deprecated options") + group.add_option( + '--mail', + '-m', + dest = "mailcmd", + metavar = 'CMD', + help = ( ( 'Should tell %s which command to use ' + + 'when mailing logs - not used.' ) + %(str(self.prog)) ), + ) + + self.parser.add_option_group(group) + ###### # Option group for common options @@ -228,6 +248,20 @@ class LogrotateOptParser(object): self.options, self.args = self.parser.parse_args() self.parsed = True + if self.options.force and self.options.configcheck: + raise LogrotateOptParserError('Invalid usage of --force and ' + + '--config-check.') + + if self.args is None: + raise LogrotateOptParserError('No configuration file given.') + + if len(self.args) != 1: + raise LogrotateOptParserError('Only one configuration file is allowed.') + + if self.options.mailcmd: + sys.stderr.write('Usage of --mail is deprecated ' + + 'in this version.\n\n') + #======================================================================== if __name__ == "__main__": diff --git a/logrotate.py b/logrotate.py index c64a75b..c570ef9 100755 --- a/logrotate.py +++ b/logrotate.py @@ -18,6 +18,7 @@ import sys import pprint from LogRotateGetopts import LogrotateOptParser; +from LogRotateGetopts import LogrotateOptParserError; revision = '$Revision$' revision = re.sub( r'\$', '', revision ) @@ -38,7 +39,12 @@ def main(): version = __version__, ) pp = pprint.PrettyPrinter(indent=4) - opt_parser.getOpts() + try: + opt_parser.getOpts() + except LogrotateOptParserError, e: + sys.stderr.write(str(e) + "\n\n") + opt_parser.parser.print_help(sys.stderr) + sys.exit(1) print "Options: " + pp.pformat(opt_parser.options) print "Arguments: " + pp.pformat(opt_parser.args)