Re: [PATCH] %default keyword expansion
Greg Ward <[email protected]> Wed, 31 Mar 2004 20:45:33 -0500
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
OK, here's my crack at expanding "%%default" and "%default" as explained
in my last message. This should work, but I'm not terribly proud of it
-- surely there's an easier way. Anyone?
--- lib/help.py 27 Aug 2003 02:35:41 -0000 1.6
+++ lib/help.py 1 Apr 2004 01:32:43 -0000
@@ -4,6 +4,10 @@
__all__ = ['HelpFormatter', 'IndentedHelpFormatter', 'TitledHelpFormatter']
+import re
+
+_option_default_re = re.compile(r'%?%default')
+
class HelpFormatter:
@@ -91,7 +95,8 @@
indent_first = 0
result.append(opts)
if option.help:
- help_lines = textwrap.wrap(option.help, self.help_width)
+ help_text = self._expand_default(option.help, 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:]])
@@ -99,6 +104,22 @@
result.append("\n")
return "".join(result)
+ def _expand_default (self, help_text, default):
+ search = _option_default_re.search
+ pos = 0
+ chunks = []
+ match = search(help_text, pos)
+ while match:
+ chunks.append(help_text[pos:match.start()])
+ if match.group(0) == "%%default":
+ chunks.append("%default")
+ else:
+ chunks.append(default)
+ pos = match.end()
+ match = search(help_text, pos)
+ chunks.append(help_text[pos:])
+ return "".join(chunks)
+
def store_option_strings (self, parser):
self.indent()
max_len = 0
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: 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