root / pykota / trunk / pykota / commandline / parser.py @ 3312

Revision 3312, 4.0 kB (checked in by jerome, 16 years ago)

Now handles -v|--version automatically at parsing time.

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