[RFC] use optparse for option handling

Aaron Bentley aaron.bentley at utoronto.ca
Fri Jul 14 01:32:31 BST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben Finney wrote:
> Aaron Bentley <aaron.bentley at utoronto.ca> writes:

>> I really want to check identity.  I want to be able to use
>> OptionParser.DEFAULT_VALUE as a special object, the same way None is
>> used.
> 
> If that's truly the best way to implement the functionality, then
> you'll need an object that *does* behave like None. Search for the
> Singleton pattern.

I don't think that facet of None is essential.  Object identity doesn't
care about classes, so it doesn't care whether there are many instances
of a class or (at maximum) one.

> However, that's a bad design smell, and doesn't quite seem to address
> what you're actually looking for.
> 
>> I want the test to evaluate to True if v is another object with the
>> same value as OptionParser.DEFAULT_VALUE.  It should only evaluate
>> to False if v and OptionParser.DEFAULT_VALUE are two names for the
>> same object.
> 
> Why not put that functionality in the object that holds that value,
> and make it cognizant of whether the value is changed?

The problem is that I'm dealing in values.  I pass a bunch of values
into the Optparse parser when I construct it, and I get some of those
values back when I parse the options.

Optparse always provides a value, even if it's the default.  But we
handle default values elsewhere, so we do not want to propagate the
defaults that optparse provides.  In order to safely avoid propagating
the default, we can use object identity to distinguish between the
default and all other values.

>     class Foo(object):
>         """ Hold a value and be aware of whether it's been changed. """
>         bar = property(_get_bar, _set_bar)

It is probably possible to get optparse to manipulate the bar property,
but seems like the result would be uglier than other approaches.

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

iD8DBQFEtuYf0F+nu1YWqI0RApf0AJ9s9PE/A6p/LalFPQ8qrH7OvBs7DgCdGWfo
Q2Ye0MPRcAsuS5nq0z+03jM=
=LyPY
-----END PGP SIGNATURE-----




More information about the bazaar mailing list