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

Revision 3314, 4.2 kB (checked in by jerome, 16 years ago)

Improved readability.

  • 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 = []
41        optparse.OptionParser.__init__(self, *args, **kwargs)
42        self.disable_interspersed_args()
[3305]43        self.add_option("-v", "--version",
44                              action="store_true",
45                              dest="version",
46                              help=_("show %prog's version number and exit."))
[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    def parse_args(self, args=None, values=None) :
62        """Parses command line arguments, and handles -v|--version as well."""
63        (options, arguments) = optparse.OptionParser.parse_args(self, args, values)
64        self.handle_generic_options(options)
65        return (options, arguments)   
[3304]66       
[3314]67    #   
68    # Below are PyKota specific additions   
69    #
[3304]70    def format_examples(self, formatter=None) :
71        """Formats examples our way."""
72        if formatter is None :
73            formatter = self.formatter
74        result = []
75        if self.examples :
76            result.append(formatter.format_heading(_("examples")))
77            formatter.indent()
78            for (cmd, explanation) in self.examples :
79                result.append(formatter.format_description(self.expand_prog_name(cmd)))
80                result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n")
81            formatter.dedent()
82        return "".join(result)   
83       
84    def format_copyright(self, formatter=None) :
85        """Formats copyright message our way."""
86        if formatter is None :
87            formatter = self.formatter
88        result = []   
89        result.append(formatter.format_heading(_("licensing terms")))
90        formatter.indent()
91        result.append(formatter.format_description("(c) %s %s\n" \
92                                                      % (version.__years__, \
93                                                         version.__author__)))
94        for part in version.__gplblurb__.split("\n\n") :
95            result.append(formatter.format_description(part) + "\n")
96        formatter.dedent()   
97        return "".join(result)
98       
99    def add_example(self, command, doc) :   
100        """Adds an usage example."""
101        self.examples.append(("%prog " + command, doc))
[3312]102       
[3314]103    def handle_generic_options(self, options) :   
104        """Handles options which are common to all PyKota command line tools."""
[3312]105        if options.version :
106            sys.stdout.write("%s (PyKota) %s\n" % (os.path.basename(sys.argv[0]),
107                                                   version.__version__))
108            sys.exit(0)
Note: See TracBrowser for help on using the browser.