Staging
v0.8.1
https://github.com/python/cpython
Revision 6c52d76db8b1cb836c136bd6a1044e85bfe8241e authored by Georg Brandl on 05 March 2011, 13:55:46 UTC, committed by Georg Brandl on 05 March 2011, 13:55:46 UTC
1 parent ccffc9e
Raw File
Tip revision: 6c52d76db8b1cb836c136bd6a1044e85bfe8241e authored by Georg Brandl on 05 March 2011, 13:55:46 UTC
Close 2.3 branch.
Tip revision: 6c52d76
required_1.py
import optparse

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("-v", action="count", dest="verbose")
parser.add_option("-f", "--file", default=None)
(options, args) = parser.parse_args()

print "verbose:", options.verbose
print "file:", options.file
parser.check_required("-f")
back to top