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

  1. 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.

  1. import os, sys, optparse
  2. 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 "".

  1. import os, sys, optparse
  2. def main():
  3.  
  4. if __name__=="__main__":
  5.     parser = optparse.OptionParser("usage: %prog [options] ")
  6.     parser.add_option("-a", "--A", dest="var1",
  7.                   help="Variable 1", type="string", default="1")
  8.     parser.add_option("-b", "--B", dest="var2",
  9.                   help="Variable 2", type="string", default="2")
  10.     parser.add_option("-c", "--C", dest="var3",
  11.                   help="Variable 3", type="string", default="3")
  12.     parser.add_option("-d", "--D", dest="var4",
  13.                   help="Variable 4", type="string", default="4")
  14.  
  15.     (options, args) = parser.parse_args()

Lastly I want to take the options.+dest+ and convert that into a single variable

  1. import os, sys, optparse
  2. def main():
  3.  
  4. if __name__=="__main__":
  5.     parser = optparse.OptionParser("usage: %prog [options] ")
  6.     parser.add_option("-a", "--A", dest="var1",
  7.                   help="Variable 1", type="string", default="1")
  8.     parser.add_option("-b", "--B", dest="var2",
  9.                   help="Variable 2", type="string", default="2")
  10.     parser.add_option("-c", "--C", dest="var3",
  11.                   help="Variable 3", type="string", default="3")
  12.     parser.add_option("-d", "--D", dest="var4",
  13.                   help="Variable 4", type="string", default="4")
  14.  
  15.     (options, args) = parser.parse_args()
  16.     var1 = options.var1
  17.     var2 = options.var2
  18.     var3 = options.var3
  19.     var4 = options.var4
  20. 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.

  1. #!/usr/bin/python
  2. #Python Script Template
  3. #Written by Ben Buchacher bbuchacher@sbnsc.net
  4. import os, sys, optparse
  5.  
  6. def main():
  7.     os.system("echo "+var1+"")
  8.     os.system("echo "+var2+"")
  9.     os.system("echo "+var3+"")
  10.     os.system("echo "+var4+"")
  11.  
  12. if __name__=="__main__":
  13.     parser = optparse.OptionParser("usage: %prog [options] ")
  14.     parser.add_option("-a", "--A", dest="var1",
  15.                   help="Variable 1", type="string", default="1")
  16.     parser.add_option("-b", "--B", dest="var2",
  17.                   help="Variable 2", type="string", default="2")
  18.     parser.add_option("-c", "--C", dest="var3",
  19.                   help="Variable 3", type="string", default="3")
  20.     parser.add_option("-d", "--D", dest="var4",
  21.                   help="Variable 4", type="string", default="4")
  22.  
  23.     (options, args) = parser.parse_args()
  24.     var1 = options.var1
  25.     var2 = options.var2
  26.     var3 = options.var3
  27.     var4 = options.var4
  28. main()