[New-bugs-announce] [issue6535] optparse required field for Options

Daniel Kaplun report at bugs.python.org
Tue Jul 21 21:27:49 CEST 2009

New submission from Daniel Kaplun <min at dvir.us>:

In the second example to allow usage of the required field for options,
it seems as if you already have all you need to implement the feature
into optparse. I modified it a bit to allow OptionGroups:

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",

	# 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):
		self.option_seen = {}

	def check_values(self, values, args):
		for option in self.option_list + sum((optiongroup.option_list for
optiongroup in self.option_groups), []):
			if isinstance(option, Option) and option.required and not
				self.error("%s not supplied" % (option, ))
		return (values, args)

The question: why hasn't your existing example been merged with the
OptionParse code to allow the required field for options?

components: Extension Modules
messages: 90767
nosy: mindvirus
severity: normal
status: open
title: optparse required field for Options
versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1, Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list