getopt

John Machin sjmachin at lexicon.net
Wed Feb 11 03:13:33 EST 2009


On Feb 11, 5:46 pm, Matthew Sacks <ntw... at gmail.com> wrote:
> I didn't realize that the no-value arguments, -b, -h, etc are required?

required sense 1: the 2nd arg of the function is required; if there
are to be no short options, the 2nd arg should be an empty string.

required sense 2: you are required to specify *all* short options that
you will accept, whether they are no-value or value options; otherwise
you would have to do your own checking for unacceptable options

> This seems to make things a bit more difficult considering unless I
> use the GNU style getopt all arguments are required to be passed?

What does "all arguments are required to be passed" mean? The user is
not required to supply an arg corresponding to any option. Options are
optional!

GNU? Change getopt.getopt to getopt.gnu_getopt and you still get an
exception like "GetoptError: option -b not recognized" if -b is not
defined in the 2nd arg of [gnu_]getopt. AFAICT the GNU difference is
only the documented different treatment of an arg string like "-a1 -b
bvalue stray -c -d" ... non-Gnu treats -c and -d the same as stray.

> I will have a look at what you have posted here and report my results.

Just bear in mind that it was written under the assumption that you
weren't plannning to use short options at all; your examples left lack
for want.

Cheers,
John



More information about the Python-list mailing list