Python command-line option parser
Just a note about the command-line parser in my iTunes python script. Back in Python 2.2 (Mac OS X 10.2) I needed a simpler and more versatile command-line parser than getopt, so I wrote commandline.py. Erik Osheim points out that I don’t need to use it any more: as of Python 2.3 there is a new command-line options parser called optparse that does everything I need, and it comes standard with Python 2.3 and greater.
It’s also easier to use and works better. Last weekend I took a close look at it and will be replacing commandline.py in my other scripts when I have time. Here’s a simple example of how it works, from a more recent script:
[toggle code]
- from optparse import OptionParser
- #set up command-line options
- parser = OptionParser()
- parser.add_option("-s", "--slug", help="start with page matching slug", metavar="SLUG", dest="pageslug")
- parser.add_option("-l", "--local", help="do not upload", default=True, action="store_false", dest="upload")
- parser.add_option("-r", "--no-recurse", help="do not publish child pages", default=True, action="store_false", dest="recurse")
- #grab options
- (options, args) = parser.parse_args()
-
#do web pages
-
if options.pageslug:
- page = getPageFromSlug(options.pageslug)
-
else:
- page = getPageFromPath("", host="www.hoboes.com")
-
if page:
- makePage(page, upload=options.upload, recurse=options.recurse)
-
else:
- print "Could not find a base page"
-
if options.pageslug:
This is from the publish script that I use to publish the on-line version of Negative Space from my local Django-managed database. It sets up three options:
- a “slug” option that tells the script to start with a specific page (identified by its “slug”);
- a “local only” flag that tells the script not to upload files to the server;
- a “do not recurse” flag that tells the script to do only the specified page and not any of its subpages.
Most of it is self-explanatory, but the “metavar” is the name used in the help for that option’s required text, and “dest” is the name of the property you’ll use to access the value of that option. You can see that I check “options.pageslug” to decide whether to start from a specific page or start from the home page, and I use options.upload and options.recurse to set those arguments in the “makePage” function.
The parser automatically sets up a “--help” option, and the “parse_args()” method automatically stops the script if --help is requested or if the user specifies invalid options.
Here is the help generated by the above script:
7: bin/publish -help usage: publish [options] options: -h, --help show this help message and exit -sSLUG, --slug=SLUG start with page matching slug -l, --local do not upload -r, --no-recurse do not publish child pages 8:
You can also specify a “type” for each option requiring that the value be a number or a choice from a list of choices.
One thing it doesn’t do, which I’d been hoping for (my commandline.py doesn’t do it either), is a “conditional default” that is conditional to the option being specified. For example, I have an option called “--upcoming” in my iTunes script that takes a number as its value: the number of upcoming tracks to display. I’d like to be able to specify --upcoming both with and without a number, applying a default of 10 only if --upcoming was specified on the command-line but was specified with no number.
This would of course be difficult to parse (it would need to determine whether it should or should not consume the next item on the command line) but would be useful. It might be possible to add this feature by extending the available actions, or perhaps I’m missing something in the on-line docs.
In response to Using appscript in Python to control GUI applications: The appscript module for Python lets you control scriptable applications on the command line without having to coordinate your command-line script with your Applescript applications.
- optparse—More powerful command line option parser
- “optparse is a more convenient, flexible, and powerful library for parsing command-line options than getopt. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser, populate it with options, and parse the command line. optparse allows users to specify options in the conventional GNU/POSIX syntax, and additionally generates usage and help messages for you.”
- Erik Osheim
- “Interests include tool-using, violence, computers, electricity, and romantic walks in the evening.”
- Django
- “Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.” Oh, the sweet smell of pragmatism.
More Python
- Quick-and-dirty old-school island script
- Here’s a Python-based island generator using the tables from the Judges Guild Island Book 1.
- Astounding Scripts on Monterey
- Monterey removes Python 2, which means that you’ll need to replace it if you’re still using any Python 2 scripts; there’s also a minor change with Layer Windows and GraphicConverter.
- Goodreads: What books did I read last week and last month?
- I occasionally want to look in Goodreads for what I read last month or last week, and that currently means sorting by date read and counting down to the beginning and end of the period in question. This Python script will do that search on an exported Goodreads csv file.
- Test classes and objects in python
- One of the advantages of object-oriented programming is that objects can masquerade as each other.
- Timeout class with retry in Python
- In Paramiko’s ssh client, timeouts don’t seem to work; a signal can handle this—and then can also perform a retry.
- 30 more pages with the topic Python, and other related pages
You may be interested in a little Python module I wrote to make handling of command line arguments even easier (open source and free to use) - http://freshmeat.net/projects/commando
Mufasa at 2:19 p.m. February 6th, 2011
HCq51