I find both --verbose
(for users) and --debug
(for developers) useful. Here's how I do it with logging
and argparse
:
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)
So if --debug
is set, the logging level is set to DEBUG
. If --verbose
, logging is set to INFO
. If neither, the lack of --debug
sets the logging level to the default of WARNING
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…