# HG changeset patch # User Alexander Solovyov <piranha@piranha.org.ua> # Date 1245702667 -10800 # Node ID c1857739143cab0e3ea7d13053b3d806501ede85 # Parent f1590d2363ff959adb5a5713e52bfff008ddee86 some upgrade of command handling diff -r f1590d2363ff -r c1857739143c fancycmd.py --- a/fancycmd.py Sat Jun 20 17:00:54 2009 +0300 +++ b/fancycmd.py Mon Jun 22 23:31:07 2009 +0300 @@ -1,34 +1,39 @@ import sys, traceback, getopt +from itertools import imap -from fancyopts import parse +from fancyopts import parse, cmd_help -def help_(ui, header, cmdtable, globalopts, name=None): +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))) + for cmd, info in cmdtable.items(): - # TODO: Handle situation when there is no - # commands starting with '^'! - if name == 'shortlist' and not cmd.startswith('^'): + 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() - ui.status(header) - hlp = sorted(hlp) - maxlen = max(map(len, hlp)) - for cmd, doc in hlp.items(): + 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 cmdtable: - ui.warn('No commands specified!\n') - return + return ui.warn('No commands specified!\n') if not name or name == 'shortlist': - helplist() + return helplist() + + cmd, options, usage = cmdtable[name] + return cmd_help(cmd, name + ': ' + usage, options) def dispatch(args, cmdtable, globalopts=None): @@ -55,24 +60,22 @@ ui = UI() if not globalopts: - globalopts = [('h', 'help', False, 'display help')] + globalopts = [ + ('h', 'help', False, 'display help'), + ('', 'traceback', False, 'display full traceback on error')] try: return _dispatch(ui, args, cmdtable, globalopts + UI.options) except Abort, e: - ui.warn('abort: %s' % e) + ui.warn('abort: %s\n' % e) except UnknownCommand, e: - ui.warn("unknown command: '%s'" % e) + ui.warn("unknown command: '%s'\n" % e) except AmbiguousCommand, e: ui.warn("command '%s' is ambiguous:\n %s\n" % (e.args[0], ' '.join(e.args[1]))) except ParseError, e: - if e.args[0]: - ui.warn('%s: %s' % (e.args[0], e.args[1])) - # display help here? - else: - ui.warn("%s\n" % e.args[1]) - help_(ui, 'Preved', cmdtable, globalopts, 'shortlist') + ui.warn('%s: %s\n' % (e.args[0], e.args[1])) + help_(ui, cmdtable, globalopts, e.args[0]) except KeyboardInterrupt: ui.warn('interrupted!\n') except: @@ -91,8 +94,7 @@ if globaloptions['help']: pass # help elif not cmd: - import ipdb; ipdb.set_trace() - return help_(ui, '', cmdtable, globalopts, 'shortlist') + return help_(ui, cmdtable, globalopts, 'shortlist') try: return func(ui, *args, **options)