Last active
April 24, 2021 16:44
-
-
Save Jamie-/1ebaa3a0b63a23c7549da3b0a42c3eae to your computer and use it in GitHub Desktop.
Python 3 boilerplate with hashbang, argparse and logging.
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
#!/usr/bin/env python3 | |
import argparse | |
import logging | |
logging.basicConfig( | |
format="[%(asctime)s.%(msecs)03d][%(levelname)8s][%(module)s] %(message)s", | |
datefmt="%Y-%m-%d %H:%M:%S", | |
# level=logging.INFO, | |
) | |
logger = logging.getLogger(__name__) | |
parser = argparse.ArgumentParser(description='Description goes here.') | |
parser.add_argument('-v', dest='verbose_info', action='store_true', help='Enable info messages.') | |
parser.add_argument('-vv', dest='verbose_debug', action='store_true', help='Enable info and debug messages.') | |
parser.add_argument('-o', '--option', dest='my_opt', metavar='opt', default='hello', help='Generic option.') | |
parser.add_argument('--boolean', dest='my_bool', action='store_true', help='Generic boolean option.') | |
args = parser.parse_args() | |
# Set root logging level | |
if args.verbose_debug: | |
logging.getLogger().setLevel(logging.DEBUG) | |
elif args.verbose_info: | |
logging.getLogger().setLevel(logging.INFO) | |
logger.debug('Arguments parsed successfully.') | |
logger.info('The value of \'-o\' is \'%s\'.', args.my_opt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment