[getopt-sig] Separation of mechanics from semantics

Greg Ward gward@python.net
Fri, 15 Feb 2002 13:43:32 -0500


On 14 February 2002, Patrick Callahan said:
> What kinds of semantics do people want to represent by having an option with 
> an optional option argument.  What would the difference be between 
> "-x "and "-x value"?  When people talk about allowing options to have 
> optional arguments do they usually want to have the option without the 
> argument represent some default value or do they have some other use in mind?

I'm one of the people who has, on occasion, wanted to have optional
option arguments.  The only example I can think of offhand is in the
Distutils, specifically the --home option to the "install" command.

Background: usually when you install something with the Distutils, it
goes to the standard system-wide location for third-party Python
modules, eg. /usr/local/lib/python2.2/site-packages on Unix.  (Same idea
on Windows and Mac OS, but the layout is of course different.)
Sometimes that's not what you want, and my guess was that the most
common "non-standard" installation would be to your home directory (at
least on Unix).  The way I *wanted* this to work was

  setup.py install             # install to system dir
  setup.py install --home      # install to my home dir (~/lib/python)
  setup.py install --home=/tmp # install as though my home dir were
                               # /tmp (/tmp/lib/python)

But since the Distutils use getopt underneath, an option must either
take an argument or not.  So the second version has to be spelled like
this:

  setup.py install --home=~

...and I had to write code that specifically handles "~", because not
all shells will do it in this context (ie. not at the beginning of a
word).  (Of course, you could also write "--home=$HOME" or "--home ~",
but I wanted "--home=~" to just work.)

This is not a big deal, and I don't think it kills the usability of the
Distutils.  It's just a very small wart that I once thought could be
cured by having optional option arguments.  The more I think about it,
the less convinced that they're a good idea.  At some point, I'll
probably try to implement them experimentally in Optik, and that will
decide me.  Most likely the extra complexity and syntactic ambiguity
will kill the idea.

        Greg
-- 
Greg Ward - Unix bigot                                  gward@python.net
http://starship.python.net/~gward/
Drive defensively -- buy a tank.