[PATCH] %default keyword expansion

John Belmonte <[email protected]> Tue, 23 Mar 2004 10:00:22 -0500
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
Greg,

Please consider the attached Optik patch, which expands occurrences of 
"%default" in a help string with the option's default value.

Here's an example:

$ cat test-opt.py
from optparse import OptionParser

class MyOptionParser(OptionParser):
     def __init__(self):
         OptionParser.__init__(self, usage='usage: %prog [options]')
         self.add_option(
             '--max-foo',
             type='int',
             default=10,
             help='maximum allowable foo [default: %default]')

parser = MyOptionParser()
options, args = parser.parse_args()

$ python test-opt.py -h
usage: test-opt.py [options]

options:
   -h, --help         show this help message and exit
   --max-foo=MAX_FOO  maximum allowable foo [default: 10]


Regards,
-John Belmonte


-- 
http:// if  ile.org/
optik-default-expansion.patch (text/x-patch, 714 B)
diff -urN Optik-1.4.1/lib/help.py Optik-1.4.1-new/lib/help.py
--- Optik-1.4.1/lib/help.py	2003-04-20 10:12:14.000000000 -0400
+++ Optik-1.4.1-new/lib/help.py	2004-03-23 09:36:52.000000000 -0500
@@ -91,7 +91,8 @@
             indent_first = 0
         result.append(opts)
         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)
             result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
             result.extend(["%*s%s\n" % (self.help_position, "", line)
                            for line in help_lines[1:]])