25
25
argparse ._CountAction )
26
26
27
27
28
+ try :
29
+ import argcomplete
30
+ ARGCOMLETE_IMPORTED = True
31
+ except ImportError :
32
+ ARGCOMLETE_IMPORTED = False
33
+
34
+
28
35
class Manager (object ):
29
36
"""
30
37
Controller class for handling a set of commands.
@@ -52,9 +59,12 @@ def run(self):
52
59
:param app: Flask instance or callable returning a Flask instance.
53
60
:param with_default_commands: load commands **runserver** and **shell**
54
61
by default.
62
+ :param disable_argcomplete: disable automatic loading of argcomplete.
63
+
55
64
"""
56
65
57
- def __init__ (self , app = None , with_default_commands = None , usage = None ):
66
+ def __init__ (self , app = None , with_default_commands = None , usage = None ,
67
+ disable_argcomplete = False ):
58
68
59
69
self .app = app
60
70
@@ -67,6 +77,7 @@ def __init__(self, app=None, with_default_commands=None, usage=None):
67
77
self .add_default_commands ()
68
78
69
79
self .usage = self .description = usage
80
+ self .disable_argcomplete = disable_argcomplete
70
81
71
82
self .parent = None
72
83
@@ -135,27 +146,31 @@ def create_parser(self, prog, parents=None):
135
146
for option in self .get_options ():
136
147
option_parser .add_argument (* option .args , ** option .kwargs )
137
148
138
- if parents is None :
139
- parents = [option_parser ]
149
+ parser_parents = [option_parser ] if parents is None else parents
140
150
141
151
def _create_command (item ):
142
152
name , command = item
143
153
description = getattr (command , 'description' ,
144
154
command .__doc__ )
145
155
return name , command , description , \
146
- command .create_parser (name , parents = parents )
156
+ command .create_parser (name , parents = parser_parents )
147
157
148
158
commands = map (_create_command , self ._commands .iteritems ())
149
159
150
160
parser = argparse .ArgumentParser (prog = prog , usage = self .usage ,
151
- parents = parents )
152
-
153
- #parser.set_defaults(func_handle=self._handle)
161
+ parents = parser_parents )
154
162
155
163
subparsers = parser .add_subparsers ()
156
164
for name , command , description , parent in commands :
157
165
subparsers .add_parser (name , usage = description , help = description ,
158
166
parents = [parent ], add_help = False )
167
+
168
+ ## enable autocomplete only for parent parser when argcomplete is
169
+ ## imported and it is NOT disabled in constructor
170
+ if parents is None and ARGCOMLETE_IMPORTED \
171
+ and not self .disable_argcomplete :
172
+ argcomplete .autocomplete (parser , always_complete_options = True )
173
+
159
174
return parser
160
175
161
176
def get_options (self ):
@@ -365,6 +380,7 @@ def handle(self, prog, args=None):
365
380
positional_args = []
366
381
367
382
app = self .create_app (** app_config )
383
+
368
384
return handle (app , * positional_args , ** kwargs )
369
385
370
386
def run (self , commands = None , default_command = None ):
0 commit comments