[RFC] use optparse for option handling

Aaron Bentley aaron.bentley at utoronto.ca
Fri Jul 14 03:26:16 BST 2006

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:]
            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,
            negation_strings = ['--%s' % self.get_negation_name()]
            parser.add_option(action='store_const', dest=self.name,

> 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.

Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the bazaar mailing list