diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-04-02 12:42:09 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-04-02 12:42:09 -0400 |
commit | 91a861dc35367a48d8a4cc33ac133f49bc185538 (patch) | |
tree | f74e37bbee7c1694857988449a05b7aff1fa1488 | |
parent | 760785317ec1ffb7a5b1e0f01723f5faf94e3c7a (diff) | |
download | cmd2-git-91a861dc35367a48d8a4cc33ac133f49bc185538.tar.gz |
Added ability to query individual alias
-rwxr-xr-x | cmd2.py | 24 | ||||
-rw-r--r-- | tests/test_cmd2.py | 10 |
2 files changed, 27 insertions, 7 deletions
@@ -2745,15 +2745,20 @@ class Cmd(cmd.Cmd): def do_alias(self, arglist): """Define or display aliases -Usage: Usage: alias [<name> <value>] +Usage: Usage: alias [name] | [<name> <value>] Where: - name - name of the alias being added or edited - value - what the alias will be resolved to + name - name of the alias being looked up, added, or edited + value - what the alias will be resolved to (if adding or editing) this can contain spaces and does not need to be quoted Without arguments, 'alias' prints a list of all aliases in a reusable form which can be outputted to a startup_script to preserve aliases across sessions. + With one argument, 'alias' shows the value of the specified alias. + Example: alias ls (Prints the value of the alias called 'ls' if it exists) + + With two or more arguments, 'alias' creates or replaces an alias. + Example: alias ls !ls -lF If you want to use redirection or pipes in the alias, then either quote the tokens with these @@ -2769,8 +2774,16 @@ Usage: Usage: alias [<name> <value>] for cur_alias in self.aliases: self.poutput("alias {} {}".format(cur_alias, self.aliases[cur_alias])) + # The user is looking up an alias + elif len(arglist) == 1: + name = arglist[0] + if name in self.aliases: + self.poutput("alias {} {}".format(name, self.aliases[name])) + else: + self.perror("Alias '{}' not found".format(name), traceback_war=False) + # The user is creating an alias - elif len(arglist) >= 2: + else: name = arglist[0] value = ' '.join(arglist[1:]) @@ -2785,9 +2798,6 @@ Usage: Usage: alias [<name> <value>] self.aliases[name] = value self.poutput("Alias created") - else: - self.do_help('alias') - def complete_alias(self, text, line, begidx, endidx): """ Tab completion for alias """ index_dict = \ diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 9b64a5e3..4003c387 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1560,6 +1560,16 @@ def test_alias(base_app, capsys): out = run_cmd(base_app, 'alias') assert out == normalize('alias fake pyscript') + # Lookup the new alias + out = run_cmd(base_app, 'alias fake') + assert out == normalize('alias fake pyscript') + +def test_alias_lookup_invalid_alias(base_app, capsys): + # Lookup invalid alias + out = run_cmd(base_app, 'alias invalid') + out, err = capsys.readouterr() + assert "not found" in err + def test_alias_with_invalid_name(base_app, capsys): run_cmd(base_app, 'alias @ help') out, err = capsys.readouterr() |