Mercurial > hg
comparison mercurial/dispatch.py @ 22160:645457f73aa6
alias: keep error message in "badalias" so that help can see it
Upcoming patches will
- change help_() to get badalias message without executing cmdalias()
- raise Abort on bad alias
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 17 May 2014 21:13:31 +0900 |
parents | bc2132dfc0a4 |
children | 063628423fd1 |
comparison
equal
deleted
inserted
replaced
22159:db7921812f56 | 22160:645457f73aa6 |
---|---|
355 class cmdalias(object): | 355 class cmdalias(object): |
356 def __init__(self, name, definition, cmdtable): | 356 def __init__(self, name, definition, cmdtable): |
357 self.name = self.cmd = name | 357 self.name = self.cmd = name |
358 self.cmdname = '' | 358 self.cmdname = '' |
359 self.definition = definition | 359 self.definition = definition |
360 self.fn = None | |
360 self.args = [] | 361 self.args = [] |
361 self.opts = [] | 362 self.opts = [] |
362 self.help = '' | 363 self.help = '' |
363 self.norepo = True | 364 self.norepo = True |
364 self.optionalrepo = False | 365 self.optionalrepo = False |
365 self.badalias = False | 366 self.badalias = None |
366 | 367 |
367 try: | 368 try: |
368 aliases, entry = cmdutil.findcmd(self.name, cmdtable) | 369 aliases, entry = cmdutil.findcmd(self.name, cmdtable) |
369 for alias, e in cmdtable.iteritems(): | 370 for alias, e in cmdtable.iteritems(): |
370 if e is entry: | 371 if e is entry: |
373 self.shadows = True | 374 self.shadows = True |
374 except error.UnknownCommand: | 375 except error.UnknownCommand: |
375 self.shadows = False | 376 self.shadows = False |
376 | 377 |
377 if not self.definition: | 378 if not self.definition: |
378 def fn(ui, *args): | 379 self.badalias = _("no definition for alias '%s'") % self.name |
379 ui.warn(_("no definition for alias '%s'\n") % self.name) | |
380 return -1 | |
381 self.fn = fn | |
382 self.badalias = True | |
383 return | 380 return |
384 | 381 |
385 if self.definition.startswith('!'): | 382 if self.definition.startswith('!'): |
386 self.shell = True | 383 self.shell = True |
387 def fn(ui, *args): | 384 def fn(ui, *args): |
403 return | 400 return |
404 | 401 |
405 try: | 402 try: |
406 args = shlex.split(self.definition) | 403 args = shlex.split(self.definition) |
407 except ValueError, inst: | 404 except ValueError, inst: |
408 def fn(ui, *args): | 405 self.badalias = (_("error in definition for alias '%s': %s") |
409 ui.warn(_("error in definition for alias '%s': %s\n") | 406 % (self.name, inst)) |
410 % (self.name, inst)) | |
411 return -1 | |
412 self.fn = fn | |
413 self.badalias = True | |
414 return | 407 return |
415 self.cmdname = cmd = args.pop(0) | 408 self.cmdname = cmd = args.pop(0) |
416 args = map(util.expandpath, args) | 409 args = map(util.expandpath, args) |
417 | 410 |
418 for invalidarg in ("--cwd", "-R", "--repository", "--repo", "--config"): | 411 for invalidarg in ("--cwd", "-R", "--repository", "--repo", "--config"): |
419 if _earlygetopt([invalidarg], args): | 412 if _earlygetopt([invalidarg], args): |
420 def fn(ui, *args): | 413 self.badalias = (_("error in definition for alias '%s': %s may " |
421 ui.warn(_("error in definition for alias '%s': %s may only " | 414 "only be given on the command line") |
422 "be given on the command line\n") | 415 % (self.name, invalidarg)) |
423 % (self.name, invalidarg)) | |
424 return -1 | |
425 | |
426 self.fn = fn | |
427 self.badalias = True | |
428 return | 416 return |
429 | 417 |
430 try: | 418 try: |
431 tableentry = cmdutil.findcmd(cmd, cmdtable, False)[1] | 419 tableentry = cmdutil.findcmd(cmd, cmdtable, False)[1] |
432 if len(tableentry) > 2: | 420 if len(tableentry) > 2: |
444 self.help = self.help[4 + len(cmd):] | 432 self.help = self.help[4 + len(cmd):] |
445 self.__doc__ = self.fn.__doc__ | 433 self.__doc__ = self.fn.__doc__ |
446 | 434 |
447 except error.UnknownCommand: | 435 except error.UnknownCommand: |
448 def fn(ui, *args): | 436 def fn(ui, *args): |
449 ui.warn(_("alias '%s' resolves to unknown command '%s'\n") \ | |
450 % (self.name, cmd)) | |
451 try: | 437 try: |
452 # check if the command is in a disabled extension | 438 # check if the command is in a disabled extension |
453 commands.help_(ui, cmd, unknowncmd=True) | 439 commands.help_(ui, cmd, unknowncmd=True) |
454 except error.UnknownCommand: | 440 except error.UnknownCommand: |
455 pass | 441 pass |
456 return -1 | 442 return -1 |
457 self.fn = fn | 443 self.fn = fn |
458 self.badalias = True | 444 self.badalias = (_("alias '%s' resolves to unknown command '%s'") |
445 % (self.name, cmd)) | |
459 except error.AmbiguousCommand: | 446 except error.AmbiguousCommand: |
460 def fn(ui, *args): | 447 self.badalias = (_("alias '%s' resolves to ambiguous command '%s'") |
461 ui.warn(_("alias '%s' resolves to ambiguous command '%s'\n") \ | 448 % (self.name, cmd)) |
462 % (self.name, cmd)) | |
463 return -1 | |
464 self.fn = fn | |
465 self.badalias = True | |
466 | 449 |
467 def __call__(self, ui, *args, **opts): | 450 def __call__(self, ui, *args, **opts): |
451 if self.badalias: | |
452 ui.warn(self.badalias + '\n') | |
453 if self.fn: | |
454 return self.fn(ui, *args, **opts) | |
455 return -1 | |
468 if self.shadows: | 456 if self.shadows: |
469 ui.debug("alias '%s' shadows command '%s'\n" % | 457 ui.debug("alias '%s' shadows command '%s'\n" % |
470 (self.name, self.cmdname)) | 458 (self.name, self.cmdname)) |
471 | 459 |
472 if util.safehasattr(self, 'shell'): | 460 if util.safehasattr(self, 'shell'): |