comparison mercurial/dispatch.py @ 28621:d856e85a8a7a

dispatch: make cmdalias forward command attributes to function This delays resolution of command attributes so that missing attributes can be warned only when necessary.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 14 Mar 2016 00:06:52 +0900
parents 009f58f1ea75
children 527cf881d000
comparison
equal deleted inserted replaced
28620:759d167f75cf 28621:d856e85a8a7a
435 self.definition = definition 435 self.definition = definition
436 self.fn = None 436 self.fn = None
437 self.args = [] 437 self.args = []
438 self.opts = [] 438 self.opts = []
439 self.help = '' 439 self.help = ''
440 self.norepo = True
441 self.optionalrepo = False
442 self.inferrepo = False
443 self.badalias = None 440 self.badalias = None
444 self.unknowncmd = False 441 self.unknowncmd = False
445 442
446 try: 443 try:
447 aliases, entry = cmdutil.findcmd(self.name, cmdtable) 444 aliases, entry = cmdutil.findcmd(self.name, cmdtable)
499 self.fn, self.opts, self.help = tableentry 496 self.fn, self.opts, self.help = tableentry
500 else: 497 else:
501 self.fn, self.opts = tableentry 498 self.fn, self.opts = tableentry
502 499
503 self.args = aliasargs(self.fn, args) 500 self.args = aliasargs(self.fn, args)
504 if not self.fn.norepo:
505 self.norepo = False
506 if self.fn.optionalrepo:
507 self.optionalrepo = True
508 if self.fn.inferrepo:
509 self.inferrepo = True
510 if self.help.startswith("hg " + cmd): 501 if self.help.startswith("hg " + cmd):
511 # drop prefix in old-style help lines so hg shows the alias 502 # drop prefix in old-style help lines so hg shows the alias
512 self.help = self.help[4 + len(cmd):] 503 self.help = self.help[4 + len(cmd):]
513 self.__doc__ = self.fn.__doc__ 504 self.__doc__ = self.fn.__doc__
514 505
517 % (self.name, cmd)) 508 % (self.name, cmd))
518 self.unknowncmd = True 509 self.unknowncmd = True
519 except error.AmbiguousCommand: 510 except error.AmbiguousCommand:
520 self.badalias = (_("alias '%s' resolves to ambiguous command '%s'") 511 self.badalias = (_("alias '%s' resolves to ambiguous command '%s'")
521 % (self.name, cmd)) 512 % (self.name, cmd))
513
514 def __getattr__(self, name):
515 adefaults = {'norepo': True, 'optionalrepo': False, 'inferrepo': False}
516 if name not in adefaults:
517 raise AttributeError(name)
518 if self.badalias or util.safehasattr(self, 'shell'):
519 return adefaults[name]
520 return getattr(self.fn, name)
522 521
523 def __call__(self, ui, *args, **opts): 522 def __call__(self, ui, *args, **opts):
524 if self.badalias: 523 if self.badalias:
525 hint = None 524 hint = None
526 if self.unknowncmd: 525 if self.unknowncmd: