问题
I don't know how to execute a program with optional arguments on Spyder. I know how to pass variables to it, but my program uses argparse, and I want to execute it with the "-h" or "--help" option, the code is the following one
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
For now, it only has the default optional argument of "-h"/"--help", I tried putting it on "Command line options" but it doesn't work.
回答1:
You would have to define the arguments in order for them to be used. It looks like it is just using the default argparse method which only defines the help method in the constructor.
Check the docs here: https://docs.python.org/3/library/argparse.html
Here is an example method for parsing the args I have used before:
import argparse
def process_args(source=None):
parser = argparse.ArgumentParser(prog='my-awesome-program')
parser.add_argument('--value1', dest='value1', type=str)
parser.add_argument('--value2', dest='value2', type=str)
args = parser.parse_args(source)
return args
def main():
args = process_args()
args = vars(args)
my_value_1 = args['value1']
my_value_2 = args['value2']
print('%s, %s' % (my_value_1, my_value_2))
if __name__ == '__main__':
main()
来源:https://stackoverflow.com/questions/53768365/how-to-execute-with-optional-argument-in-spyder