Today I learned that Python's argparse module allows you to abbreviate long command line options
Argparse is the
standard Python library for Unix style command line argument handling;
I use it in all of my Python programs these days. As part of this
it supports the now standard long options, so you can have options
--dead-disks' instead of just '
-D' (there are only so
many single characters to go around, and they can be hard to
remember). Today I learned that argparse accepts abbreviations
for these long options, provided that they're unambiguous. If you
have a long option '
--dead-disks' and no other long option that
starts with 'd', argparse will accept all of '--dead-disk', '--dead',
and even '--d' as meaning '--dead-disks'.
This is clearly documented in the argparse documentation if you
bother to read it all (I never did), in Argument abbreviations
(prefix matching). You
can turn it off when constructing your ArgumentParser
by setting the
keyword argument to '
false'. Unfortunately I don't think there's
anything visible in a program's --help that will tell you whether
or not it accepts abbreviated options; you have to either read
the Python code or just try it with something harmless.
(It appears that optparse, argparse's predecessor, always allowed abbreviations, with no way to turn it off I'm basing this on a little mention of abbreviated long options in this section. This makes argparse a clear improvement here, since at least you can turn abbreviated options off if you want to.)
With my user hat on, I think this is a fine little feature. Long options are often, well, long, and if you can abbreviate them you might as well so people don't have to type as much (at least the people who don't have command line completion for options for your programs).
With my sysadmin hat on, I'm worried about the implications of abbreviated options accidentally getting embedded into other scripts, crontab entries, and so on. For instance, if the real option is '--dead-disks' but it's usually used with a single disk name, it would be easy to accidentally forget that it wasn't '--dead-disk' and embed that mistake in a script. Although this works today, it risks confusion in people who later read the script (including your future self). With heavily abbreviated options, evolving the program to add more options risks now making a previously unambiguous and working abbreviation now ambiguous and not working. If you add a new '--deadline' argument and scripts were using '--dead' as an abbreviation for '--dead-disks', suddenly they're going to start getting errors.
(You can think of this as a version of Postel's law, in which case The Harmful Consequences of the Robustness Principle sort of applies.)
Given this concern, it's tempting to update at least some of our sysadmin tools to disable abbreviated command line options and perhaps to make it my default argparse setting in future tools.