Dealing with Command Line Options in Python
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
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
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.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- RSS Feeds
- Senior Perl Developer
- Technical Support Rep
- Introduction to MapReduce with Hadoop on Linux
- Weechat, Irssi's Little Brother
- What the author describes
10 min 49 sec ago - Reply to comment | Linux Journal
4 hours 21 min ago - Reply to comment | Linux Journal
5 hours 6 min ago - Didn't read
5 hours 16 min ago - Reply to comment | Linux Journal
5 hours 21 min ago - Poul-Henning Kamp: welcome to
7 hours 31 min ago - This has already been done
7 hours 32 min ago - Reply to comment | Linux Journal
8 hours 18 min ago - Welcome to 1998
9 hours 6 min ago - notifier shortcomings
9 hours 30 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Specifying a file name containing a space
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
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.filethe 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
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
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
Very interesting post, I was reading everything and I liked a lot, Keep it!
getopt
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
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!
- Recetas España
Rec
Just what I needed
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.