Given code like this, how do I actually set a file in the run options?
I am using Spyder and have put -h -s -p -o
as arguments, but I'm not sure how to specify a named file for the -o
option.
class CommandLine:
def __init__(self):
opts, args = getopt.getopt(sys.argv[1:],'hspw:o:')
opts = dict(opts)
if '-o' in opts:
self.outfile = opts['-o']
else:
self.outfile = None
This is a simple tutorial dealing with argpase.
But first of all, i recommend you to read the official documentation if you want to have more control when using argparse module.
Also if you want to pass arguments to Spyder, i would recommend the answer of @Carlos Cordoba who suggested to see this answer.
My tutorial script:
Now, in your terminal or with Spyder:
Output:
And when you use
-h
or--help
as argument you'll have this output:Output: