Im trying to write a script using python language(abc.py). I need to take few command line arguments while executing the script.
say -m, -p are my arguments. I need to have a string beside these options.
ex:
1. $> python abc.py -m 'first' -p 'second' {correct}
2. $> python abc.py -p 'first' -m 'second' {correct}
3. $> python abc.py -m -p 'first' 'second' {Incorrect}
4. $> python abc.py 'first' -p 'second' {Incorrect}
I have more such arguments like -m, -p. What is the best algorithm to check if the passed arguments are in the correct format. I am unable think other than the method maintaining the previous argument and check based on it.
Thanks for you help in advance
Anji
You don’t need to do that yourself, Python as they say comes with batteries included. The standard library has two modules for parsing command line arguments: argparse for python 2.7+ and optparse for 2.6 or older. Documentation for those has good usage examples, too.