Last active
May 4, 2019 01:46
-
-
Save CMCDragonkai/432b1e2348ae41862444c55f0bac33b2 to your computer and use it in GitHub Desktop.
Python Argparse Verbose Count #python
This file contains 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 argparse | |
import logging | |
def verbose_to_loglevel(verbose=0): | |
loglevel = logging.WARNING | |
if verbose == 1: | |
loglevel = logging.INFO | |
elif verbose >= 2: | |
loglevel = logging.DEBUG | |
return loglevel | |
# this is really useful like ['--verbose'] * verbose | |
# so you interpolate it into command list | |
# ['command', *(['--verbose'] * verbose)] | |
def loglevel_to_verbose(loglevel=logging.WARNING): | |
if loglevel >= logging.WARNING: | |
verbose = 0 | |
elif loglevel == logging.INFO: | |
verbose = 1 | |
elif loglevel < logging.INFO: | |
verbose = 2 | |
return verbose | |
def main(): | |
options_parser = argparse.ArgumentParser() | |
# this allows you to parse `-v`, `-vv`, `-v -v` | |
options_parser.add_argument( | |
'-v', | |
'--verbose', | |
help='Log Verbose Messages', | |
action='count', | |
default=0) | |
options = options_parser.parse_args() | |
logging.basicConfig(level=verbose_to_loglevel(options.verbose)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment