File: required_1.py

package info (click to toggle)
python2.5-doc 2.5-4
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 7,816 kB
  • ctags: 2,720
  • sloc: python: 5,817; perl: 3,674; sh: 2,110; makefile: 1,355; xml: 836; lisp: 836; ansic: 719; sed: 2
file content (20 lines) | stat: -rwxr-xr-x 565 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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")