From abb351362b4075603f829b0dd9cc9aeedaa6f70b Mon Sep 17 00:00:00 2001
From: Steven Knight <knight@baldmt.com>
Date: Sun, 6 Mar 2005 00:45:02 +0000
Subject: Make the GenerateHelpText format easily configurable.

---
 doc/man/scons.1                          | 38 +++++++++++++++++++++---
 src/CHANGES.txt                          |  3 ++
 src/engine/SCons/Options/OptionsTests.py | 51 ++++++++++++++++++++++++++++++++
 src/engine/SCons/Options/__init__.py     | 19 ++++++++----
 4 files changed, 101 insertions(+), 10 deletions(-)

diff --git a/doc/man/scons.1 b/doc/man/scons.1
index ae57f82..3aac25c 100644
--- a/doc/man/scons.1
+++ b/doc/man/scons.1
@@ -7723,6 +7723,18 @@ the Environment() function:
 env = Environment(options=opts)
 .EE
 
+.IP
+The text file(s) that were specified
+when the Options object was created
+are executed as Python scripts,
+and the values of (global) Python variables set in the file
+are added to the construction environment.
+Example:
+
+.ES
+CC = 'my_cc'
+.EE
+
 .TP
 .RI Save( filename ", " env )
 This saves the currently set options into a script file named  
@@ -7766,12 +7778,30 @@ Help(opts.GenerateHelpText(env))
 Help(opts.GenerateHelpText(env, sort=cmp))
 .EE
 
-The text based SConscript file is executed as a Python script, and the
-global variables are queried for customizable construction
-variables. Example:
+.TP
+.RI FormatOptionHelpText( env ", " opt ", " help ", " default ", " actual )
+This method returns a formatted string
+containing the printable help text
+for one option.
+It is normally not called directly,
+but is called by the
+.IR GenerateHelpText ()
+method to create the returned help text.
+It may be overridden with your own
+function that takes the arguments specified above
+and returns a string of help text formatted to your liking.
+Note that the
+.IR GenerateHelpText ()
+will not put any blank lines or extra
+characters in between the entries,
+so you must add those characters to the returned
+string if you want the entries separated.
 
 .ES
-CC = 'my_cc'
+def my_format(env, opt, help, default, actual):
+    fmt = "\n%s: default=%s actual=%s (%s)\n"
+    return fmt % (opt, default. actual, help)
+opts.FormatOptionHelpText = my_format
 .EE
 
 To make it more convenient to work with customizable Options,
diff --git a/src/CHANGES.txt b/src/CHANGES.txt
index 2fa54ab..0535e10 100644
--- a/src/CHANGES.txt
+++ b/src/CHANGES.txt
@@ -225,6 +225,9 @@ RELEASE 0.97 - XXX
   - Enhanced the SCons setup.py script to install man pages on
     UNIX/Linux systems.
 
+  - Add support for an Options.FormatOptionHelpText() method that can
+    be overridden to customize the format of Options help text.
+
   From Wayne Lee:
 
   - Avoid "maximum recursion limit" errors when removing $(-$) pairs
diff --git a/src/engine/SCons/Options/OptionsTests.py b/src/engine/SCons/Options/OptionsTests.py
index b99aa98..96f2e2b 100644
--- a/src/engine/SCons/Options/OptionsTests.py
+++ b/src/engine/SCons/Options/OptionsTests.py
@@ -375,6 +375,7 @@ class OptionsTestCase(unittest.TestCase):
                                 'THIS_SHOULD_WORK' : 'baz' })
 
     def test_GenerateHelpText(self):
+        """Test generating the default format help text"""
         opts = SCons.Options.Options()
 
         opts.Add('ANSWER',
@@ -430,6 +431,56 @@ B: b - alpha test
 """
         text = opts.GenerateHelpText(env, sort=cmp)
         assert text == expectAlpha, text
+
+    def test_FormatOptionHelpText(self):
+        """Test generating custom format help text"""
+        opts = SCons.Options.Options()
+
+        def my_format(env, opt, help, default, actual):
+            return '%s %s %s %s\n' % (opt, default, actual, help)
+
+        opts.FormatOptionHelpText = my_format
+
+        opts.Add('ANSWER',
+                 'THE answer to THE question',
+                 "42",
+                 check,
+                 lambda x: int(x) + 12)
+
+        opts.Add('B',
+                 'b - alpha test',
+                 "42",
+                 check,
+                 lambda x: int(x) + 12)
+
+        opts.Add('A',
+                 'a - alpha test',
+                 "42",
+                 check,
+                 lambda x: int(x) + 12)
+
+        env = Environment()
+        opts.Update(env, {})
+
+        expect = """\
+ANSWER 42 54 THE answer to THE question
+B 42 54 b - alpha test
+A 42 54 a - alpha test
+"""
+
+        text = opts.GenerateHelpText(env)
+        assert text == expect, text
+
+        expectAlpha = """\
+A 42 54 a - alpha test
+ANSWER 42 54 THE answer to THE question
+B 42 54 b - alpha test
+"""
+        text = opts.GenerateHelpText(env, sort=cmp)
+        assert text == expectAlpha, text
+
+
+    
         
 if __name__ == "__main__":
     suite = unittest.makeSuite(OptionsTestCase, 'test_')
diff --git a/src/engine/SCons/Options/__init__.py b/src/engine/SCons/Options/__init__.py
index fdaee92..f5f1b8d 100644
--- a/src/engine/SCons/Options/__init__.py
+++ b/src/engine/SCons/Options/__init__.py
@@ -30,6 +30,7 @@ customizable variables to an SCons build.
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import os.path
+import string
 
 import SCons.Errors
 import SCons.Util
@@ -223,11 +224,17 @@ class Options:
         else:
             options = self.options
 
-        for option in options:
-            help_text = help_text + '\n%s: %s\n    default: %s\n'%(option.key, option.help, option.default)
-            if env.has_key(option.key):
-                help_text = help_text + '    actual: %s\n'%env.subst('${%s}'%option.key)
+        def format(opt, self=self, env=env):
+            if env.has_key(opt.key):
+                actual = env.subst('${%s}' % opt.key)
             else:
-                help_text = help_text + '    actual: None\n'
+                actual = None
+            return self.FormatOptionHelpText(env, opt.key, opt.help, opt.default, actual)
+        lines = filter(None, map(format, options))
 
-        return help_text
+        return string.join(lines, '')
+
+    format = '\n%s: %s\n    default: %s\n    actual: %s\n'
+
+    def FormatOptionHelpText(self, env, key, help, default, actual):
+        return self.format % (key, help, default, actual)
-- 
cgit v0.12