How to store argparse values in variables?

argparse, python

Solution

That will work, but you can simplify it a bit like this:

args = parser.parse_args()

foo = args.one
bar = args.two
cheese = args.three

Problem

I am trying to add command line options to my script, using the following code: ``` import argparse parser = argparse.ArgumentParser('My program') parser.add_argument('-x', '--one') parser.add_argument('-y', '--two') parser.add_argument('-z', '--three') args = vars(parser.parse_args()) foo = args['one'] bar = args['two'] cheese = args['three'] ``` Is this the correct way to do this? Also, how do I run it from the IDLE shell? I use the command 'python myprogram.py -x foo -y bar -z cheese' and it gives me a syntax error

Original source