Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ssato/4f4e1d11616bb845145d4236b0df64ff to your computer and use it in GitHub Desktop.
Save ssato/4f4e1d11616bb845145d4236b0df64ff to your computer and use it in GitHub Desktop.
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