Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/logilab/common/optparser.py

Issue 753543006: pylint: upgrade to 1.4.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/logilab/common/deprecation.py ('k') | third_party/logilab/common/pytest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4 # 4 #
5 # This file is part of logilab-common. 5 # This file is part of logilab-common.
6 # 6 #
7 # logilab-common is free software: you can redistribute it and/or modify it unde r 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 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 9 # Software Foundation, either version 2.1 of the License, or (at your option) an y
10 # later version. 10 # later version.
(...skipping 11 matching lines...) Expand all
22 22
23 >>> parser = OptionParser() 23 >>> parser = OptionParser()
24 >>> parser.usage = '%prog COMMAND [options] <arg> ...' 24 >>> parser.usage = '%prog COMMAND [options] <arg> ...'
25 >>> parser.add_command('build', 'mymod.build') 25 >>> parser.add_command('build', 'mymod.build')
26 >>> parser.add_command('clean', run_clean, add_opt_clean) 26 >>> parser.add_command('clean', run_clean, add_opt_clean)
27 >>> run, options, args = parser.parse_command(sys.argv[1:]) 27 >>> run, options, args = parser.parse_command(sys.argv[1:])
28 >>> return run(options, args[1:]) 28 >>> return run(options, args[1:])
29 29
30 With mymod.build that defines two functions run and add_options 30 With mymod.build that defines two functions run and add_options
31 """ 31 """
32 from __future__ import print_function
33
32 __docformat__ = "restructuredtext en" 34 __docformat__ = "restructuredtext en"
33 35
34 from warnings import warn 36 from warnings import warn
35 warn('lgc.optparser module is deprecated, use lgc.clcommands instead', Deprecati onWarning, 37 warn('lgc.optparser module is deprecated, use lgc.clcommands instead', Deprecati onWarning,
36 stacklevel=2) 38 stacklevel=2)
37 39
38 import sys 40 import sys
39 import optparse 41 import optparse
40 42
41 class OptionParser(optparse.OptionParser): 43 class OptionParser(optparse.OptionParser):
42 44
43 def __init__(self, *args, **kwargs): 45 def __init__(self, *args, **kwargs):
44 optparse.OptionParser.__init__(self, *args, **kwargs) 46 optparse.OptionParser.__init__(self, *args, **kwargs)
45 self._commands = {} 47 self._commands = {}
46 self.min_args, self.max_args = 0, 1 48 self.min_args, self.max_args = 0, 1
47 49
48 def add_command(self, name, mod_or_funcs, help=''): 50 def add_command(self, name, mod_or_funcs, help=''):
49 """name of the command, name of module or tuple of functions 51 """name of the command, name of module or tuple of functions
50 (run, add_options) 52 (run, add_options)
51 """ 53 """
52 assert isinstance(mod_or_funcs, str) or isinstance(mod_or_funcs, tuple), \ 54 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" 55 "mod_or_funcs has to be a module name or a tuple of functions"
54 self._commands[name] = (mod_or_funcs, help) 56 self._commands[name] = (mod_or_funcs, help)
55 57
56 def print_main_help(self): 58 def print_main_help(self):
57 optparse.OptionParser.print_help(self) 59 optparse.OptionParser.print_help(self)
58 print '\ncommands:' 60 print('\ncommands:')
59 for cmdname, (_, help) in self._commands.items(): 61 for cmdname, (_, help) in self._commands.items():
60 print '% 10s - %s' % (cmdname, help) 62 print('% 10s - %s' % (cmdname, help))
61 63
62 def parse_command(self, args): 64 def parse_command(self, args):
63 if len(args) == 0: 65 if len(args) == 0:
64 self.print_main_help() 66 self.print_main_help()
65 sys.exit(1) 67 sys.exit(1)
66 cmd = args[0] 68 cmd = args[0]
67 args = args[1:] 69 args = args[1:]
68 if cmd not in self._commands: 70 if cmd not in self._commands:
69 if cmd in ('-h', '--help'): 71 if cmd in ('-h', '--help'):
70 self.print_main_help() 72 self.print_main_help()
71 sys.exit(0) 73 sys.exit(0)
72 elif self.version is not None and cmd == "--version": 74 elif self.version is not None and cmd == "--version":
73 self.print_version() 75 self.print_version()
74 sys.exit(0) 76 sys.exit(0)
75 self.error('unknown command') 77 self.error('unknown command')
76 self.prog = '%s %s' % (self.prog, cmd) 78 self.prog = '%s %s' % (self.prog, cmd)
77 mod_or_f, help = self._commands[cmd] 79 mod_or_f, help = self._commands[cmd]
78 # optparse inserts self.description between usage and options help 80 # optparse inserts self.description between usage and options help
79 self.description = help 81 self.description = help
80 if isinstance(mod_or_f, str): 82 if isinstance(mod_or_f, str):
81 exec 'from %s import run, add_options' % mod_or_f 83 exec('from %s import run, add_options' % mod_or_f)
82 else: 84 else:
83 run, add_options = mod_or_f 85 run, add_options = mod_or_f
84 add_options(self) 86 add_options(self)
85 (options, args) = self.parse_args(args) 87 (options, args) = self.parse_args(args)
86 if not (self.min_args <= len(args) <= self.max_args): 88 if not (self.min_args <= len(args) <= self.max_args):
87 self.error('incorrect number of arguments') 89 self.error('incorrect number of arguments')
88 return run, options, args 90 return run, options, args
89 91
90 92
OLDNEW
« no previous file with comments | « third_party/logilab/common/deprecation.py ('k') | third_party/logilab/common/pytest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698