Skip to content

Commit ad09ee3

Browse files
committed
parse_command_only now appropriately sets multiline_command
1 parent c4f8870 commit ad09ee3

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

cmd2/parsing.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ def parse_command_only(self, rawinput: str) -> Statement:
407407
"""Partially parse input into a Statement object.
408408
409409
The command is identified, and shortcuts and aliases are expanded.
410-
Terminators, multiline commands, and output redirection are not
411-
parsed.
410+
Multiline commands are identified, but terminators and output
411+
redirection are not parsed.
412412
413413
This method is used by tab completion code and therefore must not
414414
generate an exception if there are unclosed quotes.
@@ -420,8 +420,8 @@ def parse_command_only(self, rawinput: str) -> Statement:
420420
- args
421421
422422
Different from parse(), this method does not remove redundant whitespace
423-
within statement.args. It does however, ensure args does not have leading
424-
or trailing whitespace.
423+
within statement.args. It does however, ensure args does not have
424+
leading or trailing whitespace.
425425
"""
426426
# expand shortcuts and aliases
427427
line = self._expand(rawinput)
@@ -447,13 +447,20 @@ def parse_command_only(self, rawinput: str) -> Statement:
447447
if not command or not args:
448448
args = None
449449

450+
# set multiline
451+
if command in self.multiline_commands:
452+
multiline_command = command
453+
else:
454+
multiline_command = None
455+
450456
# build the statement
451457
# string representation of args must be an empty string instead of
452458
# None for compatibility with standard library cmd
453459
statement = Statement('' if args is None else args,
454460
raw=rawinput,
455461
command=command,
456462
args=args,
463+
multiline_command=multiline_command,
457464
)
458465
return statement
459466

tests/test_parsing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ def test_parse_command_only_none(parser, line):
601601
assert statement.args is None
602602
assert statement == ''
603603

604+
def test_parse_command_only_multiline(parser):
605+
line = 'multiline with partially "open quotes and no terminator'
606+
statement = parser.parse_command_only(line)
607+
assert statement.command == 'multiline'
608+
assert statement.multiline_command == 'multiline'
609+
assert statement.args == 'with partially "open quotes and no terminator'
610+
assert statement == statement.args
611+
assert statement.command_and_args == line
612+
613+
604614
def test_statement_initialization(parser):
605615
string = 'alias'
606616
statement = cmd2.Statement(string)

0 commit comments

Comments
 (0)