Re: Suggested optparse enhancement

Greg Ward <[email protected]> Tue, 27 Jul 2004 21:15:11 -0400
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
On 03 July 2004, Allan B. Wilson said:
> [I have submitted this as a feature request on sourceforge.net, but wanted
> to solicit input from list members as well.] 

Sorry for the long delay -- I was on vacation, then too busy enjoying
the summer to spend time on the computer, and then busy getting Optik
1.5a1 ready.  *Now* I can take a look at this.

> Of course, on Unix/Linux I could just define a shell alias that sets
> particular options, but decided it would be more convenient to have optparse
> set defaults from a .ini file (if it exists). This way I can create a
> different .ini file as appropriate for each system (or situation) and
> optparse will set "local" defaults.

Hmmm.  Afraid I don't like it -- it feels too much like it's imposing
policy, rather than providing mechanism.  What if I want my application
to use an environment variable instead?  Or a ~/.myapprc file?
Requiring a config file in the same directory as the script is a bad
idea -- it means ordinary users can't customize the behaviour of
/usr/local/bin/myapp!  Imposing both location and syntax of the proposed
config file really bothers me.  (And I have a deep, ingrained loathing
of Python's ConfigParser module that I really cannot put my finger on.
Sigh.)

Oh, one other thing:

> 	def add_option(self, *args, **kwargs):
> 		if self.cp and kwargs.has_key("default") and kwargs.has_key("dest") and self.cp.has_option("Defaults", kwargs["dest"]):
> 			argstring = self.cp.get("Defaults", kwargs["dest"])
> 			argtype = type(kwargs["default"])
> 			if argtype is bool: kwargs["default"] = bool(argstring.capitalize() == "True" or argstring == "1" or argstring.capitalize() == "Yes")
> 			elif argtype is float: kwargs["default"] = float(argstring)
> 			elif argtype is int: kwargs["default"] = int(argstring)
> 			elif argtype is long: kwargs["default"] = long(argstring)
> 			elif argtype is str: kwargs["default"] = str(argstring)
> 		OptionParser.add_option(self, *args, **kwargs)

This is wrong.  You should be digging through the option value
converters in self.option_class.TYPE_CHECKER to convert values.  Take a
look at how default values are handled in Optik 1.5 (or current CVS) --
see OptionParser.get_default_values().

Oh, and finally: please don't post code with lines longer than 80
columns; it's really ugly and hard to read.

        Greg
-- 
Greg Ward <[email protected]>                         http://www.gerg.ca/
I'm a lumberjack and I'm OK / I sleep all night and I work all day


-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click