From e99285dad75565dd754585910661d3c2fc2ad39f 2015-03-26 18:20:51 From: Thomas Kluyver Date: 2015-03-26 18:20:51 Subject: [PATCH] Merge pull request #8151 from brotherjack/master Included a way to 'comment out' an %s for a non-arg --- diff --git a/IPython/core/alias.py b/IPython/core/alias.py index e1a6242..aa55137 100644 --- a/IPython/core/alias.py +++ b/IPython/core/alias.py @@ -151,8 +151,8 @@ class Alias(object): raise InvalidAliasError("An alias command must be a string, " "got: %r" % self.cmd) - nargs = self.cmd.count('%s') - + nargs = self.cmd.count('%s') - self.cmd.count('%%s') + if (nargs > 0) and (self.cmd.find('%l') >= 0): raise InvalidAliasError('The %s and %l specifiers are mutually ' 'exclusive in alias definitions.') @@ -169,7 +169,10 @@ class Alias(object): if cmd.find('%l') >= 0: cmd = cmd.replace('%l', rest) rest = '' + if nargs==0: + if cmd.find('%%s') >= 1: + cmd = cmd.replace('%%s', '%s') # Simple, argument-less aliases cmd = '%s %s' % (cmd, rest) else: diff --git a/IPython/core/tests/test_alias.py b/IPython/core/tests/test_alias.py index 8ae57f6..7417e95 100644 --- a/IPython/core/tests/test_alias.py +++ b/IPython/core/tests/test_alias.py @@ -38,4 +38,25 @@ def test_alias_args_error(): with capture_output() as cap: _ip.run_cell('parts 1') - nt.assert_equal(cap.stderr.split(':')[0], 'UsageError') \ No newline at end of file + nt.assert_equal(cap.stderr.split(':')[0], 'UsageError') + +def test_alias_args_commented(): + """Check that alias correctly ignores 'commented out' args""" + _ip.magic('alias commetarg echo this is %%s a commented out arg') + + with capture_output() as cap: + _ip.run_cell('commetarg') + + nt.assert_equal(cap.stdout, 'this is %s a commented out arg') + +def test_alias_args_commented_nargs(): + """Check that alias correctly counts args, excluding those commented out""" + am = _ip.alias_manager + alias_name = 'comargcount' + cmd = 'echo this is %%s a commented out arg and this is not %s' + + am.define_alias(alias_name, cmd) + assert am.is_alias(alias_name) + + thealias = am.get_alias(alias_name) + nt.assert_equal(thealias.nargs, 1) \ No newline at end of file