Skip to content

Instantly share code, notes, and snippets.

@dkirkby
Last active May 3, 2022 21:37
Show Gist options
  • Save dkirkby/da4d55ffb202f7408851cb0565b25845 to your computer and use it in GitHub Desktop.
Save dkirkby/da4d55ffb202f7408851cb0565b25845 to your computer and use it in GitHub Desktop.
Skeleton command line script
'''
Save this file to scripts/myprog.py then add these lines to setup.py:
entry_points = {
'console_scripts': [
'myprog=fpoffline.scripts.myprog:main',
],
}
'''
import argparse
import logging
import pdb
import traceback
import sys
def run(args):
# raise an exception here to flag any error
# return value is propagated to the shell
return 0
def main():
# https://docs.python.org/3/howto/argparse.html
parser = argparse.ArgumentParser(
description='Calculate per-exposure effective depths',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-v', '--verbose', action='store_true',
help='provide verbose output on progress')
parser.add_argument('--debug', action='store_true',
help='provide verbose and debugging output')
parser.add_argument('--logpath', type=str, metavar='PATH',
help='Path where logging output should be written')
parser.add_argument('--traceback', action='store_true',
help='print traceback and enter debugger after an exception')
args = parser.parse_args()
# Configure logging.
if args.debug:
level = logging.DEBUG
elif args.verbose:
level = logging.INFO
else:
level = logging.WARNING
logging.basicConfig(filename=args.logpath, level=level,
format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y%m%d %H:%M:%S')
try:
retval = run(args)
sys.exit(retval)
except Exception as e:
if args.traceback:
# https://stackoverflow.com/questions/242485/starting-python-debugger-automatically-on-error
extype, value, tb = sys.exc_info()
traceback.print_exc()
pdb.post_mortem(tb)
else:
print(e)
sys.exit(-1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment