usage for command should be separate string
authorAlexander Solovyov <piranha@piranha.org.ua>
Mon, 01 Jun 2009 20:33:17 +0300
changeset 5 652a1ffd8dec
parent 4 0a8204aeaa4b
child 6 f535f416945a
usage for command should be separate string
fancyopts.py
--- a/fancyopts.py	Mon Jun 01 18:14:01 2009 +0300
+++ b/fancyopts.py	Mon Jun 01 20:33:17 2009 +0300
@@ -4,18 +4,21 @@
 
 import getopt, types, textwrap
 
-def fancyopts(cmd, args, options):
+def fancyopts(cmd, usage, args, options):
     if not args:
-        help_(cmd, options)
+        help_(cmd, usage, options)
     else:
         opts, args = parse(args, options)
         cmd(*args, **opts)
 
-def help_(cmd, options):
+def help_(cmd, usage, options):
     '''show help for given command
 
     >>> def test(*args, **opts):
-    ...     """test [-l HOST] [NAME]"""
+    ...     """that's a test command
+    ...
+    ...        you can do nothing with this command"""
+    ...     pass
     >>> opts = [('l', 'listen', 'localhost',
     ...          'ip to listen on'),
     ...         ('p', 'port', 8000,
@@ -24,9 +27,13 @@
     ...          'daemonize process'),
     ...         ('', 'pid-file', '',
     ...          'name of file to write process ID to')]
-    >>> help_(test, opts)
+    >>> help_(test, 'test [-l HOST] [NAME]', opts)
     test [-l HOST] [NAME]
     <BLANKLINE>
+    that's a test command
+    <BLANKLINE>
+           you can do nothing with this command
+    <BLANKLINE>
     options:
     <BLANKLINE>
      -l --listen     ip to listen on (default: localhost)
@@ -41,7 +48,7 @@
     doc = cmd.__doc__
     if not doc:
         doc = '(no help text available)'
-    print '%s\n' % doc.strip()
+    print '%s\n\n%s\n' % (usage, doc.strip())
     print '\n'.join(help_options(options))