Skip to content

Instantly share code, notes, and snippets.

@cyber-murmel
Last active May 14, 2023 18:40
Show Gist options
  • Save cyber-murmel/d91c8ea16ab550a5e08022bfad7cc531 to your computer and use it in GitHub Desktop.
Save cyber-murmel/d91c8ea16ab550a5e08022bfad7cc531 to your computer and use it in GitHub Desktop.
Python CLI program template
#!/usr/bin/env python3.10
from argparse import ArgumentParser, Action, FileType, ArgumentError, ArgumentTypeError, Namespace
from logging import debug, info, warning, error, exception
from logging import DEBUG, INFO, WARNING, ERROR
from coloredlogs import install as color_log
def parse_arguments():
parser = ArgumentParser(
description="",
prog="foobar.py",
epilog="""
""",
)
verbosity = parser.add_mutually_exclusive_group()
verbosity.add_argument(
"-q", "--quiet", action="store_true", help="turn off warnings"
)
verbosity.add_argument(
"-v", "--verbose", action="count", help="set verbose loglevel"
)
args = parser.parse_args()
return args
def get_log_level(verbose, quiet):
return (
ERROR if quiet else WARNING if not verbose else INFO if 1 == verbose else DEBUG
) # 2 <= verbose
def main(args: Namespace):
return 0
if "__main__" == __name__:
args = parse_arguments()
color_log(level=get_log_level(args.verbose, args.quiet))
debug(args)
exit(main(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment