56
56
"""
57
57
58
58
59
+ class CompletingArgumentParser (argparse .ArgumentParser ):
60
+ @staticmethod
61
+ def _wrap_complete (action ):
62
+ def wrapper (* args , complete = None , ** kwargs ):
63
+ argument = action (* args , ** kwargs )
64
+ if complete is not None :
65
+ argument .complete = complete
66
+ return argument
67
+
68
+ return wrapper
69
+
70
+ def __init__ (self , * args , ** kwargs ):
71
+ super ().__init__ (* args , ** kwargs )
72
+ for k , v in self ._registries ['action' ].items ():
73
+ self ._registries ['action' ][k ] = self ._wrap_complete (v )
74
+
75
+
59
76
def add_git_arguments (parser , * , commit = True , prefix = True ):
60
77
# Add this whenever we add git arguments since we pull the remote and
61
78
# branch from mkdocs.yml.
62
79
parser .add_argument ('-F' , '--config-file' , metavar = 'FILE' ,
63
- default = 'mkdocs.yml' ,
80
+ default = 'mkdocs.yml' , complete = 'file' ,
64
81
help = 'the MkDocs configuration file to use' )
65
82
66
83
git = parser .add_argument_group ('git arguments' )
@@ -78,6 +95,7 @@ def add_git_arguments(parser, *, commit=True, prefix=True):
78
95
79
96
if prefix :
80
97
git .add_argument ('--prefix' , metavar = 'PATH' , default = '' ,
98
+ complete = 'directory' ,
81
99
help = ('subdirectory within {branch} where docs are ' +
82
100
'located' ))
83
101
@@ -227,7 +245,7 @@ def generate_completion(parser, args):
227
245
228
246
229
247
def main ():
230
- parser = argparse . ArgumentParser (prog = 'mike' , description = description )
248
+ parser = CompletingArgumentParser (prog = 'mike' , description = description )
231
249
subparsers = parser .add_subparsers (metavar = 'COMMAND' )
232
250
subparsers .required = True
233
251
@@ -246,7 +264,7 @@ def main():
246
264
deploy_p .add_argument ('--no-redirect' , dest = 'redirect' , default = True ,
247
265
action = 'store_false' ,
248
266
help = 'make copies of docs for each alias' )
249
- deploy_p .add_argument ('-T' , '--template' ,
267
+ deploy_p .add_argument ('-T' , '--template' , complete = 'file' ,
250
268
help = 'the template file to use for redirects' )
251
269
add_git_arguments (deploy_p )
252
270
deploy_p .add_argument ('version' , metavar = 'VERSION' ,
@@ -273,7 +291,7 @@ def main():
273
291
alias_p .add_argument ('--no-redirect' , dest = 'redirect' , default = True ,
274
292
action = 'store_false' ,
275
293
help = 'make copies of docs for each alias' )
276
- alias_p .add_argument ('-T' , '--template' ,
294
+ alias_p .add_argument ('-T' , '--template' , complete = 'file' ,
277
295
help = 'the template file to use for redirects' )
278
296
add_git_arguments (alias_p )
279
297
alias_p .add_argument ('version' , metavar = 'VERSION' ,
@@ -307,7 +325,7 @@ def main():
307
325
help = 'set the default version for your docs'
308
326
)
309
327
set_default_p .set_defaults (func = set_default )
310
- set_default_p .add_argument ('-T' , '--template' ,
328
+ set_default_p .add_argument ('-T' , '--template' , complete = 'file' ,
311
329
help = 'the template file to use' )
312
330
add_git_arguments (set_default_p )
313
331
set_default_p .add_argument ('version' , metavar = 'VERSION' ,
0 commit comments