Created
November 17, 2017 15:26
-
-
Save barend/cf80ecbbe22a547c245e4828f4a4d4e2 to your computer and use it in GitHub Desktop.
Python command line program skeleton
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 | |
""" | |
Does cool things, with optional verbose output. | |
""" | |
import logging | |
log = logging.getLogger() | |
def do_stuff(arg1: str, arg2: list): | |
log.info("Normal output") | |
log.debug("Verbose output (%s) %s", arg1, arg2) | |
if __name__ == "__main__": | |
import argparse, sys | |
parser = argparse.ArgumentParser(description='Text that goes at the top', | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
epilog='Text that goes at the bottom.') | |
parser.add_argument('-v', '--verbose', action='store_true', help='show detailed output') | |
parser.add_argument('--arg_1', default='arg_1_default', help='first argument') | |
parser.add_argument(dest='arg_2', nargs=3, help='trailing arguments') | |
args = parser.parse_args() | |
level = 'DEBUG' if args.verbose else 'INFO' | |
logging.basicConfig(stream=sys.stdout, level=level, format='[{levelname}]\t{message}', style='{') | |
do_stuff(args.arg_1, args.arg_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment