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.

Title: optparse required field for Options
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.2, Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, mindvirus, r.david.murray
Priority: normal Keywords: easy, patch

Created on 2009-07-21 19:27 by mindvirus, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit mindvirus, 2009-07-21 22:08 Optparse patch mindvirus, 2009-07-21 22:09 Test case
Messages (6)
msg90767 - (view) Author: [] (mindvirus) Date: 2009-07-21 19:27
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?
msg90773 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-07-21 20:44
Feature requests can only go into releases under development.

No guarantees, but if you want to give this a better chance of getting
in a patch against trunk including tests would be a minimum prerequisite.

Alternatively or in addition you could help review argparse for
inclusion into the stdlib (see issue 6247; I know the issue is closed
but it could still be moved forward if enough interest is expressed).
msg90777 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-07-21 22:57
What we are looking for in the way of tests is unit tests to add to
test_optparse.  Another piece that I forgot about that we'll need is a
documentation patch.

Finally, it will be easier to review the patch if you don't make other
changes in the patch.  Your PEP 8 changes are good in principle, but
they make it harder to review the patch.
msg90781 - (view) Author: [] (mindvirus) Date: 2009-07-21 23:24
I have not a single clue how to unit test.

What do you mean by documentation patch? Do you want me to update the
documentation within the file to reflect changes?
msg90783 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-07-22 00:03
Well, if you get check out the current trunk via svn (as described in
the developer's faq linked from and you look
at what Lib/test/ currently does, you might find it is
not too hard to add some new cases to test the new code.  Then in
Doc/library/optparse.rst, you make appropriate changes to the
documentation RestructuredTest source code, and post everything in a
single patch.

If you don't want to do all this that's fine, the ticket will sit here
until someone comes along who wants to move it forward ;)

Thanks for taking an interest in this.
msg111975 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-07-29 17:17
I don't think optparse will get this update -- new features should go into argparse instead.
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50784
2010-07-29 17:17:58georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg111975

resolution: out of date
2009-07-22 00:03:22r.david.murraysetmessages: + msg90783
2009-07-21 23:24:44mindvirussetmessages: + msg90781
2009-07-21 22:57:55r.david.murraysetmessages: + msg90777
2009-07-21 22:09:09mindvirussetfiles: +
2009-07-21 22:08:39mindvirussetfiles: +
keywords: + patch
2009-07-21 20:44:46r.david.murraysetpriority: normal

components: + Library (Lib), - Extension Modules
versions: - Python 2.6, Python 2.5, Python 2.4, Python 3.0, Python 3.1
keywords: + easy
nosy: + r.david.murray

messages: + msg90773
stage: test needed
2009-07-21 19:29:05mindvirussettype: enhancement
2009-07-21 19:27:47mindviruscreate