Python for n00bs: Command Line Options
07.14.11
Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. It can be used on many differant types of network enviroments for automating complex and mundane tasks and also can be utilized in such a wide variety it makes it a must know language. For starters we will go over OPTPARSE python module which will let us process options from a cmd shell and pass those to variables we can later call on from within our script.
First thing we do is import our required modules into the script, in this case we will be using os, sys, and optparse
import os, sys, optparse
Next, we define our main function, this is the function the script will automaticly jump to when done parsing our command arguments.
import os, sys, optparse
def main():
Now, lets define our options we want the parser to read and add in the parser, this will take the variables supplied in the shell and parse them into options.+dest+ where +dest+ is going to be whatever you want your option to be called. We will parse this particular one as a string, rather than an integer. We are also setting a default value to avoid errors if no value is supplied, for a null value you may add "".
import os, sys, optparse
def main():
if __name__=="__main__":
parser = optparse.OptionParser("usage: %prog [options] ")parser.add_option("-a", "--A", dest="var1",help="Variable 1", type="string", default="1")
parser.add_option("-b", "--B", dest="var2",help="Variable 2", type="string", default="2")
parser.add_option("-c", "--C", dest="var3",help="Variable 3", type="string", default="3")
parser.add_option("-d", "--D", dest="var4",help="Variable 4", type="string", default="4")
(options, args) = parser.parse_args()
Lastly I want to take the options.+dest+ and convert that into a single variable
import os, sys, optparse
def main():
if __name__=="__main__":
parser = optparse.OptionParser("usage: %prog [options] ")parser.add_option("-a", "--A", dest="var1",help="Variable 1", type="string", default="1")
parser.add_option("-b", "--B", dest="var2",help="Variable 2", type="string", default="2")
parser.add_option("-c", "--C", dest="var3",help="Variable 3", type="string", default="3")
parser.add_option("-d", "--D", dest="var4",help="Variable 4", type="string", default="4")
(options, args) = parser.parse_args()
var1 = options.var1
var2 = options.var2
var3 = options.var3
var4 = options.var4
main()
We are done with the setup of command options, now to utilze them, jump back up to our main function and start adding some functions. For the demo and template I have it set to echo the variables values. You can decide which variables your script will need ahead of time and test to make sure they are all working before you begin your actual project.
#!/usr/bin/python
#Python Script Template
#Written by Ben Buchacher bbuchacher@sbnsc.net
import os, sys, optparse
def main():
os.system("echo "+var1+"")os.system("echo "+var2+"")os.system("echo "+var3+"")os.system("echo "+var4+"")if __name__=="__main__":
parser = optparse.OptionParser("usage: %prog [options] ")parser.add_option("-a", "--A", dest="var1",help="Variable 1", type="string", default="1")
parser.add_option("-b", "--B", dest="var2",help="Variable 2", type="string", default="2")
parser.add_option("-c", "--C", dest="var3",help="Variable 3", type="string", default="3")
parser.add_option("-d", "--D", dest="var4",help="Variable 4", type="string", default="4")
(options, args) = parser.parse_args()
var1 = options.var1
var2 = options.var2
var3 = options.var3
var4 = options.var4
main()
December 2012 (0)