5656"""
5757
5858
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+
5976def add_git_arguments (parser , * , commit = True , prefix = True ):
6077 # Add this whenever we add git arguments since we pull the remote and
6178 # branch from mkdocs.yml.
6279 parser .add_argument ('-F' , '--config-file' , metavar = 'FILE' ,
63- default = 'mkdocs.yml' ,
80+ default = 'mkdocs.yml' , complete = 'file' ,
6481 help = 'the MkDocs configuration file to use' )
6582
6683 git = parser .add_argument_group ('git arguments' )
@@ -78,6 +95,7 @@ def add_git_arguments(parser, *, commit=True, prefix=True):
7895
7996 if prefix :
8097 git .add_argument ('--prefix' , metavar = 'PATH' , default = '' ,
98+ complete = 'directory' ,
8199 help = ('subdirectory within {branch} where docs are ' +
82100 'located' ))
83101
@@ -227,7 +245,7 @@ def generate_completion(parser, args):
227245
228246
229247def main ():
230- parser = argparse . ArgumentParser (prog = 'mike' , description = description )
248+ parser = CompletingArgumentParser (prog = 'mike' , description = description )
231249 subparsers = parser .add_subparsers (metavar = 'COMMAND' )
232250 subparsers .required = True
233251
@@ -246,7 +264,7 @@ def main():
246264 deploy_p .add_argument ('--no-redirect' , dest = 'redirect' , default = True ,
247265 action = 'store_false' ,
248266 help = 'make copies of docs for each alias' )
249- deploy_p .add_argument ('-T' , '--template' ,
267+ deploy_p .add_argument ('-T' , '--template' , complete = 'file' ,
250268 help = 'the template file to use for redirects' )
251269 add_git_arguments (deploy_p )
252270 deploy_p .add_argument ('version' , metavar = 'VERSION' ,
@@ -273,7 +291,7 @@ def main():
273291 alias_p .add_argument ('--no-redirect' , dest = 'redirect' , default = True ,
274292 action = 'store_false' ,
275293 help = 'make copies of docs for each alias' )
276- alias_p .add_argument ('-T' , '--template' ,
294+ alias_p .add_argument ('-T' , '--template' , complete = 'file' ,
277295 help = 'the template file to use for redirects' )
278296 add_git_arguments (alias_p )
279297 alias_p .add_argument ('version' , metavar = 'VERSION' ,
@@ -307,7 +325,7 @@ def main():
307325 help = 'set the default version for your docs'
308326 )
309327 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' ,
311329 help = 'the template file to use' )
312330 add_git_arguments (set_default_p )
313331 set_default_p .add_argument ('version' , metavar = 'VERSION' ,
0 commit comments