Opterator 0.5 Supports Function Annotations

By | January 11, 2015

Opterator is a decorator to make quick and clean command line interfaces for scripts. It’s not designed to compete with argparse or click. Instead it supports that common usecase where you’re writing a reusable script and don’t want to write all the bits required for argument parsing, but also don’t want to use the maligned ‘just access sys.argv and pray’ technique.

Say you’ve written a script that makes a thumbnail of an image. It requires the image name, the output thumbnail name, and an optional algorithm. Just do this:

There’s no mucking about with options, and you even get a helptext that is borderline useful:

If that’s not good enough, you can customize the helptext by editing the docstring, as described in the

But opterator has supported that for years. Let’s talk about some new stuff. I recently released version 0.5, which has a few bugfixes and a couple exciting new features that I want to introduce.

First, I ported it from optparse to argparse. This makes opterator handle positional arguments much more neatly, both in the code and in the resulting helptext. It doesn’t really change your interaction with opterator as an api client, but the users will see improved helptext.

Second, I’ve added support for function annotations so you can change the variable name output by opterator. This obviously only works on Python 3, since function annotations are a new feature in Python. Opterator itself, however, is Python 2 and 3 compatible as it has been since 2012. Here’s the same example, but we changed the algorithm argument to be passed in as –scaling or -s:

Here’s the resulting helptext:

The main reason you might want to do this is that you’ve got options that start with the same letter (the first letter is what opterator uses as a short option, by default). Function annotations let you supply other options if you like. Or possibly you just have a burning desire to play with function annotations. Either way, opterator has you covered.

Opterator is well-tested, so I’ve (belatedly) upgraded the classifier from beta to production. It’s a small tool that has saved me a lot of time and many lines of ugly code over the years. I think it’s worth having installed in site- packages so you can write quick scripts without thinking about getting data from the user.

Leave a Reply

Your email address will not be published. Required fields are marked *