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

Revision 3315, 4.4 kB (checked in by jerome, 16 years ago)

Added a specific method to add all generic options.

  • 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()
[3315]43        self.add_generic_options()
[3304]44       
45    def format_help(self, formatter=None) :
46        """
47        Reformats help our way : adding examples and copyright
48        message at the end.
49        """
50        if formatter is None :
51            formatter = self.formatter
52        result = []
53        result.append(optparse.OptionParser.format_help(self, formatter) + "\n")
[3314]54        result.append(self.format_examples())
55        result.append(self.format_copyright())
[3304]56        return "".join(result)
[3314]57           
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)   
[3304]63       
[3314]64    #   
65    # Below are PyKota specific additions   
66    #
[3304]67    def format_examples(self, formatter=None) :
68        """Formats examples our way."""
69        if formatter is None :
70            formatter = self.formatter
71        result = []
72        if self.examples :
73            result.append(formatter.format_heading(_("examples")))
74            formatter.indent()
75            for (cmd, explanation) in self.examples :
76                result.append(formatter.format_description(self.expand_prog_name(cmd)))
77                result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n")
78            formatter.dedent()
79        return "".join(result)   
80       
81    def format_copyright(self, formatter=None) :
82        """Formats copyright message our way."""
83        if formatter is None :
84            formatter = self.formatter
85        result = []   
86        result.append(formatter.format_heading(_("licensing terms")))
87        formatter.indent()
88        result.append(formatter.format_description("(c) %s %s\n" \
89                                                      % (version.__years__, \
90                                                         version.__author__)))
91        for part in version.__gplblurb__.split("\n\n") :
92            result.append(formatter.format_description(part) + "\n")
93        formatter.dedent()   
94        return "".join(result)
95       
96    def add_example(self, command, doc) :   
97        """Adds an usage example."""
98        self.examples.append(("%prog " + command, doc))
[3312]99       
[3315]100    def add_generic_options(self) :   
101        """Adds options which are common to all PyKota command line tools."""
102        self.add_option("-v", "--version",
103                              action="store_true",
104                              dest="version",
105                              help=_("show the version number and exit"))
106       
[3314]107    def handle_generic_options(self, options) :   
108        """Handles options which are common to all PyKota command line tools."""
[3312]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)
Note: See TracBrowser for help on using the browser.