summaryrefslogtreecommitdiff
path: root/optparser.py
blob: e5b0fd7f423bba405227438eb873bd359b761263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""Extend OptionParser with commands.

Example:

>>> parser = OptionParser()
>>> parser.usage = '%prog COMMAND [options] <arg> ...'
>>> parser.add_command('build', 'mymod.build')
>>> parser.add_command('clean', run_clean, add_opt_clean)
>>> run, options, args = parser.parse_command(sys.argv[1:])
>>> return run(options, args[1:])

With mymod.build that defines two functions run and add_options

:copyright: 2000-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: General Public License version 2 - http://www.gnu.org/licenses
"""
__docformat__ = "restructuredtext en"

# XXX merge with optik_ext ? merge with clcommands ?

import sys
import optparse

class OptionParser(optparse.OptionParser):

    def __init__(self, *args, **kwargs):
        optparse.OptionParser.__init__(self, *args, **kwargs)
        self._commands = {}
        self.min_args, self.max_args = 0, 1

    def add_command(self, name, mod_or_funcs, help=''):
        """name of the command
	name of module or tuple of functions (run, add_options)
	"""
        assert isinstance(mod_or_funcs, str) or isinstance(mod_or_funcs, tuple), \
	       "mod_or_funcs has to be a module name or a tuple of functions"
        self._commands[name] = (mod_or_funcs, help)

    def print_main_help(self):
        optparse.OptionParser.print_help(self)
        print '\ncommands:'
        for cmdname, (_, help) in self._commands.items():
            print '% 10s - %s' % (cmdname, help)

    def parse_command(self, args):
        if len(args) == 0:
            self.print_main_help()
            sys.exit(1)
        cmd = args[0]
	args = args[1:]
        if cmd not in self._commands:
            if cmd in ('-h', '--help'):
                self.print_main_help()
                sys.exit(0)
            elif self.version is not None and cmd == "--version":
                self.print_version()
                sys.exit(0)
            self.error('unknown command')
        self.prog = '%s %s' % (self.prog, cmd)
        mod_or_f, help = self._commands[cmd]
        # optparse inserts self.description between usage and options help
        self.description = help
        if isinstance(mod_or_f, str):
            exec 'from %s import run, add_options' % mod_or_f
	else:
	    run, add_options = mod_or_f
        add_options(self)
        (options, args) = self.parse_args(args)
        if not (self.min_args <= len(args) <= self.max_args):
            self.error('incorrect number of arguments')
        return run, options, args