Adjust indentation level in multiline docstrings
authorSergey Astanin <s.astanin@gmail.com>
Thu, 17 Feb 2011 22:25:22 +0100
changeset 181 c3cb22c01987
parent 180 bcc87f3710b5
child 182 f8a82de0540d
child 183 72ee7b8c0821
Adjust indentation level in multiline docstrings
opster.py
tests/opster.t
--- a/opster.py	Thu Feb 17 15:35:05 2011 +0100
+++ b/opster.py	Thu Feb 17 22:25:22 2011 +0100
@@ -203,7 +203,7 @@
                 if shortlist and not cmd.startswith('^'):
                     continue  # short help contains only marked commands
                 cmd = cmd.lstrip('^~')
-                doc = info[0].__doc__ or '(no help text available)'
+                doc = pretty_doc_string(info[0])
                 hlp[cmd] = doc.strip().splitlines()[0].rstrip()
 
             hlplist = sorted(hlp)
@@ -262,7 +262,7 @@
         --pid-file   name of file to write process ID to
     '''
     write(usage + '\n')
-    doc = func.__doc__ or '(no help text available)'
+    doc = pretty_doc_string(func)
     write('\n' + doc.strip() + '\n\n')
     if options:
         write(''.join(help_options(options)))
@@ -571,6 +571,15 @@
         return name[2:]
     return name
 
+def pretty_doc_string(item):
+    "Doc string with adjusted indentation level of the 2nd line and beyond."
+    raw_doc = item.__doc__ or '(no help text available)'
+    lines = raw_doc.strip().splitlines()
+    if len(lines) <= 1:
+        return raw_doc
+    indent = len(lines[1]) - len(lines[1].lstrip())
+    return '\n'.join([lines[0]] + map(lambda l: l[indent:], lines[1:]))
+
 try:
     from functools import wraps
 except ImportError:
--- a/tests/opster.t	Thu Feb 17 15:35:05 2011 +0100
+++ b/tests/opster.t	Thu Feb 17 22:25:22 2011 +0100
@@ -171,4 +171,21 @@
       --assist  show help
    -h --help    show help
 
+
+Are we getting nicely stripped body when not following subject/body convention
+of writing commands?
+
+  $ run hello.py --help
+  hello.py [options]
+  
+  Hello world continues the long established tradition
+  of delivering simple, but working programs in all
+  kinds of programming languages.
+  
+  options:
+  
+   -n --name  your name (default: world)
+   -h --help  show help
+
+
 That's all for today; see you next time!