Mercurial > public > mercurial-scm > hg-stable
diff mercurial/dispatch.py @ 37137:6890b7e991a4
help: supporting both help and doc for aliases
This allows an alias to be definted like:
[alias]
lj = log -Tjson
lj:help = [-r REV]
lj:doc = Shows the revision log in JSON format.
Differential Revision: https://phab.mercurial-scm.org/D2678
author | Rodrigo Damazio <rdamazio@google.com> |
---|---|
date | Sun, 04 Mar 2018 17:11:33 -0500 |
parents | a8a902d7176e |
children | aa55c5354b8f |
line wrap: on
line diff
--- a/mercurial/dispatch.py Tue Mar 27 20:21:30 2018 +0530 +++ b/mercurial/dispatch.py Sun Mar 04 17:11:33 2018 -0500 @@ -450,7 +450,7 @@ return r.sub(lambda x: replacemap[x.group()], cmd) class cmdalias(object): - def __init__(self, name, definition, cmdtable, source): + def __init__(self, ui, name, definition, cmdtable, source): self.name = self.cmd = name self.cmdname = '' self.definition = definition @@ -477,6 +477,7 @@ return if self.definition.startswith('!'): + shdef = self.definition[1:] self.shell = True def fn(ui, *args): env = {'HG_ARGS': ' '.join((self.name,) + args)} @@ -490,11 +491,12 @@ "of %i variable in alias '%s' definition.\n" % (int(m.groups()[0]), self.name)) return '' - cmd = re.sub(br'\$(\d+|\$)', _checkvar, self.definition[1:]) + cmd = re.sub(br'\$(\d+|\$)', _checkvar, shdef) cmd = aliasinterpolate(self.name, args, cmd) return ui.system(cmd, environ=env, blockedtag='alias_%s' % self.name) self.fn = fn + self._populatehelp(ui, name, shdef, self.fn) return try: @@ -516,14 +518,12 @@ try: tableentry = cmdutil.findcmd(cmd, cmdtable, False)[1] if len(tableentry) > 2: - self.fn, self.opts, self.help = tableentry + self.fn, self.opts, cmdhelp = tableentry else: self.fn, self.opts = tableentry + cmdhelp = None - if self.help.startswith("hg " + cmd): - # drop prefix in old-style help lines so hg shows the alias - self.help = self.help[4 + len(cmd):] - self.__doc__ = self.fn.__doc__ + self._populatehelp(ui, name, cmd, self.fn, cmdhelp) except error.UnknownCommand: self.badalias = (_("alias '%s' resolves to unknown command '%s'") @@ -533,6 +533,14 @@ self.badalias = (_("alias '%s' resolves to ambiguous command '%s'") % (self.name, cmd)) + def _populatehelp(self, ui, name, cmd, fn, defaulthelp=None): + self.help = ui.config('alias', '%s:help' % name, defaulthelp or '') + if self.help and self.help.startswith("hg " + cmd): + # drop prefix in old-style help lines so hg shows the alias + self.help = self.help[4 + len(cmd):] + + self.__doc__ = ui.config('alias', '%s:doc' % name, fn.__doc__) + @property def args(self): args = pycompat.maplist(util.expandpath, self.givenargs) @@ -577,7 +585,8 @@ class lazyaliasentry(object): """like a typical command entry (func, opts, help), but is lazy""" - def __init__(self, name, definition, cmdtable, source): + def __init__(self, ui, name, definition, cmdtable, source): + self.ui = ui self.name = name self.definition = definition self.cmdtable = cmdtable.copy() @@ -585,7 +594,8 @@ @util.propertycache def _aliasdef(self): - return cmdalias(self.name, self.definition, self.cmdtable, self.source) + return cmdalias(self.ui, self.name, self.definition, self.cmdtable, + self.source) def __getitem__(self, n): aliasdef = self._aliasdef @@ -609,7 +619,7 @@ # aliases are processed after extensions have been loaded, so they # may use extension commands. Aliases can also use other alias definitions, # but only if they have been defined prior to the current definition. - for alias, definition in ui.configitems('alias'): + for alias, definition in ui.configitems('alias', ignoresub=True): try: if cmdtable[alias].definition == definition: continue @@ -618,7 +628,7 @@ pass source = ui.configsource('alias', alias) - entry = lazyaliasentry(alias, definition, cmdtable, source) + entry = lazyaliasentry(ui, alias, definition, cmdtable, source) cmdtable[alias] = entry def _parse(ui, args):