14.3.3.7 Parsing arguments

The whole point of creating and populating an OptionParser is to call its parse_args() method:

(options, args) = parser.parse_args(args=None, values=None)

where the input parameters are

args
the list of arguments to process (default: sys.argv[1:])
values
object to store option arguments in (default: a new instance of optparse.Values)

and the return values are

options
the same object that was passed in as options, or the optparse.Values instance created by optparse
args
the leftover positional arguments after all options have been processed

The most common usage is to supply neither keyword argument. If you supply options, it will be modified with repeated setattr() calls (roughly one for every option argument stored to an option destination) and returned by parse_args().

If parse_args() encounters any errors in the argument list, it calls the OptionParser's error() method with an appropriate end-user error message. This ultimately terminates your process with an exit status of 2 (the traditional Unix exit status for command-line errors).

See About this document... for information on suggesting changes.