Linux Today: Linux News On Internet Time.

Building Command Line Utilities with Python

Nov 11, 2008, 21:34 (0 Talkback[s])
(Other stories by Jeremy M. Jones)

"The command line arguments are treated as a list, which is accessible by referencing sys.argv. The first element of the list is the name of the script and the remaining elements are the arguments passed in after the script name. If I wanted to, I could figure out what options I wanted to take, iterate through sys.argv, and map what the user gave me to the options I wanted to handle. I could do that, but it would be wasted time.

"There is a better way. A much better way. It's called optparse.

"With optparse, you declaratively build up an option parser by adding the behavior you want it to exhibit when it sees various options. Here's a simple example of a script that uses optparse."

Complete Story

Related Stories: