Skip to content

Commit a67c694

Browse files
committed
Merge branch 'macro' into argparse_conversion
2 parents 8483725 + b4e2172 commit a67c694

File tree

9 files changed

+135
-120
lines changed

9 files changed

+135
-120
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Added ``macro`` command to create macros, which are similar to aliases, but can take arguments when called
2525
* ``alias`` is now an argparse command with subcommands to create, list, and delete aliases
2626
* Deprecations
27-
* Deprecated the builtin ``cmd2`` support for colors including ``Cmd.colorize()`` and ``Cmd._colorcodes``
27+
* Deprecated the built-in ``cmd2`` support for colors including ``Cmd.colorize()`` and ``Cmd._colorcodes``
2828
* `unalias` is no longer a command since ``alias delete`` replaced it
2929
* Deletions
3030
* The ``preparse``, ``postparsing_precmd``, and ``postparsing_postcmd`` methods *deprecated* in the previous release

cmd2/cmd2.py

Lines changed: 102 additions & 101 deletions
Large diffs are not rendered by default.

cmd2/parsing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Macro:
5959
required_arg_count = attr.ib(validator=attr.validators.instance_of(int))
6060

6161
# Used to fill in argument placeholders in the macro
62-
arg_list = attr.ib(factory=list, validator=attr.validators.instance_of(list))
62+
arg_list = attr.ib(default=attr.Factory(list), validator=attr.validators.instance_of(list))
6363

6464

6565
@attr.s(frozen=True)
@@ -308,17 +308,17 @@ def is_valid_command(self, word: str) -> Tuple[bool, str]:
308308
309309
valid, errmsg = statement_parser.is_valid_command('>')
310310
if not valid:
311-
errmsg = "Aliases {}".format(errmsg)
311+
errmsg = "Alias {}".format(errmsg)
312312
"""
313313
valid = False
314314

315315
if not word:
316316
return False, 'cannot be an empty string'
317317

318-
errmsg = 'cannot start with a shortcut: '
319-
errmsg += ', '.join(shortcut for (shortcut, expansion) in self.shortcuts)
320318
for (shortcut, expansion) in self.shortcuts:
321319
if word.startswith(shortcut):
320+
errmsg = 'cannot start with a shortcut: '
321+
errmsg += ', '.join(shortcut for (shortcut, expansion) in self.shortcuts)
322322
return False, errmsg
323323

324324
errmsg = 'cannot contain: whitespace, quotes, '

cmd2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def __getattr__(self, item: str):
308308

309309
def unquote_redirection_tokens(args: List[str]) -> None:
310310
"""
311-
Used to unquote redirection tokens in a list of command line arguments
311+
Unquote redirection tokens in a list of command-line arguments
312312
This is used when redirection tokens have to be passed to another command
313313
:param args: the command line args
314314
"""

examples/.cmd2rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
alias ls !ls -hal
2-
alias pwd !pwd
1+
alias create ls !ls -hal
2+
alias create pwd !pwd

examples/arg_print.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
# coding=utf-8
33
"""A simple example demonstrating the following:
44
1) How arguments and options get parsed and passed to commands
5-
2) How to change what syntax get parsed as a comment and stripped from
6-
the arguments
5+
2) How to change what syntax get parsed as a comment and stripped from the arguments
76
87
This is intended to serve as a live demonstration so that developers can
98
experiment with and understand how command and argument parsing work.
109
11-
It also serves as an example of how to create command aliases (shortcuts).
10+
It also serves as an example of how to create shortcuts.
1211
"""
1312
import argparse
1413

@@ -18,7 +17,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
1817
""" Example cmd2 application where we create commands that just print the arguments they are called with."""
1918

2019
def __init__(self):
21-
# Create command aliases which are shorter
20+
# Create command shortcuts which are typically 1 character abbreviations which can be used in place of a command
2221
self.shortcuts.update({'$': 'aprint', '%': 'oprint'})
2322

2423
# Make sure to call this super class __init__ *after* setting and/or updating shortcuts

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,9 @@ def get_begidx():
160160
def get_endidx():
161161
return endidx
162162

163-
first_match = None
164163
with mock.patch.object(readline, 'get_line_buffer', get_line):
165164
with mock.patch.object(readline, 'get_begidx', get_begidx):
166165
with mock.patch.object(readline, 'get_endidx', get_endidx):
167166
# Run the readline tab-completion function with readline mocks in place
168167
first_match = app.complete(text, 0)
169-
170-
return first_match
168+
return first_match

tests/test_cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ def test_alias_create_with_macro_name(base_app, capsys):
18521852
run_cmd(base_app, 'macro create {} help'.format(macro))
18531853
run_cmd(base_app, 'alias create {} help'.format(macro))
18541854
out, err = capsys.readouterr()
1855-
assert "Aliases cannot have the same name as a macro" in err
1855+
assert "Alias cannot have the same name as a macro" in err
18561856

18571857
def test_alias_list_invalid_alias(base_app, capsys):
18581858
# Look up invalid alias
@@ -1953,13 +1953,13 @@ def test_macro_create_with_alias_name(base_app, capsys):
19531953
run_cmd(base_app, 'alias create {} help'.format(macro))
19541954
run_cmd(base_app, 'macro create {} help'.format(macro))
19551955
out, err = capsys.readouterr()
1956-
assert "Macros cannot have the same name as an alias" in err
1956+
assert "Macro cannot have the same name as an alias" in err
19571957

19581958
def test_macro_create_with_command_name(base_app, capsys):
19591959
macro = "my_macro"
19601960
run_cmd(base_app, 'macro create help stuff')
19611961
out, err = capsys.readouterr()
1962-
assert "Macros cannot have the same name as a command" in err
1962+
assert "Macro cannot have the same name as a command" in err
19631963

19641964
def test_macro_create_with_args(base_app, capsys):
19651965
# Create the macro

tests/test_completion.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616
import cmd2
1717
from cmd2 import utils
18-
from .conftest import complete_tester
18+
from .conftest import base_app, complete_tester, normalize, run_cmd
1919
from examples.subcommands import SubcommandsExample
2020

2121
# List of strings used with completion functions
@@ -113,6 +113,23 @@ def test_complete_bogus_command(cmd2_app):
113113
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
114114
assert first_match is None
115115

116+
def test_complete_macro(base_app, request):
117+
# Create the macro
118+
out = run_cmd(base_app, 'macro create fake pyscript {1}')
119+
assert out == normalize("Macro 'fake' created")
120+
121+
# Macros do path completion
122+
test_dir = os.path.dirname(request.module.__file__)
123+
124+
text = os.path.join(test_dir, 'script.py')
125+
line = 'fake {}'.format(text)
126+
127+
endidx = len(line)
128+
begidx = endidx - len(text)
129+
130+
first_match = complete_tester(text, line, begidx, endidx, base_app)
131+
assert first_match == text + ' '
132+
116133

117134
def test_cmd2_command_completion_multiple(cmd2_app):
118135
text = 'h'

0 commit comments

Comments
 (0)