Monday, March 8, 2010

March Madness Entry 8

As exhaustion sets in from spreading myself a bit thin, I offer up this morsel of python programming to pass yet another day in the challenge. Some of you may notice that yesterday's is missing - the code is done, but the video required for such a project is late coming for various reasons. When I get video and can offer up the code, I will post it.

For now? A touch of getopts.
When writing scripts for command line use, the getopt parser is a useful little module.

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-t", "--test", dest="testvalue", help="the test value that will be printed.")

(options, args) = parser.parse_args()

print parser.values.testvalue
This is the simplest possible program you can use getopt for - here you can see how the opt parser will collect its data and store it. By grabbing known options and args. The best part is, if you set it up correctly, you can get help files automatically generated. Add a help option, and you can just create a simple script to scroll through optparse options, and their descriptions, to generate a nice list of help files.

Now, if you excuse me, I literally feel my knee about to throw out, and I'm tired and have work early tomorrow morning. So good night, readers. Sorry for the morsel size code entry today - couldn't be helped.

No comments:

Post a Comment