Last active
June 10, 2020 11:50
-
-
Save ssato/4f4e1d11616bb845145d4236b0df64ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
_VERB_OPT_RE = re.compile(r"v+") | |
def verbose_flag(options, verb_re=_VERB_OPT_RE): | |
""" | |
Return computed verbosity flag. | |
:param options: A mapping object holding CLI options | |
:return: A list of strings give verbose option | |
>>> verbose_flag({}) | |
[] | |
>>> verbose_flag(dict(become=True)) | |
[] | |
>>> verbose_flag(dict(v=True)) | |
['-v'] | |
>>> verbose_flag(dict(v=False)) | |
[] | |
>>> verbose_flag(dict(verbose=True)) | |
['-v'] | |
>>> verbose_flag(dict(vvv=True)) | |
['-vvv'] | |
>>> verbose_flag(dict(vvvvvvvvv=True)) | |
['-vvvv'] | |
""" | |
if not options: | |
return [] | |
if options.get("verbose", False): | |
return ["-v"] | |
# verbs: eg. [('vvv', True)] | |
verbs = [(k, v) for k, v in options.items() if verb_re.match(k)] | |
if verbs and verbs[0][-1]: # [('vvv', True)] -> True | |
return ["-{}".format(verbs[0][0][:4])] | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment