Changeset 3304

Show
Ignore:
Timestamp:
01/29/08 23:51:36 (16 years ago)
Author:
jerome
Message:

Added the skeleton for an improved command line parser.

Location:
pykota/trunk/pykota
Files:
1 added
1 modified
2 copied

Legend:

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

    r3275 r3304  
    1919# $Id$ 
    2020# 
     21 
     22"""This modules defines a command line options parser for PyKota's command line tools.""" 
     23 
     24import optparse 
     25from gettext import gettext as _ 
     26 
     27from pykota import version 
     28 
     29class PyKotaOptionParser(optparse.OptionParser) : 
     30    """ 
     31    This class to define additional methods, and different help 
     32    formatting, from the traditional OptionParser. 
     33    """    
     34    def __init__(self, *args, **kwargs) : 
     35        """ 
     36        Initializes our option parser with additional attributes. 
     37        """ 
     38        self.examples = [] 
     39        optparse.OptionParser.__init__(self, *args, **kwargs) 
     40        self.disable_interspersed_args() 
     41        self.add_option("-", "--version", 
     42                             action="store_true", 
     43                             dest="version", 
     44                             help=_("show %prog's version number and exit.")) 
     45         
     46    def format_help(self, formatter=None) : 
     47        """ 
     48        Reformats help our way : adding examples and copyright 
     49        message at the end. 
     50        """ 
     51        if formatter is None : 
     52            formatter = self.formatter 
     53        result = [] 
     54        result.append(optparse.OptionParser.format_help(self, formatter) + "\n") 
     55        result.append(self.format_examples() + "\n") 
     56        result.append(self.format_copyright() + "\n") 
     57        return "".join(result) 
     58         
     59    def format_examples(self, formatter=None) : 
     60        """Formats examples our way.""" 
     61        if formatter is None : 
     62            formatter = self.formatter 
     63        result = [] 
     64        if self.examples : 
     65            result.append(formatter.format_heading(_("examples"))) 
     66            formatter.indent() 
     67            for (cmd, explanation) in self.examples : 
     68                result.append(formatter.format_description(self.expand_prog_name(cmd))) 
     69                result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n") 
     70            formatter.dedent() 
     71        return "".join(result)     
     72         
     73    def format_copyright(self, formatter=None) : 
     74        """Formats copyright message our way.""" 
     75        if formatter is None : 
     76            formatter = self.formatter 
     77        result = []     
     78        result.append(formatter.format_heading(_("licensing terms"))) 
     79        formatter.indent() 
     80        result.append(formatter.format_description("(c) %s %s\n" \ 
     81                                                      % (version.__years__, \ 
     82                                                         version.__author__))) 
     83        for part in version.__gplblurb__.split("\n\n") : 
     84            result.append(formatter.format_description(part) + "\n") 
     85        formatter.dedent()     
     86        return "".join(result) 
     87         
     88    def add_example(self, command, doc) :     
     89        """Adds an usage example.""" 
     90        self.examples.append(("%prog " + command, doc)) 
  • pykota/trunk/pykota/version.py

    r3298 r3304  
    2828__author__ = "Jerome Alet - alet@librelogiciel.com" 
    2929 
    30 __years__ = "2003, 2004, 2005, 2006, 2007, 2008" 
     30__years__ = "2003-2008" 
    3131 
    32 __gplblurb__ = """(c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> 
    33 This program is free software: you can redistribute it and/or modify 
     32__gplblurb__ = """This program is free software: you can redistribute it and/or modify 
    3433it under the terms of the GNU General Public License as published by 
    3534the Free Software Foundation, either version 3 of the License, or