help improvements, tests added
authorAlexander Solovyov <piranha@piranha.org.ua>
Fri, 26 Jun 2009 09:49:03 +0300
changeset 14 4d1b80fd0e28
parent 13 c1857739143c
child 15 9477d21dc845
help improvements, tests added
fancycmd.py
fancyopts.py
test.py
--- a/fancycmd.py	Mon Jun 22 23:31:07 2009 +0300
+++ b/fancycmd.py	Fri Jun 26 09:49:03 2009 +0300
@@ -3,38 +3,46 @@
 
 from fancyopts import parse, cmd_help
 
-def help_(ui, cmdtable, globalopts, name=None):
-    def helplist():
-        hlp = {}
-        # determine if there any command marked for shortlist
-        shortlist = (name == 'shortlist' and
-                     any(imap(lambda x: x.startswith('^'), cmdtable)))
+def help_(cmdtable, globalopts):
+    def inner(ui, name=None):
+        '''Show help for a given help topic or a help overview
+
+        With no arguments, print a list of commands with short help messages.
 
-        for cmd, info in cmdtable.items():
-            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)'
-            hlp[cmd] = doc.splitlines()[0].rstrip()
+        Given a command name, print help for that command.
+        '''
+        def helplist():
+            hlp = {}
+            # determine if there any command marked for shortlist
+            shortlist = (name == 'shortlist' and
+                         any(imap(lambda x: x.startswith('^'), cmdtable)))
 
-        hlplist = sorted(hlp)
-        maxlen = max(map(len, hlplist))
-        for cmd in hlplist:
-            doc = hlp[cmd]
-            if ui.verbose:
-                ui.write(' %s:\n     %s\n' % (cmd.replace('|', ', '), doc))
-            else:
-                ui.write(' %-*s  %s\n' % (maxlen, cmd.split('|', 1)[0], doc))
+            for cmd, info in cmdtable.items():
+                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)'
+                hlp[cmd] = doc.splitlines()[0].rstrip()
 
-    if not cmdtable:
-        return ui.warn('No commands specified!\n')
+            hlplist = sorted(hlp)
+            maxlen = max(map(len, hlplist))
+            for cmd in hlplist:
+                doc = hlp[cmd]
+                if ui.verbose:
+                    ui.write(' %s:\n     %s\n' % (cmd.replace('|', ', '), doc))
+                else:
+                    ui.write(' %-*s  %s\n' % (maxlen, cmd.split('|', 1)[0],
+                                              doc))
 
-    if not name or name == 'shortlist':
-        return helplist()
+        if not cmdtable:
+            return ui.warn('No commands specified!\n')
 
-    cmd, options, usage = cmdtable[name]
-    return cmd_help(cmd, name + ': ' + usage, options)
+        if not name or name == 'shortlist':
+            return helplist()
 
+        aliases, (cmd, options, usage) = findcmd(name, cmdtable)
+        return cmd_help(cmd, aliases[0]  + ' ' + usage, options)
+    return inner
 
 def dispatch(args, cmdtable, globalopts=None):
     '''Dispatch command arguments based on subcommands.
@@ -64,6 +72,8 @@
             ('h', 'help', False, 'display help'),
             ('', 'traceback', False, 'display full traceback on error')]
 
+    cmdtable['help'] = (help_(cmdtable, globalopts), [], '[TOPIC]')
+
     try:
         return _dispatch(ui, args, cmdtable, globalopts + UI.options)
     except Abort, e:
@@ -75,7 +85,7 @@
                 (e.args[0], ' '.join(e.args[1])))
     except ParseError, e:
         ui.warn('%s: %s\n' % (e.args[0], e.args[1]))
-        help_(ui, cmdtable, globalopts, e.args[0])
+        cmdtable['help'][0](ui, e.args[0])
     except KeyboardInterrupt:
         ui.warn('interrupted!\n')
     except:
@@ -94,7 +104,7 @@
     if globaloptions['help']:
         pass # help
     elif not cmd:
-        return help_(ui, cmdtable, globalopts, 'shortlist')
+        return cmdtable['help'][0](ui, 'shortlist')
 
     try:
         return func(ui, *args, **options)
--- a/fancyopts.py	Mon Jun 22 23:31:07 2009 +0300
+++ b/fancyopts.py	Fri Jun 26 09:49:03 2009 +0300
@@ -86,7 +86,8 @@
     if not doc:
         doc = '(no help text available)'
     print '%s\n' % doc.strip()
-    print ''.join(help_options(options))
+    if options:
+        print ''.join(help_options(options))
 
 def help_options(options):
     yield 'options:\n\n'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.py	Fri Jun 26 09:49:03 2009 +0300
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+import sys
+
+from fancycmd import dispatch
+
+
+def simple(ui, *args, **opts):
+    '''Just simple command to do nothing.
+
+    I assure you! Nothing to look here. ;-)
+    '''
+    print opts
+
+def complex_(ui, *args, **opts):
+    '''That's more complex command indented to do something
+
+    Let's try to do that (damn, but what?!)
+    '''
+    print args, opts
+
+cmdtable = {
+    'simple':
+        (simple,
+         [('t', 'test', False, 'just test execution')],
+         '[-t] ...'),
+    'complex':
+        (complex_,
+         [],
+         '')}
+
+if __name__ == '__main__':
+    dispatch(sys.argv[1:], cmdtable)