##// END OF EJS Templates
AliasCaller knows its own name
Thomas Kluyver -
Show More
@@ -27,6 +27,7 b' import re'
27 27 import sys
28 28
29 29 from IPython.config.configurable import Configurable
30 from IPython.core.error import UsageError
30 31 from IPython.core.splitinput import split_user_input
31 32
32 33 from IPython.utils.traitlets import List, Instance
@@ -105,13 +106,17 b' class InvalidAliasError(AliasError):'
105 106 pass
106 107
107 108 class AliasCaller(object):
108 def __init__(self, shell, cmd):
109 def __init__(self, shell, name, cmd):
109 110 self.shell = shell
111 self.name = name
110 112 self.cmd = cmd
111 113 self.nargs = cmd.count('%s')
112 114 if (self.nargs > 0) and (cmd.find('%l') >= 0):
113 115 raise InvalidAliasError('The %s and %l specifiers are mutually '
114 116 'exclusive in alias definitions.')
117
118 def __repr__(self):
119 return "<alias {} for {!r}>".format(self.name, self.cmd)
115 120
116 121 def __call__(self, rest=''):
117 122 cmd = self.cmd
@@ -127,8 +132,8 b' class AliasCaller(object):'
127 132 # Handle aliases with positional arguments
128 133 args = rest.split(None, nargs)
129 134 if len(args) < nargs:
130 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
131 (alias, nargs, len(args)))
135 raise UsageError('Alias <%s> requires %s arguments, %s given.' %
136 (self.name, nargs, len(args)))
132 137 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
133 138
134 139 self.shell.system(cmd)
@@ -189,7 +194,7 b' class AliasManager(Configurable):'
189 194 problems.
190 195 """
191 196 self.validate_alias(name, cmd)
192 caller = AliasCaller(shell=self.shell, cmd=cmd)
197 caller = AliasCaller(shell=self.shell, name=name, cmd=cmd)
193 198 self.shell.magics_manager.register_function(caller, magic_kind='line',
194 199 magic_name=name)
195 200
General Comments 0
You need to be logged in to leave comments. Login now