tqdm/.meta/mkcompletion.py

54 lines
1.3 KiB
Python
Raw Normal View History

2020-04-24 21:25:32 +00:00
from __future__ import print_function
from os import path
import sys
sys.path.insert(0, path.dirname(path.dirname(__file__))) # NOQA
import tqdm
import tqdm.cli
from io import open as io_open
from os import path
import re
2020-04-25 17:44:23 +00:00
RE_OPT = re.compile(r'(\w+) :', flags=re.M)
def doc2opt(doc):
"""
doc : str, document to parse
"""
2020-04-25 18:45:35 +00:00
return ('--' + i for i in RE_OPT.findall(doc))
2020-04-25 17:44:23 +00:00
# CLI options
2020-04-25 17:44:23 +00:00
options = {'-h', '--help', '-v', '--version'}
2020-04-25 18:45:35 +00:00
for doc in (tqdm.tqdm.__init__.__doc__, tqdm.cli.CLI_EXTRA_DOC):
options.update(doc2opt(doc))
options.difference_update(
'--' + i for i in ('name',) + tqdm.cli.UNSUPPORTED_OPTS)
2020-04-24 21:25:32 +00:00
src_dir = path.abspath(path.dirname(__file__))
completion = u"""\
#!/usr/bin/env bash
_tqdm()
{{
local cur prv
cur="${{COMP_WORDS[COMP_CWORD]}}"
prv="${{COMP_WORDS[COMP_CWORD - 1]}}"
case ${{prv}} in
"--log")
COMPREPLY=($(compgen -W 'CRITICAL FATAL ERROR WARN WARNING INFO DEBUG NOTSET' -- ${{cur}}))
;;
*)
COMPREPLY=($(compgen -W '{0}' -- ${{cur}}))
;;
esac
}}
complete -F _tqdm tqdm
""".format(' '.join(options))
2020-04-24 21:25:32 +00:00
if __name__ == "__main__":
fncompletion = path.join(path.dirname(src_dir), 'tqdm', 'completion.sh')
with io_open(fncompletion, mode='w', encoding='utf-8') as fd:
fd.write(completion)