root / pykota / trunk / pykota / commandline.py @ 3316

Revision 3316, 4.3 kB (checked in by jerome, 16 years ago)

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3260]1# -*- coding: UTF-8 -*-
[695]2#
[3260]3# PyKota : Print Quotas for CUPS
[695]4#
[3275]5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
[3260]6# This program is free software: you can redistribute it and/or modify
[873]7# it under the terms of the GNU General Public License as published by
[3260]8# the Free Software Foundation, either version 3 of the License, or
[873]9# (at your option) any later version.
[3260]10#
[873]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
[3260]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[695]18#
19# $Id$
20#
[3304]21
22"""This modules defines a command line options parser for PyKota's command line tools."""
23
[3312]24import sys
25import os
[3304]26import optparse
27from gettext import gettext as _
28
29from pykota import version
30
31class PyKotaOptionParser(optparse.OptionParser) :
32    """
33    This class to define additional methods, and different help
34    formatting, from the traditional OptionParser.
35    """   
36    def __init__(self, *args, **kwargs) :
37        """
38        Initializes our option parser with additional attributes.
39        """
40        self.examples = []
[3316]41        kwargs["version"] = "%s (PyKota) %s" % (os.path.basename(sys.argv[0]),
42                                                version.__version__)
[3304]43        optparse.OptionParser.__init__(self, *args, **kwargs)
44        self.disable_interspersed_args()
[3316]45        self.remove_version_and_help()
[3315]46        self.add_generic_options()
[3304]47       
48    def format_help(self, formatter=None) :
49        """
50        Reformats help our way : adding examples and copyright
51        message at the end.
52        """
53        if formatter is None :
54            formatter = self.formatter
55        result = []
56        result.append(optparse.OptionParser.format_help(self, formatter) + "\n")
[3314]57        result.append(self.format_examples())
58        result.append(self.format_copyright())
[3304]59        return "".join(result)
[3314]60           
61    #   
62    # Below are PyKota specific additions   
63    #
[3304]64    def format_examples(self, formatter=None) :
65        """Formats examples our way."""
66        if formatter is None :
67            formatter = self.formatter
68        result = []
69        if self.examples :
70            result.append(formatter.format_heading(_("examples")))
71            formatter.indent()
72            for (cmd, explanation) in self.examples :
73                result.append(formatter.format_description(self.expand_prog_name(cmd)))
74                result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n")
75            formatter.dedent()
76        return "".join(result)   
77       
78    def format_copyright(self, formatter=None) :
79        """Formats copyright message our way."""
80        if formatter is None :
81            formatter = self.formatter
82        result = []   
83        result.append(formatter.format_heading(_("licensing terms")))
84        formatter.indent()
85        result.append(formatter.format_description("(c) %s %s\n" \
86                                                      % (version.__years__, \
87                                                         version.__author__)))
88        for part in version.__gplblurb__.split("\n\n") :
89            result.append(formatter.format_description(part) + "\n")
90        formatter.dedent()   
91        return "".join(result)
92       
93    def add_example(self, command, doc) :   
94        """Adds an usage example."""
95        self.examples.append(("%prog " + command, doc))
[3312]96       
[3316]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               
[3315]105    def add_generic_options(self) :   
106        """Adds options which are common to all PyKota command line tools."""
[3316]107        self.add_option("-h", "--help",
108                              action="help",
109                              help=_("show this help message and exit"))
[3315]110        self.add_option("-v", "--version",
[3316]111                              action="version",
[3315]112                              help=_("show the version number and exit"))
Note: See TracBrowser for help on using the browser.