Created
September 25, 2021 04:24
-
-
Save elliotwesoff/76d691446278143cc0fc9893293b8e5d to your computer and use it in GitHub Desktop.
python cli template
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 sys | |
import logging | |
import traceback | |
from optparse import OptionParser | |
def main(): | |
(options, args) = setup() | |
return 0 | |
def setup(): | |
parser = OptionParser() | |
return parser.parse_args() | |
def format_exception(e): | |
# this method credit to https://stackoverflow.com/a/12539332 | |
# ...why doesn't it use the exception argument? lol | |
exception_list = traceback.format_stack() | |
exception_list = exception_list[:-2] | |
exception_list.extend(traceback.format_tb(sys.exc_info()[2])) | |
exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])) | |
exception_str = "Traceback (most recent call last):\n" | |
exception_str += "".join(exception_list) | |
exception_str = exception_str[:-1] | |
return exception_str | |
if __name__ == '__main__': | |
try: | |
format = '%(levelname)s - %(asctime)s -> %(message)s' | |
logging.basicConfig(format=format, level=logging.DEBUG) | |
exit(main()) | |
except Exception as e: | |
logging.fatal(format_exception(e)) | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment