Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1 | import optparse |
| 2 | |
| 3 | class OptionParser (optparse.OptionParser): |
| 4 | |
| 5 | def check_required (self, opt): |
| 6 | option = self.get_option(opt) |
| 7 | |
| 8 | # Assumes the option's 'default' is set to None! |
| 9 | if getattr(self.values, option.dest) is None: |
| 10 | self.error("%s option not supplied" % option) |
| 11 | |
| 12 | |
| 13 | parser = OptionParser() |
| 14 | parser.add_option("-v", action="count", dest="verbose") |
| 15 | parser.add_option("-f", "--file", default=None) |
| 16 | (options, args) = parser.parse_args() |
| 17 | |
| 18 | print "verbose:", options.verbose |
| 19 | print "file:", options.file |
| 20 | parser.check_required("-f") |