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:
optparsemodule 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
-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
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
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".
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Profiles and RC Files
- Understanding Ceph and Its Place in the Market
- Git 2.9 Released
- Astronomy for KDE
- OpenSwitch Finds a New Home
- Maru OS Brings Debian to Your Phone
- What's Our Next Fight?
- The Giant Zero, Part 0.x
- Snappy Moves to New Platforms
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide