serhii.net

In the middle of the desert you can say anything you want

13 Jan 2022

Easier python logging setup with argparse's 'dest' parameter

I find this approach1 brilliant (and of course it works with everything split in separate functions a la my last post: 211124-1744 argparse notes):

import argparse
import logging

parser = argparse.ArgumentParser()
parser.add_argument(
    '-d', '--debug',
    help="Print lots of debugging statements",
    action="store_const", dest="loglevel", const=logging.DEBUG,
    default=logging.WARNING,
)
parser.add_argument(
    '-v', '--verbose',
    help="Be verbose",
    action="store_const", dest="loglevel", const=logging.INFO,
)
args = parser.parse_args()    
logging.basicConfig(level=args.loglevel)

And TIL about dest= that will make my life much easier too by outsourcing more logic to argparse.

Nel mezzo del deserto posso dire tutto quello che voglio.