Skip to content
Matt Harley edited this page Oct 9, 2015 · 4 revisions

Logging and args

# My recommendation is argparse, it's usually more than powerful enough and comes with python. You can even pass in a list of arguments (default of None means that it uses sys.argv) so that you can automate testing of your command line arguments too.

parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument('-q', '--quiet', action='count', default=0)

logging_level = logging.WARN + 10*args.quiet - 10*args.verbose

# script -vv -> DEBUG
# script -v -> INFO
# script -> WARNING
# script -q -> ERROR
# script -qq -> CRITICAL
# script -qqq -> no logging at all
Clone this wiki locally