Package logilab :: Package common :: Module optparser
[frames] | no frames]

Source Code for Module logilab.common.optparser

 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 under 
 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) any 
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  __docformat__ = "restructuredtext en" 
33   
34  from warnings import warn 
35  warn('lgc.optparser module is deprecated, use lgc.clcommands instead', DeprecationWarning, 
36       stacklevel=2) 
37   
38  import sys 
39  import optparse 
40   
41 -class OptionParser(optparse.OptionParser):
42
43 - def __init__(self, *args, **kwargs):
44 optparse.OptionParser.__init__(self, *args, **kwargs) 45 self._commands = {} 46 self.min_args, self.max_args = 0, 1
47
48 - def add_command(self, name, mod_or_funcs, help=''):
49 """name of the command, name of module or tuple of functions 50 (run, add_options) 51 """ 52 assert isinstance(mod_or_funcs, str) or isinstance(mod_or_funcs, tuple), \ 53 "mod_or_funcs has to be a module name or a tuple of functions" 54 self._commands[name] = (mod_or_funcs, help)
55
56 - def print_main_help(self):
57 optparse.OptionParser.print_help(self) 58 print '\ncommands:' 59 for cmdname, (_, help) in self._commands.items(): 60 print '% 10s - %s' % (cmdname, help)
61
62 - def parse_command(self, args):
63 if len(args) == 0: 64 self.print_main_help() 65 sys.exit(1) 66 cmd = args[0] 67 args = args[1:] 68 if cmd not in self._commands: 69 if cmd in ('-h', '--help'): 70 self.print_main_help() 71 sys.exit(0) 72 elif self.version is not None and cmd == "--version": 73 self.print_version() 74 sys.exit(0) 75 self.error('unknown command') 76 self.prog = '%s %s' % (self.prog, cmd) 77 mod_or_f, help = self._commands[cmd] 78 # optparse inserts self.description between usage and options help 79 self.description = help 80 if isinstance(mod_or_f, str): 81 exec 'from %s import run, add_options' % mod_or_f 82 else: 83 run, add_options = mod_or_f 84 add_options(self) 85 (options, args) = self.parse_args(args) 86 if not (self.min_args <= len(args) <= self.max_args): 87 self.error('incorrect number of arguments') 88 return run, options, args
89