Skip to content

Instantly share code, notes, and snippets.

@qheuristics
Forked from elmotec/bootstrap_cmdline.py
Created February 15, 2018 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qheuristics/56e9190ea388fe91f1768559be1b77aa to your computer and use it in GitHub Desktop.
Save qheuristics/56e9190ea388fe91f1768559be1b77aa to your computer and use it in GitHub Desktop.
Minimal python program with logging and argparse.
#!/usr/bin/env python
# encoding: utf-8
"""Minimal python commad line."""
import sys
import argparse
import logging
module = sys.modules['__main__'].__file__
log = logging.getLogger(module)
def parse_command_line(argv):
"""Parse command line argument. See -h option
:param argv: arguments on the command line must include caller file name.
"""
formatter_class = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(description=module,
formatter_class=formatter_class)
parser.add_argument("--version", action="version",
version="%(prog)s {}".format(__version__))
parser.add_argument("-v", "--verbose", dest="verbose_count",
action="count", default=0,
help="increases log verbosity for each occurence.")
parser.add_argument('-o', metavar="output",
type=argparse.FileType('w'), default=sys.stdout,
help="redirect output to a file")
parser.add_argument('input', metavar="input", nargs='+',
argparse.REMAINDER, help="input if any...")
arguments = parser.parse_args(argv[1:])
# Sets log level to WARN going more verbose for each new -v.
log.setLevel(max(3 - arguments.verbose_count, 0) * 10)
return arguments
def main():
"""Main program. Sets up logging and do some work."""
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
format='%(name)s (%(levelname)s): %(message)s')
try:
arguments = parse_command_line(sys.argv)
# Do something with arguments.
except KeyboardInterrupt:
log.error('Program interrupted!')
finally:
logging.shutdown()
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment