From The Compiler, 9 Years ago, written in Python.
Embed
  1. import argparse, logging
  2.  
  3. def parseargs():
  4.     parser.add_argument('-l', '--loglevel', help=argparse.SUPPRESS,
  5.            default='info', type=opt_loglevel)
  6.     options = parser.parse_args()
  7.     return options
  8.  
  9. def opt_loglevel(loglevel):
  10.     """ Get the numeric loglevel from the command line option """
  11.     numeric_level = getattr(logging, loglevel.upper(), None)
  12.     if not isinstance(numeric_level, int):
  13.         raise argparse.ArgumentTypeError('{} is not a valid '
  14.                        'loglevel!'.format(loglevel))
  15.     return numeric_level
  16.  
  17. def initlog(numeric_level):
  18.     """ Initialize logging """
  19.     logging.basicConfig(level=numeric_level,
  20.                         format='[%(levelname)-8s] %(message)s')
  21.  
  22. options = parseargs()
  23. initlog(options.loglevel)