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
Line 
1# -*- coding: UTF-8 -*-
2#
3# PyKota : Print Quotas for CUPS
4#
5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21
22"""This modules defines a command line options parser for PyKota's command line tools."""
23
24import sys
25import os
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()
43        self.add_generic_options()
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")
54        result.append(self.format_examples())
55        result.append(self.format_copyright())
56        return "".join(result)
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)   
63       
64    #   
65    # Below are PyKota specific additions   
66    #
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))
99       
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       
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)
Note: See TracBrowser for help on using the browser.