mercurial/commands.py
changeset 2553 5b426676f616
parent 2549 e1831f06eef1
child 2567 2748253b49c2
equal deleted inserted replaced
2552:bb403d427974 2553:5b426676f616
   532         "This is free software; see the source for copying conditions. "
   532         "This is free software; see the source for copying conditions. "
   533         "There is NO\nwarranty; "
   533         "There is NO\nwarranty; "
   534         "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
   534         "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
   535     ))
   535     ))
   536 
   536 
   537 def help_(ui, cmd=None, with_version=False):
   537 def help_(ui, name=None, with_version=False):
   538     """show help for a given command or all commands"""
   538     """show help for a command, extension, or list of commands
       
   539 
       
   540     With no arguments, print a list of commands and short help.
       
   541     
       
   542     Given a command name, print help for that command.
       
   543 
       
   544     Given an extension name, print help for that extension, and the
       
   545     commands it provides."""
   539     option_lists = []
   546     option_lists = []
   540     if cmd and cmd != 'shortlist':
   547 
       
   548     def helpcmd(name):
   541         if with_version:
   549         if with_version:
   542             show_version(ui)
   550             show_version(ui)
   543             ui.write('\n')
   551             ui.write('\n')
   544         aliases, i = find(cmd)
   552         aliases, i = findcmd(name)
   545         # synopsis
   553         # synopsis
   546         ui.write("%s\n\n" % i[2])
   554         ui.write("%s\n\n" % i[2])
   547 
   555 
   548         # description
   556         # description
   549         doc = i[0].__doc__
   557         doc = i[0].__doc__
   559                 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:]))
   567                 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:]))
   560 
   568 
   561             # options
   569             # options
   562             if i[1]:
   570             if i[1]:
   563                 option_lists.append(("options", i[1]))
   571                 option_lists.append(("options", i[1]))
   564 
   572         
   565     else:
   573     def helplist(select=None):
   566         # program name
       
   567         if ui.verbose or with_version:
       
   568             show_version(ui)
       
   569         else:
       
   570             ui.status(_("Mercurial Distributed SCM\n"))
       
   571         ui.status('\n')
       
   572 
       
   573         # list of commands
       
   574         if cmd == "shortlist":
       
   575             ui.status(_('basic commands (use "hg help" '
       
   576                         'for the full list or option "-v" for details):\n\n'))
       
   577         elif ui.verbose:
       
   578             ui.status(_('list of commands:\n\n'))
       
   579         else:
       
   580             ui.status(_('list of commands (use "hg help -v" '
       
   581                         'to show aliases and global options):\n\n'))
       
   582 
       
   583         h = {}
   574         h = {}
   584         cmds = {}
   575         cmds = {}
   585         for c, e in table.items():
   576         for c, e in table.items():
   586             f = c.split("|")[0]
   577             f = c.split("|", 1)[0]
   587             if cmd == "shortlist" and not f.startswith("^"):
   578             if select and not select(f):
       
   579                 continue
       
   580             if name == "shortlist" and not f.startswith("^"):
   588                 continue
   581                 continue
   589             f = f.lstrip("^")
   582             f = f.lstrip("^")
   590             if not ui.debugflag and f.startswith("debug"):
   583             if not ui.debugflag and f.startswith("debug"):
   591                 continue
   584                 continue
   592             doc = e[0].__doc__
   585             doc = e[0].__doc__
   602             if ui.verbose:
   595             if ui.verbose:
   603                 commands = cmds[f].replace("|",", ")
   596                 commands = cmds[f].replace("|",", ")
   604                 ui.write(" %s:\n      %s\n"%(commands, h[f]))
   597                 ui.write(" %s:\n      %s\n"%(commands, h[f]))
   605             else:
   598             else:
   606                 ui.write(' %-*s   %s\n' % (m, f, h[f]))
   599                 ui.write(' %-*s   %s\n' % (m, f, h[f]))
       
   600 
       
   601     def helpext(name):
       
   602         try:
       
   603             mod = findext(name)
       
   604         except KeyError:
       
   605             raise UnknownCommand(name)
       
   606 
       
   607         doc = (mod.__doc__ or _('No help text available')).splitlines(0)
       
   608         ui.write(_('%s extension - %s\n') % (name.split('.')[-1], doc[0]))
       
   609         for d in doc[1:]:
       
   610             ui.write(d, '\n')
       
   611 
       
   612         ui.status('\n')
       
   613         if ui.verbose:
       
   614             ui.status(_('list of commands:\n\n'))
       
   615         else:
       
   616             ui.status(_('list of commands (use "hg help -v %s" '
       
   617                         'to show aliases and global options):\n\n') % name)
       
   618 
       
   619         modcmds = dict.fromkeys([c.split('|', 1)[0] for c in mod.cmdtable])
       
   620         helplist(modcmds.has_key)
       
   621 
       
   622     if name and name != 'shortlist':
       
   623         try:
       
   624             helpcmd(name)
       
   625         except UnknownCommand:
       
   626             helpext(name)
       
   627 
       
   628     else:
       
   629         # program name
       
   630         if ui.verbose or with_version:
       
   631             show_version(ui)
       
   632         else:
       
   633             ui.status(_("Mercurial Distributed SCM\n"))
       
   634         ui.status('\n')
       
   635 
       
   636         # list of commands
       
   637         if name == "shortlist":
       
   638             ui.status(_('basic commands (use "hg help" '
       
   639                         'for the full list or option "-v" for details):\n\n'))
       
   640         elif ui.verbose:
       
   641             ui.status(_('list of commands:\n\n'))
       
   642         else:
       
   643             ui.status(_('list of commands (use "hg help -v" '
       
   644                         'to show aliases and global options):\n\n'))
       
   645 
       
   646         helplist()
   607 
   647 
   608     # global options
   648     # global options
   609     if ui.verbose:
   649     if ui.verbose:
   610         option_lists.append(("global options", globalopts))
   650         option_lists.append(("global options", globalopts))
   611 
   651 
  1252 
  1292 
  1253     if opts['options']:
  1293     if opts['options']:
  1254         options = []
  1294         options = []
  1255         otables = [globalopts]
  1295         otables = [globalopts]
  1256         if cmd:
  1296         if cmd:
  1257             aliases, entry = find(cmd)
  1297             aliases, entry = findcmd(cmd)
  1258             otables.append(entry[1])
  1298             otables.append(entry[1])
  1259         for t in otables:
  1299         for t in otables:
  1260             for o in t:
  1300             for o in t:
  1261                 if o[0]:
  1301                 if o[0]:
  1262                     options.append('-%s' % o[0])
  1302                     options.append('-%s' % o[0])
  3272     if not choice and debugchoice:
  3312     if not choice and debugchoice:
  3273         choice = debugchoice
  3313         choice = debugchoice
  3274 
  3314 
  3275     return choice
  3315     return choice
  3276 
  3316 
  3277 def find(cmd):
  3317 def findcmd(cmd):
  3278     """Return (aliases, command table entry) for command string."""
  3318     """Return (aliases, command table entry) for command string."""
  3279     choice = findpossible(cmd)
  3319     choice = findpossible(cmd)
  3280 
  3320 
  3281     if choice.has_key(cmd):
  3321     if choice.has_key(cmd):
  3282         return choice[cmd]
  3322         return choice[cmd]
  3309     except fancyopts.getopt.GetoptError, inst:
  3349     except fancyopts.getopt.GetoptError, inst:
  3310         raise ParseError(None, inst)
  3350         raise ParseError(None, inst)
  3311 
  3351 
  3312     if args:
  3352     if args:
  3313         cmd, args = args[0], args[1:]
  3353         cmd, args = args[0], args[1:]
  3314         aliases, i = find(cmd)
  3354         aliases, i = findcmd(cmd)
  3315         cmd = aliases[0]
  3355         cmd = aliases[0]
  3316         defaults = ui.config("defaults", cmd)
  3356         defaults = ui.config("defaults", cmd)
  3317         if defaults:
  3357         if defaults:
  3318             args = defaults.split() + args
  3358             args = defaults.split() + args
  3319         c = list(i[1])
  3359         c = list(i[1])
  3336         options[n] = cmdoptions[n]
  3376         options[n] = cmdoptions[n]
  3337         del cmdoptions[n]
  3377         del cmdoptions[n]
  3338 
  3378 
  3339     return (cmd, cmd and i[0] or None, args, options, cmdoptions)
  3379     return (cmd, cmd and i[0] or None, args, options, cmdoptions)
  3340 
  3380 
       
  3381 external = {}
       
  3382 
       
  3383 def findext(name):
       
  3384     '''return module with given extension name'''
       
  3385     try:
       
  3386         return external[name]
       
  3387     except KeyError:
       
  3388         dotname = '.' + name
       
  3389         for k, v in external.iteritems():
       
  3390             if k.endswith('.' + name) or v.__name__ == name:
       
  3391                 return v
       
  3392         raise KeyError(name)
       
  3393     
  3341 def dispatch(args):
  3394 def dispatch(args):
  3342     for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
  3395     for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
  3343         num = getattr(signal, name, None)
  3396         num = getattr(signal, name, None)
  3344         if num: signal.signal(num, catchterm)
  3397         if num: signal.signal(num, catchterm)
  3345 
  3398 
  3347         u = ui.ui(traceback='--traceback' in sys.argv[1:])
  3400         u = ui.ui(traceback='--traceback' in sys.argv[1:])
  3348     except util.Abort, inst:
  3401     except util.Abort, inst:
  3349         sys.stderr.write(_("abort: %s\n") % inst)
  3402         sys.stderr.write(_("abort: %s\n") % inst)
  3350         return -1
  3403         return -1
  3351 
  3404 
  3352     external = []
       
  3353     for x in u.extensions():
  3405     for x in u.extensions():
  3354         try:
  3406         try:
  3355             if x[1]:
  3407             if x[1]:
  3356                 # the module will be loaded in sys.modules
  3408                 # the module will be loaded in sys.modules
  3357                 # choose an unique name so that it doesn't
  3409                 # choose an unique name so that it doesn't
  3364                     components = name.split('.')
  3416                     components = name.split('.')
  3365                     for comp in components[1:]:
  3417                     for comp in components[1:]:
  3366                         mod = getattr(mod, comp)
  3418                         mod = getattr(mod, comp)
  3367                     return mod
  3419                     return mod
  3368                 try:
  3420                 try:
  3369                     mod = importh("hgext." + x[0])
  3421                     name = 'hgext.' + x[0]
       
  3422                     mod = importh(name)
  3370                 except ImportError:
  3423                 except ImportError:
  3371                     mod = importh(x[0])
  3424                     name = x[0]
  3372             external.append(mod)
  3425                     mod = importh(name)
       
  3426             external[name] = mod
  3373         except (util.SignalInterrupt, KeyboardInterrupt):
  3427         except (util.SignalInterrupt, KeyboardInterrupt):
  3374             raise
  3428             raise
  3375         except Exception, inst:
  3429         except Exception, inst:
  3376             u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst))
  3430             u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst))
  3377             if u.print_exc():
  3431             if u.print_exc():
  3378                 return 1
  3432                 return 1
  3379 
  3433 
  3380     for x in external:
  3434     for x in external.itervalues():
  3381         uisetup = getattr(x, 'uisetup', None)
  3435         uisetup = getattr(x, 'uisetup', None)
  3382         if uisetup:
  3436         if uisetup:
  3383             uisetup(u)
  3437             uisetup(u)
  3384         cmdtable = getattr(x, 'cmdtable', {})
  3438         cmdtable = getattr(x, 'cmdtable', {})
  3385         for t in cmdtable:
  3439         for t in cmdtable:
  3431             if cmd not in norepo.split():
  3485             if cmd not in norepo.split():
  3432                 try:
  3486                 try:
  3433                     if not repo:
  3487                     if not repo:
  3434                         repo = hg.repository(u, path=path)
  3488                         repo = hg.repository(u, path=path)
  3435                     u = repo.ui
  3489                     u = repo.ui
  3436                     for x in external:
  3490                     for x in external.itervalues():
  3437                         if hasattr(x, 'reposetup'):
  3491                         if hasattr(x, 'reposetup'):
  3438                             x.reposetup(u, repo)
  3492                             x.reposetup(u, repo)
  3439                 except hg.RepoError:
  3493                 except hg.RepoError:
  3440                     if cmd not in optionalrepo.split():
  3494                     if cmd not in optionalrepo.split():
  3441                         raise
  3495                         raise