6.21.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, options=None)

where the input parameters are

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

and the return values are

options
the same object as was passed in as options, or the new 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 a values object, it will be repeatedly modified with a setattr() call for every option argument written to an option destination, and finally 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.