Skip to content

Instantly share code, notes, and snippets.

@mortn
Forked from elmotec/bootstrap_cmdline.py
Created September 13, 2017 07:21
Show Gist options
  • Save mortn/d588f5c8c53e84a3ef40fd7e41d15bc4 to your computer and use it in GitHub Desktop.
Save mortn/d588f5c8c53e84a3ef40fd7e41d15bc4 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