From: Frank Brehm Date: Sat, 16 Apr 2011 08:36:53 +0000 (+0000) Subject: Mit option parser weitergemacht X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=4029b3ae92f1d97d070e9cd7391e0cb5a90b696e;p=my-stuff%2Fpy-logrotate.git Mit option parser weitergemacht git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@199 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/LogRotateGetopts.py b/LogRotateGetopts.py index de7e082..06f82e7 100755 --- a/LogRotateGetopts.py +++ b/LogRotateGetopts.py @@ -18,6 +18,7 @@ import sys from optparse import OptionError from optparse import OptionParser +from optparse import OptionGroup from optparse import OptionConflictError @@ -46,8 +47,6 @@ class LogrotateOptParser(object): #------------------------------------------------------- def __init__( self, prog = '%prog', version = None, - description = 'rotates and compresses system logs', - usage = 'Usage: %s [options]', ): ''' Costructor. @@ -55,10 +54,6 @@ class LogrotateOptParser(object): @type prog: str @param version: The version string to use @type version: str - @param description: The Description the process should use - @type description: str - @param usage: An usage string fro the help screen, must have a '%s' for the program name - @type usage: str @return: None ''' @@ -68,19 +63,19 @@ class LogrotateOptParser(object): @type: str ''' - self.version = __version__ + self.version = version ''' @ivar: The version string to use @type: str ''' - self.description = description + self.description = 'rotates and compresses system logs' ''' @ivar: description of the program @type: str ''' - self.usage = usage %(prog) + self.usage = "Usage: %s [options] " %(prog) ''' @ivar: the usage string in getopt help output @type: str @@ -109,10 +104,11 @@ class LogrotateOptParser(object): self.version = version self.parser = OptionParser( - prog = self.prog, - version = self.version, - description = self.description, - usage = self.usage + prog = self.prog, + version = self.version, + description = self.description, + usage = self.usage, + conflict_handler = "resolve", ) ''' @ivar: the working OptionParser Object @@ -128,6 +124,12 @@ class LogrotateOptParser(object): to the OptionParser object ''' + if self.parser.has_option('--help'): + self.parser.remove_option('--help') + + if self.parser.has_option('--version'): + self.parser.remove_option('--version') + self.parser.add_option( '--simulate', '--test', @@ -147,6 +149,73 @@ class LogrotateOptParser(object): help = 'set the verbosity level' ) + self.parser.add_option( + '--debug', + '-d', + default = False, + action = 'store_true', + dest = 'debug', + help = "Don't do anything, just test (implies -v and -T)" + ) + + self.parser.add_option( + '--force', + '-f', + default = False, + action = 'store_true', + dest = 'force', + help = "Force file rotation" + ) + + self.parser.add_option( + '--config-check', + '-c', + default = False, + action = 'store_true', + dest = 'configcheck', + help = "Checks only the given configuration file and does " + + "nothing. Conflicts with -f", + ) + + self.parser.add_option( + '--state', + '-s', + dest = "statefile", + metavar = 'FILE', + help = 'Path of state file (different to configuration)', + ) + + ###### + # Deprecated options for compatibilty to logrotate + group = OptionGroup(self.parser, "Deprecated options") + + ###### + # Option group for common options + + group = OptionGroup(self.parser, "Common options") + + group.add_option( + '-h', + '-?', + '--help', + '--usage', + default = False, + action = 'help', + dest = 'help', + help = 'shows a help message and exit' + ) + + group.add_option( + '-V', + '--version', + default = False, + action = 'version', + dest = 'version', + help = 'shows the version number of the program and exit', + ) + + self.parser.add_option_group(group) + #---------------------------------------------------------------------- def getOpts(self): ''' diff --git a/logrotate.py b/logrotate.py index e165a22..c64a75b 100755 --- a/logrotate.py +++ b/logrotate.py @@ -14,6 +14,8 @@ ''' import re +import sys +import pprint from LogRotateGetopts import LogrotateOptParser; @@ -30,8 +32,17 @@ __license__ = 'GPL3' #----------------------------------------------------------------- def main(): - print "Lege los ..." - pass + + opt_parser = LogrotateOptParser( + prog = "logrotate", + version = __version__, + ) + pp = pprint.PrettyPrinter(indent=4) + opt_parser.getOpts() + + print "Options: " + pp.pformat(opt_parser.options) + print "Arguments: " + pp.pformat(opt_parser.args) + #========================================================================