6.20.1.1 Terminology

First, we need to establish some terminology.

argument
a chunk of text that a user enters on the command-line, and that the shell passes to execl() or execv(). In Python, arguments are elements of sys.argv[1:]. (sys.argv[0] is the name of the program being executed; in the context of parsing arguments, it's not very important.) Unix shells also use the term ``word''.

It is occasionally desirable to use an argument list other than sys.argv[1:], so you should read ``argument'' as ``an element of sys.argv[1:], or of some other list provided as a substitute for sys.argv[1:]''.

option
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 - 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:

optparse does not support these option syntaxes, and it never will. (If you really want to use one of those option syntaxes, you'll have to subclass OptionParser and override all the difficult bits. But please don't! optparse does things the traditional Unix/GNU way deliberately; the first three are non-standard anywhere, and the last one makes sense only if you're exclusively targeting MS-DOS/Windows and/or VMS.)

option argument
an argument that follows an option, is closely associated with that option, and is consumed from the argument list when the option is. Often, option arguments may also be included in the same argument as the option, e.g. :

    ["-f", "foo"]

may be equivalent to:

    ["-ffoo"]

(optparse supports this syntax.)

Some options never take an argument. Some options always take an argument. 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 and -b are both options, and -a takes an optional argument, how do we interpret -ab? optparse does not support optional option arguments.

positional argument
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.

required option
an option that must be supplied on the command-line. The phrase ``required option'' is an oxymoron; the presence of ``required options'' in a program is usually a sign of careless user interface design. optparse doesn't prevent you from implementing required options, but doesn't give you much help with it either. See ``Extending Examples'' (section 6.20.5) 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 the --report option 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.