Index: Doc/library/optparse.rst =================================================================== --- Doc/library/optparse.rst (revision 76557) +++ Doc/library/optparse.rst (working copy) @@ -157,10 +157,24 @@ an option that must be supplied on the command-line; note that the phrase "required option" is self-contradictory in English. :mod:`optparse` doesn't prevent you from implementing required options, but doesn't give you much - help at it either. See ``examples/required_1.py`` and - ``examples/required_2.py`` in the :mod:`optparse` source distribution for two - ways to implement required options with :mod:`optparse`. + help at it either. For example, a simple implementation of required options + can be implemented as:: + class OptionParser(optparse.OptionParser): + + def check_required (self, opt): + option = self.get_option(opt) + + # Assumes the option's 'default' is set to None! + if getattr(self.values, option.dest) is None: + self.error("%s option not supplied" % option) + + parser = OptionParser() + parser.add_option("-f", "--file", default=None) + (options, args) = parser.parse_args() + + parser.check_required("-f") + For example, consider this hypothetical command-line:: prog -v --report /tmp/report.txt foo bar