[Python-Dev] Python 2.7b1 and argparse's version action

Tobias Herp Tobias.Herp at gmx.de
Sun Apr 18 12:31:02 CEST 2010


Hi, all,

(I don't know whether the first attempt to post this went through; I
could'nt find it, so I retry.  My apologies for any inconvenience...)

I noticed that Python 2.7 beta 1 now contains the argparse module, which
might be a good thing.  The code has been cleaned up, too, compared to
the current version 1.1.

But there is still one issue with argparse; to tell the story from the
beginning:

The ArgumentParser class uses uses '-v' as a default short option for
the 'tell me the program version' facility.  Since this is commonly used
for verbosity, and the vast majority of *X commandline tools use '-V'
instead (if they bother to accompany '--version' with a short option at
all).  Therefore I submitted an issue report at

<http://code.google.com/p/argparse/issues/detail?id=43>.

To put it short:
*Argparse should simply do this like optparse does already.*
I.e., use '--version', '--help' and '-h' by default, but not '-v'.

Subsequently I spent a hard time arguing.
(Perhaps it was somewhat distracting that I proposed a way of postponing
the creation of the help and version options and to provide them with
the short options which are left unused e.g. for hosts (database
clients), human readability (ls, du, etc.) or verbosity; Steven had
stated it might break backward compatibility if the '-v' option string
for version output would be dropped which might be relied on by other
programs which parse the output.  IMO, however, anyone should be beaten
who relies on '-v' giving the version while '--version' has been present
all the time.  The Zen of Python, 2nd aphorism: "Explicit is better than
implicit")

What happened was the following:
Completely unnecessarily, the 'version' constructor argument is now
deprecated.  This fact doesn't solve any problem I can think of; the
only effect is to make programming more cumbersome, and it is /one more/
difference to optparse.

The 'version' argument is a perfectly reasonable way to provide a script
with a simple version information feature.  Of course, it should only
define the '--version' argument; it *must not* define '-v' for this
purpose, since this is commonly used for verbosity, and '-V' is widely
used to accompany '--version'.  I have lots of scripts which use
optparse, and every single one uses the version argument.  I consider
this a matter of good style.

The deprecation of the 'version' argument press-gangs people to replace
it by
  parser.add_argument('--version', action='version',
                      version='<the version>',    # the only common part
                      help="show program's version number and exit")
in every single script (which violates the DRY principle, by the way).

Of course, if a more fancy version information is needed, e.g. reporting
the versions of all imported modules, it is perfectly possible to just
omit the version argument during creation
and build a special 'version' action.  No deprecation warnings are
needed for this.

*Before Python 2.7 reaches productivity stage*, IMNSHO the following
changes should be applied to argparse.py:

- removal of the deprecation warnings
- removal of the default usage of '-v'
  with the version information facility

This is a very simple thing to do; I'd happily provide a patch.
(The only complicated task might be to make the unit tests reflect the
change;  but for a start, 7 lines of the test_main function could be
dropped.)

Just for the records, this is what optparse does:
- it defines -h and --help for the help (unless suppressed)
- it defines --version for the version (if version argument given)
This is how it should be (regarding the version, at least).
This is how the vast majority of *x tools looks like.
No reason to change this behaviour.

What do you think?

-- 

Cheers, Tobias


More information about the Python-Dev mailing list