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".
- Android Candy: Google Keep
- Readers' Choice Awards 2014
- Handling the workloads of the Future
- How Can We Get Business to Care about Freedom, Openness and Interoperability?
- Synchronize Your Life with ownCloud
- diff -u: What's New in Kernel Development
- Days Between Dates?
- Computing without a Computer
- December 2014 Issue of Linux Journal: Readers' Choice
Editorial Advisory Panel
Thank you to our 2014 Editorial Advisors!
- Jeff Parent
- Brad Baillio
- Nick Baronian
- Steve Case
- Chadalavada Kalyana
- Caleb Cullen
- Keir Davis
- Michael Eager
- Nick Faltys
- Dennis Frey
- Philip Jacob
- Jay Kruizenga
- Steve Marquez
- Dave McAllister
- Craig Oda
- Mike Roberts
- Chris Stark
- Patrick Swartz
- David Lynch
- Alicia Gibb
- Thomas Quinlan
- Carson McDonald
- Kristen Shoemaker
- Charnell Luchich
- James Walker
- Victor Gregorio
- Hari Boukis
- Brian Conner
- David Lane