| 1 | # -*- coding: iso-8859-15 -*- |
|---|
| 2 | # Copyright (c) 2006 LOGILAB S.A. (Paris, FRANCE). |
|---|
| 3 | # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 6 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 7 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 8 | # version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 12 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License along with |
|---|
| 15 | # this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 16 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 17 | """Extend OptionParser with commands. |
|---|
| 18 | |
|---|
| 19 | Example: |
|---|
| 20 | |
|---|
| 21 | >>> parser = OptionParser() |
|---|
| 22 | >>> parser.usage = '%prog COMMAND [options] <arg> ...' |
|---|
| 23 | >>> parser.add_command('build', 'mymod.build') |
|---|
| 24 | >>> parser.add_command('clean', run_clean, add_opt_clean) |
|---|
| 25 | >>> run, options, args = parser.parse_command(sys.argv[1:]) |
|---|
| 26 | >>> return run(options, args[1:]) |
|---|
| 27 | |
|---|
| 28 | With mymod.build that defines two functions run and add_options |
|---|
| 29 | """ |
|---|
| 30 | |
|---|
| 31 | # XXX merge with optik_ext ? |
|---|
| 32 | |
|---|
| 33 | import sys |
|---|
| 34 | import optparse |
|---|
| 35 | |
|---|
| 36 | class OptionParser(optparse.OptionParser): |
|---|
| 37 | |
|---|
| 38 | def __init__(self, *args, **kwargs): |
|---|
| 39 | optparse.OptionParser.__init__(self, *args, **kwargs) |
|---|
| 40 | self._commands = {} |
|---|
| 41 | self.min_args, self.max_args = 0, 1 |
|---|
| 42 | |
|---|
| 43 | def add_command(self, name, mod_or_funcs, help=''): |
|---|
| 44 | """name of the command |
|---|
| 45 | name of module or tuple of functions (run, add_options) |
|---|
| 46 | """ |
|---|
| 47 | assert isinstance(mod_or_funcs, str) or isinstance(mod_or_funcs, tuple), \ |
|---|
| 48 | "mod_or_funcs has to be a module name or a tuple of functions" |
|---|
| 49 | self._commands[name] = (mod_or_funcs, help) |
|---|
| 50 | |
|---|
| 51 | def print_main_help(self): |
|---|
| 52 | optparse.OptionParser.print_help(self) |
|---|
| 53 | print '\ncommands:' |
|---|
| 54 | for cmdname, (_, help) in self._commands.items(): |
|---|
| 55 | print '% 10s - %s' % (cmdname, help) |
|---|
| 56 | |
|---|
| 57 | def parse_command(self, args): |
|---|
| 58 | if len(args) == 0: |
|---|
| 59 | self.print_main_help() |
|---|
| 60 | sys.exit(1) |
|---|
| 61 | cmd = args[0] |
|---|
| 62 | args = args[1:] |
|---|
| 63 | if cmd not in self._commands: |
|---|
| 64 | if cmd in ('-h', '--help'): |
|---|
| 65 | self.print_main_help() |
|---|
| 66 | sys.exit(0) |
|---|
| 67 | elif self.version is not None and cmd == "--version": |
|---|
| 68 | self.print_version() |
|---|
| 69 | sys.exit(0) |
|---|
| 70 | self.error('unknow command') |
|---|
| 71 | self.prog = '%s %s' % (self.prog, cmd) |
|---|
| 72 | mod_or_f, help = self._commands[cmd] |
|---|
| 73 | # optparse inserts self.description between usage and options help |
|---|
| 74 | self.description = help |
|---|
| 75 | if isinstance(mod_or_f, str): |
|---|
| 76 | exec 'from %s import run, add_options' % mod_or_f |
|---|
| 77 | else: |
|---|
| 78 | run, add_options = mod_or_f |
|---|
| 79 | add_options(self) |
|---|
| 80 | (options, args) = self.parse_args(args) |
|---|
| 81 | if not (self.min_args <= len(args) <= self.max_args): |
|---|
| 82 | self.error('incorrect number of arguments') |
|---|
| 83 | return run, options, args |
|---|
| 84 | |
|---|