Last active
February 1, 2023 17:11
-
-
Save DrDougPhD/c1f1349ccfa6e0963437dd7dbe8ea25c to your computer and use it in GitHub Desktop.
Skeleton for Python command line script, including 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 python | |
# -*- coding: utf-8 -*- | |
""" | |
SYNOPSIS | |
python SCRIPT.py [-h,--help] [-v,--verbose] | |
DESCRIPTION | |
Concisely describe the purpose this script serves. | |
ARGUMENTS | |
-h, --help show this help message and exit | |
-v, --verbose verbose output | |
AUTHOR | |
Doug McGeehan <[email protected]> | |
LICENSE | |
Specify this script's / package's license. | |
""" | |
__appname__ = "appname" | |
__author__ = "Doug McGeehan <[email protected]>" | |
__version__ = "0.0pre0" | |
__license__ = "License" | |
import argparse | |
from datetime import datetime | |
import sys | |
import os | |
import logging | |
logger = logging.getLogger(__appname__) | |
def main(args): | |
pass | |
def setup_logger(args): | |
logger.setLevel(logging.DEBUG) | |
# create file handler which logs even debug messages | |
# todo: place them in a log directory, or add the time to the log's | |
# filename, or append to pre-existing log | |
fh = logging.FileHandler(__appname__ + ".log") | |
fh.setLevel(logging.DEBUG) | |
# create console handler with a higher log level | |
ch = logging.StreamHandler() | |
if args.verbose: | |
ch.setLevel(logging.DEBUG) | |
else: | |
ch.setLevel(logging.INFO) | |
# create formatter and add it to the handlers | |
fh.setFormatter(logging.Formatter( | |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
)) | |
ch.setFormatter(logging.Formatter( | |
'%(message)s' | |
)) | |
# add the handlers to the logger | |
logger.addHandler(fh) | |
logger.addHandler(ch) | |
def get_arguments(): | |
parser = argparse.ArgumentParser( | |
description="Description printed to command-line if -h is called." | |
) | |
# during development, I set default to False so I don't have to keep | |
# calling this with -v | |
parser.add_argument('-v', '--verbose', action='store_true', | |
default=False, help='verbose output') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
try: | |
start_time = datetime.now() | |
args = get_arguments() | |
setup_logger(args) | |
logger.debug(start_time) | |
main(args) | |
finish_time = datetime.now() | |
logger.debug(finish_time) | |
logger.debug('Execution time: {time}'.format( | |
time=(finish_time - start_time) | |
)) | |
logger.debug("#"*20 + " END EXECUTION " + "#"*20) | |
sys.exit(0) | |
except KeyboardInterrupt as e: # Ctrl-C | |
raise e | |
except SystemExit as e: # sys.exit() | |
raise e | |
except Exception as e: | |
logger.exception("Something happened and I don't know what to do D:") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment