summaryrefslogtreecommitdiffstats
path: root/Doc/lib/required_2.py
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2003-01-06 16:51:37 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2003-01-06 16:51:37 (GMT)
commit488609e43af797bcc23beb5d8d20731a45d8a982 (patch)
tree871ee52d3cf56d47225a6c1924c1bdb1b4a4d9dc /Doc/lib/required_2.py
parent11f89b75e1445e430964bf71047f3a7419c59fb4 (diff)
downloadcpython-488609e43af797bcc23beb5d8d20731a45d8a982.zip
cpython-488609e43af797bcc23beb5d8d20731a45d8a982.tar.gz
cpython-488609e43af797bcc23beb5d8d20731a45d8a982.tar.bz2
SF #642236, optparse LaTeX docs by Johannes Gijsbers
Diffstat (limited to 'Doc/lib/required_2.py')
-rwxr-xr-xDoc/lib/required_2.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Doc/lib/required_2.py b/Doc/lib/required_2.py
new file mode 100755
index 0000000..421514a
--- /dev/null
+++ b/Doc/lib/required_2.py
@@ -0,0 +1,41 @@
+import optparse
+
+class Option (optparse.Option):
+ ATTRS = optparse.Option.ATTRS + ['required']
+
+ def _check_required (self):
+ if self.required and not self.takes_value():
+ raise OptionError(
+ "required flag set for option that doesn't take a value",
+ self)
+
+ # Make sure _check_required() is called from the constructor!
+ CHECK_METHODS = optparse.Option.CHECK_METHODS + [_check_required]
+
+ def process (self, opt, value, values, parser):
+ optparse.Option.process(self, opt, value, values, parser)
+ parser.option_seen[self] = 1
+
+
+class OptionParser (optparse.OptionParser):
+
+ def _init_parsing_state (self):
+ optparse.OptionParser._init_parsing_state(self)
+ self.option_seen = {}
+
+ def check_values (self, values, args):
+ for option in self.option_list:
+ if (isinstance(option, Option) and
+ option.required and
+ not self.option_seen.has_key(option)):
+ self.error("%s not supplied" % option)
+ return (values, args)
+
+
+parser = OptionParser(option_list=[
+ Option("-v", action="count", dest="verbose"),
+ Option("-f", "--file", required=1)])
+(options, args) = parser.parse_args()
+
+print "verbose:", options.verbose
+print "file:", options.file