This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mindvirus
Recipients mindvirus
Date 2009-07-21.19:27:46
SpamBayes Score 4.7855924e-09
Marked as misclassified No
Message-id <1248204469.91.0.747237681609.issue6535@psf.upfronthosting.co.za>
In-reply-to
Content
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",
				 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 + sum((optiongroup.option_list for
optiongroup in self.option_groups), []):
			if isinstance(option, Option) and option.required and not
self.option_seen.has_key(option):
				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?
History
Date User Action Args
2009-07-21 19:27:50mindvirussetrecipients: + mindvirus
2009-07-21 19:27:49mindvirussetmessageid: <1248204469.91.0.747237681609.issue6535@psf.upfronthosting.co.za>
2009-07-21 19:27:47mindviruslinkissue6535 messages
2009-07-21 19:27:46mindviruscreate