OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 4 # |
| 5 # This file is part of logilab-common. |
| 6 # |
| 7 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
| 8 # the terms of the GNU Lesser General Public License as published by the Free |
| 9 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
| 10 # later version. |
| 11 # |
| 12 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
| 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 15 # details. |
| 16 # |
| 17 # You should have received a copy of the GNU Lesser General Public License along |
| 18 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
| 19 """Extend OptionParser with commands. |
| 20 |
| 21 Example: |
| 22 |
| 23 >>> parser = OptionParser() |
| 24 >>> parser.usage = '%prog COMMAND [options] <arg> ...' |
| 25 >>> parser.add_command('build', 'mymod.build') |
| 26 >>> parser.add_command('clean', run_clean, add_opt_clean) |
| 27 >>> run, options, args = parser.parse_command(sys.argv[1:]) |
| 28 >>> return run(options, args[1:]) |
| 29 |
| 30 With mymod.build that defines two functions run and add_options |
| 31 """ |
| 32 from __future__ import print_function |
| 33 |
| 34 __docformat__ = "restructuredtext en" |
| 35 |
| 36 from warnings import warn |
| 37 warn('lgc.optparser module is deprecated, use lgc.clcommands instead', Deprecati
onWarning, |
| 38 stacklevel=2) |
| 39 |
| 40 import sys |
| 41 import optparse |
| 42 |
| 43 class OptionParser(optparse.OptionParser): |
| 44 |
| 45 def __init__(self, *args, **kwargs): |
| 46 optparse.OptionParser.__init__(self, *args, **kwargs) |
| 47 self._commands = {} |
| 48 self.min_args, self.max_args = 0, 1 |
| 49 |
| 50 def add_command(self, name, mod_or_funcs, help=''): |
| 51 """name of the command, name of module or tuple of functions |
| 52 (run, add_options) |
| 53 """ |
| 54 assert isinstance(mod_or_funcs, str) or isinstance(mod_or_funcs, tuple),
\ |
| 55 "mod_or_funcs has to be a module name or a tuple of functions" |
| 56 self._commands[name] = (mod_or_funcs, help) |
| 57 |
| 58 def print_main_help(self): |
| 59 optparse.OptionParser.print_help(self) |
| 60 print('\ncommands:') |
| 61 for cmdname, (_, help) in self._commands.items(): |
| 62 print('% 10s - %s' % (cmdname, help)) |
| 63 |
| 64 def parse_command(self, args): |
| 65 if len(args) == 0: |
| 66 self.print_main_help() |
| 67 sys.exit(1) |
| 68 cmd = args[0] |
| 69 args = args[1:] |
| 70 if cmd not in self._commands: |
| 71 if cmd in ('-h', '--help'): |
| 72 self.print_main_help() |
| 73 sys.exit(0) |
| 74 elif self.version is not None and cmd == "--version": |
| 75 self.print_version() |
| 76 sys.exit(0) |
| 77 self.error('unknown command') |
| 78 self.prog = '%s %s' % (self.prog, cmd) |
| 79 mod_or_f, help = self._commands[cmd] |
| 80 # optparse inserts self.description between usage and options help |
| 81 self.description = help |
| 82 if isinstance(mod_or_f, str): |
| 83 exec('from %s import run, add_options' % mod_or_f) |
| 84 else: |
| 85 run, add_options = mod_or_f |
| 86 add_options(self) |
| 87 (options, args) = self.parse_args(args) |
| 88 if not (self.min_args <= len(args) <= self.max_args): |
| 89 self.error('incorrect number of arguments') |
| 90 return run, options, args |
| 91 |
| 92 |
OLD | NEW |