Changeset 3316

Show
Ignore:
Timestamp:
02/02/08 14:05:12 (16 years ago)
Author:
jerome
Message:

Improved handling of --version and --help.
--help's help can now be translated as well.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/commandline.py

    r3315 r3316  
    3939        """ 
    4040        self.examples = [] 
     41        kwargs["version"] = "%s (PyKota) %s" % (os.path.basename(sys.argv[0]), 
     42                                                version.__version__) 
    4143        optparse.OptionParser.__init__(self, *args, **kwargs) 
    4244        self.disable_interspersed_args() 
     45        self.remove_version_and_help() 
    4346        self.add_generic_options() 
    4447         
     
    5659        return "".join(result) 
    5760             
    58     def parse_args(self, args=None, values=None) : 
    59         """Parses command line arguments, and handles -v|--version as well.""" 
    60         (options, arguments) = optparse.OptionParser.parse_args(self, args, values) 
    61         self.handle_generic_options(options) 
    62         return (options, arguments)     
    63          
    6461    #     
    6562    # Below are PyKota specific additions     
     
    9895        self.examples.append(("%prog " + command, doc)) 
    9996         
     97    def remove_version_and_help(self) :    
     98        """Removes the default definitions for options version and help.""" 
     99        for o in ("-h", "-help", "--help", "-v", "-version", "--version") : 
     100            try : 
     101                self.remove_option(o) 
     102            except ValueError :      
     103                pass 
     104                 
    100105    def add_generic_options(self) :     
    101106        """Adds options which are common to all PyKota command line tools.""" 
     107        self.add_option("-h", "--help", 
     108                              action="help", 
     109                              help=_("show this help message and exit")) 
    102110        self.add_option("-v", "--version", 
    103                               action="store_true", 
    104                               dest="version", 
     111                              action="version", 
    105112                              help=_("show the version number and exit")) 
    106          
    107     def handle_generic_options(self, options) :     
    108         """Handles options which are common to all PyKota command line tools.""" 
    109         if options.version : 
    110             sys.stdout.write("%s (PyKota) %s\n" % (os.path.basename(sys.argv[0]), 
    111                                                    version.__version__)) 
    112             sys.exit(0)