0

I need a simple and effective method of parsing through the Python script call arguments (preferably without using any loops).

For example, the program must understand the following two commands as the same (aka order doesn't matter):

py -3.7 main.py [-file] <filename> [-speed] <int> [-method] <str>
py -3.7 main.py [-speed] <int> [-method] <str> [-file] <filename>

Note that the script should be able to take any number of arguments (with 3 arguments being the max).

py -3.7 main.py [-speed] <int> [-method] <str>
py -3.7 main.py [-speed] <int> [-file] <filename>

One solution that could potentially work is to first identify the option (-speed, -file, or -method) as sys.argv[i] and then use sys.argv[i + 1] to grab the correct following information. However, if I'm not mistaken, this requires at least two loops to complete.

I know that a lot of the Python libraries offer a large number of optional arguments like this, so I'm wondering if there is any easier solution to this without having to write up a number of for loops.

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
Echidna
  • 172
  • 7
  • 7
    The most common way to parse Python command-line options is with [`argparse`](https://docs.python.org/3/howto/argparse.html) – Tom Karzes Jul 09 '21 at 00:46

0 Answers0