I am trying to use both argparse and logging modules in python. I have a program that I run that has a lot of possible options and I’ve successfully implemented the argparse module to handle this task.
I’d like to keep a record of the values each option has when the program is run and send it to a log file. I tried the following couple of things, and I included the associated error I encounter as a comment beneath it.
parser = argparse.ArgumentParser()
parser.add_argument('input', action="store", default='fort.13', type=str)
args = parser.parse_args()
# First try:
logging.info("Input args: " + args)
# TypeError: cannot concatenate 'str' and 'Namespace' objects
# Second try:
for x in args:
logging.info(x)
# TypeError: 'Namespace' object is not iterable
What is the proper way to do this?
You can use
varsto get the attributes of your parsed arguments:This is detailed in the docs.