Dealing with Command Line Options in Python

 in
I just wrote a bit of Python to generate some reports from the contents of a database. The one program that was more than just "display the data" was the one to print the transaction log. It included some sub-totals for various fields and paginated output.

The one task remaining was to give it some options. That is, to pass it some criteria that would modify the report. Specifically, I wanted a start and end date and the ability to change the sort order from the default which was transaction date.

Before just jumping into the brute force way of dealing with the command line I decided to see if there was anything interesting in the Python libraries that might turn this into something a bit more pleasant. Well, I found good news in the form of the optparse module. From O'Reilly's Python in a Nutshell, I found the following description:

The optparse module offers rich, powerful ways to parse the command-line options that the user passed upon starting your programs.

Sounds good. My next check was on python.org with equally positive information. So, I decided to give it a try. Here is the short story (followed by a bit of code).

  • First you instantiate the class OptionParser.
  • For each option you want to use, you use the add_option method where you can specify the options (both short and long ones are supported) and tell optparse what to do.
  • Once you are all set up, you use the parse_args method to do all the dirtywork. You then can just check for the existence of the options by checking for an attribute.
opts = OptionParser()
opts.add_option("--start", "-s", help="in format yyyy-mm-dd")
opts.add_option("--end", "-e", help="in format yyyy-mm-dd")
opts.add_option("--order", "-o", type="choice", choices=["tdate", "cname_id",
  "account_id", "project_id", "id"], help="tdate|cname_id|account_id|project_id|id")
options, arguments = opts.parse_args()

if options.start:
...
if options.order:
...

That should be enough code. Each of the opts.add_option( lines adds an option to the list. For example, the first one says that it matches either --start or -s. The implied action is to store the argument associated with it. The help argument is used to specify a help message that is printed out either in case of an error calling the command or if the command is invoked with the -h option.

The third opts.add_option( line is a bit different. It defines the type of option as a choice and then includes a list of valid values. Entering anything other than one of these values results in an error message including the usage display.

The call to opts.parse_args() does the dirtywork for you. For example, if I had invoked the program with -s 2008-11-11 then options.start would be set to 2008-11-11.

All in all, I found optparse easy to understand and enjoyed separating all the error checking from using the arguments. There are lots more things it can do for you but this should get you started. It is now in my list of "Python tricks".

______________________

Phil Hughes

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Specifying a file name containing a space

Dan Aquinas's picture

How would one specify a file name when that file name contains a space. Many of the primers on Python command line processing provide such simple examples and do not deal with this very real world case. For example, I want to supply something like this.

    PrepareReport.py -f "My Output File.txt"

or would it be:

    PrepareReport.py -f="My Output File.txt"

adding the equals sign after the "-f" option?

Arguments with spaces

Mitch Frazier's picture

Assuming you have something like this:

from optparse import OptionParser

opts = OptionParser()
opts.add_option("--file", "-f", type="string", help="a file")
options, arguments = opts.parse_args()

if options.file:
    print options.file

the following work:

    PrepareReport.py -f "My Output File.txt"
    PrepareReport.py -f"My Output File.txt"
    PrepareReport.py "-fMy Output File.txt"

    PrepareReport.py --file "My Output File.txt"
    PrepareReport.py --file="My Output File.txt"
    PrepareReport.py "--file=My Output File.txt"

Mitch Frazier is an Associate Editor for Linux Journal.

Problem with program

Mike Tipton's picture

I am really new to Python. Basically I am trying to find examples of Python code, find out what it does and how it works, and why it works.

When I copied in the program, it is failing at the line

opts = OptionParser()

which is the first line of the program.

I have no idea why this is. Everyone (on the posts) says it works for them.
If this is a function call, I don't have a clue as to where it is SUPPOSED to be found. This is my main gripe with python. I download little programs and 90% of them never work. There is always something missing, and again, I don't know where to find it. When I downloaded and installed Python (2.6.1 I think) I "assumed" it was a complete package.

I was a COBOL programmer in a previous life, so I understand programming.

Where is a good web site that gives references to ALL of the commands just like the complete reference books on COBOL that I used to use? If I needed to know a command and how it works, I could just go to one place and I could find my answer.

Re: Problem with program

Nils R Grotnes's picture

Hi Mike.

The problem is this how-to wasn't aimed at you, a Python beginner, but rather those that know that such library modules must be imported before they can be used. Fortunately there's many good sources for getting started, in fact I bet you found the answer long before I posted this. Still, in case other beginners stumble upon this post, here's a good place to start looking for answers: http://www.python.org/doc/

Nils

Good info

server counter strike's picture

Very interesting post, I was reading everything and I liked a lot, Keep it!

getopt

magos's picture

Sorry for being lame in case i'm one, but could you now have used getopt()?

I'm just a python enthusiast!!

Thanks.

____

magos

Good one, thanks

Rec's picture

Well, is an easy one to make any app a notch easier to use.

Would be great to make a post out of everyone of the items in that Python tricks list you mention... ;)

Thanks again!
Rec

Just what I needed

Anonymous's picture

I was just starting to look around for this solution last night, as a C and PHP programmer but Python n00b. Should help me out a great deal, thanks.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState