[RFC] use optparse for option handling
Aaron Bentley
aaron.bentley at utoronto.ca
Fri Jul 14 03:26:16 BST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Martin Pool wrote:
> On 12 Jul 2006, Aaron Bentley <aaron.bentley at utoronto.ca> wrote:
>> There may also be network effects, e.g. for option completion.
>
> Yes. I just hadn't worked out if there was a good way to e.g. do
> negative options, or options which accumulate a value, etc.
Those are both well-supported. I've actually got support for negative
options in the latest version of that branch:
class Option(object):
"""Description of a command line option"""
.
.
.
def get_negation_name(self):
if self.name.startswith('no-'):
return self.name[3:]
else:
return 'no-' + self.name
def add_option(self, parser, short_name):
"""Add this option to an Optparse parser"""
option_strings = ['--%s' % self.name]
if short_name is not None:
option_strings.append('-%s' % short_name)
optargfn = self.type
if optargfn is None:
parser.add_option(action='store_true', dest=self.name,
help=self.help,
default=OptionParser.DEFAULT_VALUE,
*option_strings)
negation_strings = ['--%s' % self.get_negation_name()]
parser.add_option(action='store_const', dest=self.name,
help=optparse.SUPPRESS_HELP,
const=OptionParser.DEFAULT_VALUE,
*negation_strings)
.
.
.
> If there is
> and the other issues about cost as settled then it's good with me.
>
>>> Also, we want --no-foo to set the inverse of --foo.
>> That's pretty easily done. Should we just generate those options
>> automatically? Should they be hidden options?
>
> I would say we want something like
>
> Option('remember', negatable=True)
I was assuming all booleans should be negatable. Is that wrong?
> it's probably good to show both in the help.
Okay, so that means we won't want to use optparse to generate our option
help, because it wants help for every flag name, and we don't provide
text for the inverted help.
> Relying on ("a" + "b") to not intern to "ab"? It seems possible some
> other or future Python implementation might eval it at compile time and
> then do the wrong thing... maybe this is paranoid.
I've switched to using an object instance, just to be on the safe side.
Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEtwDI0F+nu1YWqI0RAjZ7AJ9OdCooAuKW5H/h0BBTSv8jveFzWwCdHZ7L
ZCCg8ly2htmaJ23abry9W98=
=UzZ6
-----END PGP SIGNATURE-----
More information about the bazaar
mailing list