Re: [PATCH] %default keyword expansion

Greg Ward <[email protected]> Wed, 31 Mar 2004 20:32:08 -0500
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
On 23 March 2004, John Belmonte said:
> Please consider the attached Optik patch, which expands occurrences of 
> "%default" in a help string with the option's default value.

Not sure this is entirely the right way to do it.  But then, I'm not
sure what the right way *is*, which is why I never implemented this
feature for Optik.

First, a philosophical digression which is only slightly related to your
patch: what about boolean options?  I.e., is there a nice, clean, simple
way for this code:

  parser.set_defaults(quiet=True)
  parser.add_option("-q", "--quiet", action="store_true",
                    help="show only warnings and errors")
  parser.add_option("-v", "--verbose",
                    action="store_false", dest="quiet",
                    help="print informative messages")

to generate this help:

  -q, --quiet       show only warnings and errors [default]
  -v, --verbose     print informative messages [default: -q]

or something like it?

To be honest, I'm not sure if this is desirable.  Someone has to decide
which of these:

  [default]
  (DEFAULT)
  [DEFAULT: true]
  ...etc...

is preferable, and I think it should be the application writer, not
Optik.  Also, I'm not sure if the "[default: -q]" that I put in the help
for -v above is a good idea -- again, it should be up to the app
developer to make that call.

Of course, your patch takes the smart approach of avoiding the issue
entirely.  On reflection, that's probably the right thing.  Any Optik
users out there who think differently?  Ie. *should* Optik try to solve
the above problem?

Next, implementation detail:
   
>          if option.help:
> -            help_lines = textwrap.wrap(option.help, self.help_width)
> +            help_text = option.help.replace("%default", str(option.default))
> +            help_lines = textwrap.wrap(help_text, self.help_width)

What if someone wants the literal string "%default" in help text?  I
know I don't support that for %prog in the 'version' and 'usage'
strings, but those strings are much smaller and more special-purpose.
(How's that for rationalizing a flaw that has escaped my notice until
now?)

I think the right solution is to expand "%%default" to "%default", and
"%default" to the default value.  [... some minutes pass ...]  Damn, I
tried to do this with a single regex substitution, but my brain
exploded.  (What a mess.)

If anyone knows the right way to implement this substitution, feel free
to post code here.  Otherwise I'll figure something out at my snail-like
pace.  (It's hard to think with an exploded brain.)

        Greg
-- 
Greg Ward <[email protected]>                         http://www.gerg.ca/
"He's dead, Jim.  You get his tricorder and I'll grab his wallet."


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click