Created
June 6, 2023 10:26
-
-
Save pistocop/9763eb06829b5fccf8171cf4f9e94151 to your computer and use it in GitHub Desktop.
[template][py] Minimal script template using standard libraries
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
""" | |
<Script description here> | |
""" | |
import argparse | |
import logging | |
import sys | |
try: | |
import requests # example of required library | |
except ImportError: | |
logging.error('Python library "requests" required but not installed') | |
exit(1) | |
def run(arg1: str, arg2: str): | |
logging.info("Info msg") | |
logging.debug("Debug msg") | |
logging.warning("Warn msg") | |
exit(0) | |
def cmdline_args(): | |
p = argparse.ArgumentParser( | |
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | |
) | |
p.add_argument("--arg1", type=str, required=True, help="") | |
p.add_argument("--arg2", type=str, required=True, help="") | |
p.add_argument( | |
"-d", | |
"--debug", | |
help="Enable debug logging statements", | |
action="store_const", | |
dest="loglevel", | |
const=logging.DEBUG, | |
default=logging.INFO, | |
) | |
p.add_argument( | |
"-q", | |
"--quiet", | |
help="Disable info logging statements", | |
action="store_const", | |
dest="loglevel", | |
const=logging.WARNING, | |
default=logging.INFO, | |
) | |
return p.parse_args() | |
if __name__ == "__main__": | |
if sys.version_info < (3, 7, 0): | |
sys.stderr.write("You need python 3.7 or later to run this script\n") | |
sys.exit(1) | |
try: | |
args = cmdline_args() | |
except Exception as e: | |
logging.error(f"Error during command parsing: `{e}`") | |
sys.exit(1) | |
logging.basicConfig(level=args.loglevel) | |
run(arg1=args.arg1, arg2=args.arg2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment