3 class Option (optparse
.Option
):
4 ATTRS
= optparse
.Option
.ATTRS
+ ['required']
6 def _check_required (self
):
7 if self
.required
and not self
.takes_value():
9 "required flag set for option that doesn't take a value",
12 # Make sure _check_required() is called from the constructor!
13 CHECK_METHODS
= optparse
.Option
.CHECK_METHODS
+ [_check_required
]
15 def process (self
, opt
, value
, values
, parser
):
16 optparse
.Option
.process(self
, opt
, value
, values
, parser
)
17 parser
.option_seen
[self
] = 1
20 class OptionParser (optparse
.OptionParser
):
22 def _init_parsing_state (self
):
23 optparse
.OptionParser
._init
_parsing
_state
(self
)
26 def check_values (self
, values
, args
):
27 for option
in self
.option_list
:
28 if (isinstance(option
, Option
) and
30 not self
.option_seen
.has_key(option
)):
31 self
.error("%s not supplied" % option
)
35 parser
= OptionParser(option_list
=[
36 Option("-v", action
="count", dest
="verbose"),
37 Option("-f", "--file", required
=1)])
38 (options
, args
) = parser
.parse_args()
40 print "verbose:", options
.verbose
41 print "file:", options
.file