6.21.1.1 Terminology
- a string entered on the command-line, and passed by the shell to
execl()orexecv(). In Python, arguments are elements ofsys.argv[1:](sys.argv[0]is the name of the program being executed). Unix shells also use the term ``word''.It is occasionally desirable to substitute an argument list other than
sys.argv[1:], so you should read ``argument'' as ``an element ofsys.argv[1:], or of some other list provided as a substitute forsys.argv[1:]''. - an argument used to supply extra information to guide or customize the
execution of a program. There are many different syntaxes for
options; the traditional Unix syntax is a hyphen (``-'') followed by a
single letter, e.g.
"-x"or"-F". Also, traditional Unix syntax allows multiple options to be merged into a single argument, e.g."-x -F"is equivalent to"-xF". The GNU project introduced"-"followed by a series of hyphen-separated words, e.g."-file"or"-dry-run". These are the only two option syntaxes provided by optparse.Some other option syntaxes that the world has seen include:
-
a hyphen followed by a few letters, e.g.
"-pf"(this is not the same as multiple options merged into a single argument) -
a hyphen followed by a whole word, e.g.
"-file"(this is technically equivalent to the previous syntax, but they aren't usually seen in the same program) -
a plus sign followed by a single letter, or a few letters,
or a word, e.g.
"+f","+rgb" -
a slash followed by a letter, or a few letters, or a word, e.g.
"/f","/file"
These option syntaxes are not supported by optparse, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you're exclusively targeting VMS, MS-DOS, and/or Windows.
-
a hyphen followed by a few letters, e.g.
- an argument that follows an option, is closely associated with that
option, and is consumed from the argument list when that option is.
With optparse, option arguments may either be in a separate argument
from their option:
-f foo --file foo
or included in the same argument:
-ffoo --file=foo
Typically, a given option either takes an argument or it doesn't. Lots of people want an ``optional option arguments'' feature, meaning that some options will take an argument if they see it, and won't if they don't. This is somewhat controversial, because it makes parsing ambiguous: if
"-a"takes an optional argument and"-b"is another option entirely, how do we interpret"-ab"? Because of this ambiguity, optparse does not support this feature. - something leftover in the argument list after options have been parsed, i.e. after options and their arguments have been parsed and removed from the argument list.
- an option that must be supplied on the command-line; note that the
phrase ``required option'' is self-contradictory in English. optparse
doesn't prevent you from implementing required options, but doesn't
give you much help at it either. See
examples/required_1.pyandexamples/required_2.pyin the optparse source distribution for two ways to implement required options with optparse.
For example, consider this hypothetical command-line:
prog -v --report /tmp/report.txt foo bar
"-v" and "-report" are both options. Assuming that
--report takes one argument, "/tmp/report.txt" is an option
argument. "foo" and "bar" are positional arguments.
See About this document... for information on suggesting changes.





