Skip to content

Commit 91a861d

Browse files
committed
Added ability to query individual alias
1 parent 7607853 commit 91a861d

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

cmd2.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,15 +2745,20 @@ def _cmdloop(self):
27452745
def do_alias(self, arglist):
27462746
"""Define or display aliases
27472747
2748-
Usage: Usage: alias [<name> <value>]
2748+
Usage: Usage: alias [name] | [<name> <value>]
27492749
Where:
2750-
name - name of the alias being added or edited
2751-
value - what the alias will be resolved to
2750+
name - name of the alias being looked up, added, or edited
2751+
value - what the alias will be resolved to (if adding or editing)
27522752
this can contain spaces and does not need to be quoted
27532753
27542754
Without arguments, 'alias' prints a list of all aliases in a reusable form which
27552755
can be outputted to a startup_script to preserve aliases across sessions.
27562756
2757+
With one argument, 'alias' shows the value of the specified alias.
2758+
Example: alias ls (Prints the value of the alias called 'ls' if it exists)
2759+
2760+
With two or more arguments, 'alias' creates or replaces an alias.
2761+
27572762
Example: alias ls !ls -lF
27582763
27592764
If you want to use redirection or pipes in the alias, then either quote the tokens with these
@@ -2769,8 +2774,16 @@ def do_alias(self, arglist):
27692774
for cur_alias in self.aliases:
27702775
self.poutput("alias {} {}".format(cur_alias, self.aliases[cur_alias]))
27712776

2777+
# The user is looking up an alias
2778+
elif len(arglist) == 1:
2779+
name = arglist[0]
2780+
if name in self.aliases:
2781+
self.poutput("alias {} {}".format(name, self.aliases[name]))
2782+
else:
2783+
self.perror("Alias '{}' not found".format(name), traceback_war=False)
2784+
27722785
# The user is creating an alias
2773-
elif len(arglist) >= 2:
2786+
else:
27742787
name = arglist[0]
27752788
value = ' '.join(arglist[1:])
27762789

@@ -2785,9 +2798,6 @@ def do_alias(self, arglist):
27852798
self.aliases[name] = value
27862799
self.poutput("Alias created")
27872800

2788-
else:
2789-
self.do_help('alias')
2790-
27912801
def complete_alias(self, text, line, begidx, endidx):
27922802
""" Tab completion for alias """
27932803
index_dict = \

tests/test_cmd2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,16 @@ def test_alias(base_app, capsys):
15601560
out = run_cmd(base_app, 'alias')
15611561
assert out == normalize('alias fake pyscript')
15621562

1563+
# Lookup the new alias
1564+
out = run_cmd(base_app, 'alias fake')
1565+
assert out == normalize('alias fake pyscript')
1566+
1567+
def test_alias_lookup_invalid_alias(base_app, capsys):
1568+
# Lookup invalid alias
1569+
out = run_cmd(base_app, 'alias invalid')
1570+
out, err = capsys.readouterr()
1571+
assert "not found" in err
1572+
15631573
def test_alias_with_invalid_name(base_app, capsys):
15641574
run_cmd(base_app, 'alias @ help')
15651575
out, err = capsys.readouterr()

0 commit comments

Comments
 (0)