1

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.

STGHMAAV
  • 11
  • 2
  • Your question is exactly the same as [this one](https://stackoverflow.com/questions/26679272/how-to-use-argv-with-spyder/), so please remove it. – Carlos Cordoba Dec 13 '18 at 19:37
  • I don't think it's a duplicate, since that's not what I'm asking for, I tried doing what the answer on that question says, but it doesn't work, when I try to execute the program on spyder, with for example "-h" option, it says it doesn't recognise it. – STGHMAAV Dec 14 '18 at 15:54

1 Answers1

-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()
pypalms
  • 461
  • 4
  • 12