Sometimes we have to create tools that takes input as argument with certain options. We can create such tool with optparse module of python.
Here is a small example of using this.
[code language=”python”]
from optparse import OptionParser
parser = OptionParser(usage=’usage: %prog [options] arguments’)
parser.add_option(‘-a’,help="setup/cleanup",action="store", dest="action")
parser.add_option(‘-m’,help="email id",action="store", dest="email")
parser.add_option(‘-i’,help="Input json props",action="store", dest="input")
(options, args) = parser.parse_args()
[/code]
For help, type: ( This will display all the arguments that can be used with their format)
python tool.py -h
Usage: tool.py [options] arguments
Options:
-h, –help show this help message and exit
-a ACTION setup/cleanup
-m EMAIL email id
-i INPUT Input json props
save it in a programme and execute it as :
python tool.py -a setup -i file.json -m pdk@pdk.com
Now we can access the above inputs and use them, using below variables inside the programme:
[code language=”python”]
options.input
options.email
options.action
[/code]