Skip to content

Commit 09b2998

Browse files
committed
Changed Statement.multiline_command field from a string to a bool.
1 parent c2d29d4 commit 09b2998

File tree

6 files changed

+39
-44
lines changed

6 files changed

+39
-44
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ shell, and the option for a persistent bottom bar that can display realtime stat
3333
- `CompletionItem.descriptive_data` is now called `CompletionItem.table_row`.
3434
- `Cmd.default_sort_key` moved to `utils.DEFAULT_STR_SORT_KEY`.
3535
- Moved completion state data, which previously resided in `Cmd`, into other classes.
36-
1. `Cmd.matches_sorted` -> `Completions.is_sorted` and `Choices.is_sorted`
37-
1. `Cmd.completion_hint` -> `Completions.completion_hint`
38-
1. `Cmd.formatted_completions` -> `Completions.completion_table`
39-
1. `Cmd.matches_delimited` -> `Completions.is_delimited`
40-
1. `Cmd.allow_appended_space/allow_closing_quote` -> `Completions.allow_finalization`
36+
- `Cmd.matches_sorted` -> `Completions.is_sorted` and `Choices.is_sorted`
37+
- `Cmd.completion_hint` -> `Completions.completion_hint`
38+
- `Cmd.formatted_completions` -> `Completions.completion_table`
39+
- `Cmd.matches_delimited` -> `Completions.is_delimited`
40+
- `Cmd.allow_appended_space/allow_closing_quote` -> `Completions.allow_finalization`
4141
- Removed `flag_based_complete` and `index_based_complete` functions since their functionality
4242
is already provided in arpgarse-based completion.
43+
- Changed `Statement.multiline_command` field from a string to a bool.
4344
- Enhancements
4445
- New `cmd2.Cmd` parameters
4546
- **auto_suggest**: (boolean) if `True`, provide fish shell style auto-suggestions. These

cmd2/cmd2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,8 +2907,7 @@ def _input_line_to_statement(self, line: str) -> Statement:
29072907
# Make sure all input has been read and convert it to a Statement
29082908
statement = self._complete_statement(line)
29092909

2910-
# If this is the first loop iteration, save the original line and stop
2911-
# combining multiline history entries in the remaining iterations.
2910+
# If this is the first loop iteration, save the original line
29122911
if orig_line is None:
29132912
orig_line = statement.raw
29142913

cmd2/parsing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class Statement(str): # noqa: SLOT000
129129
# list of arguments to the command, not including any output redirection or terminators; quoted args remain quoted
130130
arg_list: list[str] = field(default_factory=list)
131131

132-
# if the command is a multiline command, the name of the command, otherwise empty
133-
multiline_command: str = ''
132+
# if the command is a multiline command
133+
multiline_command: bool = False
134134

135135
# the character which terminated the multiline command, if there was one
136136
terminator: str = ''
@@ -510,7 +510,7 @@ def parse(self, line: str) -> Statement:
510510
arg_list = tokens[1:]
511511

512512
# set multiline
513-
multiline_command = command if command in self.multiline_commands else ''
513+
multiline_command = command in self.multiline_commands
514514

515515
# build the statement
516516
return Statement(
@@ -580,7 +580,7 @@ def parse_command_only(self, rawinput: str) -> Statement:
580580
args = ''
581581

582582
# set multiline
583-
multiline_command = command if command in self.multiline_commands else ''
583+
multiline_command = command in self.multiline_commands
584584

585585
# build the statement
586586
return Statement(args, raw=rawinput, command=command, multiline_command=multiline_command)

tests/test_cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def test_multiline_complete_statement_without_terminator(multiline_app, monkeypa
17671767
statement = multiline_app._complete_statement(line)
17681768
assert statement == args
17691769
assert statement.command == command
1770-
assert statement.multiline_command == command
1770+
assert statement.multiline_command
17711771

17721772

17731773
def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkeypatch) -> None:
@@ -1780,7 +1780,7 @@ def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkey
17801780
statement = multiline_app._complete_statement(line)
17811781
assert statement == 'hi "partially open\nquotes\n" now closed'
17821782
assert statement.command == 'orate'
1783-
assert statement.multiline_command == 'orate'
1783+
assert statement.multiline_command
17841784
assert statement.terminator == ';'
17851785

17861786

@@ -1797,7 +1797,7 @@ def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
17971797
assert statement.raw == 'orate hi\nperson\n\n'
17981798
assert statement == 'hi person'
17991799
assert statement.command == 'orate'
1800-
assert statement.multiline_command == 'orate'
1800+
assert statement.multiline_command
18011801

18021802

18031803
def test_multiline_history_added(multiline_app, monkeypatch) -> None:

tests/test_history.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def hist():
8585
' "raw": "first",\n'
8686
' "command": "",\n'
8787
' "arg_list": [],\n'
88-
' "multiline_command": "",\n'
88+
' "multiline_command": false,\n'
8989
' "terminator": "",\n'
9090
' "suffix": "",\n'
9191
' "pipe_to": "",\n'
@@ -99,7 +99,7 @@ def hist():
9999
' "raw": "second",\n'
100100
' "command": "",\n'
101101
' "arg_list": [],\n'
102-
' "multiline_command": "",\n'
102+
' "multiline_command": false,\n'
103103
' "terminator": "",\n'
104104
' "suffix": "",\n'
105105
' "pipe_to": "",\n'
@@ -113,7 +113,7 @@ def hist():
113113
' "raw": "third",\n'
114114
' "command": "",\n'
115115
' "arg_list": [],\n'
116-
' "multiline_command": "",\n'
116+
' "multiline_command": false,\n'
117117
' "terminator": "",\n'
118118
' "suffix": "",\n'
119119
' "pipe_to": "",\n'
@@ -127,7 +127,7 @@ def hist():
127127
' "raw": "fourth",\n'
128128
' "command": "",\n'
129129
' "arg_list": [],\n'
130-
' "multiline_command": "",\n'
130+
' "multiline_command": false,\n'
131131
' "terminator": "",\n'
132132
' "suffix": "",\n'
133133
' "pipe_to": "",\n'

tests/test_parsing.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_parse_empty_string(parser) -> None:
4646
assert statement.raw == line
4747
assert statement.command == ''
4848
assert statement.arg_list == []
49-
assert statement.multiline_command == ''
49+
assert not statement.multiline_command
5050
assert statement.terminator == ''
5151
assert statement.suffix == ''
5252
assert statement.pipe_to == ''
@@ -64,7 +64,7 @@ def test_parse_empty_string_default(default_parser) -> None:
6464
assert statement.raw == line
6565
assert statement.command == ''
6666
assert statement.arg_list == []
67-
assert statement.multiline_command == ''
67+
assert not statement.multiline_command
6868
assert statement.terminator == ''
6969
assert statement.suffix == ''
7070
assert statement.pipe_to == ''
@@ -144,7 +144,7 @@ def test_parse_single_word(parser, line) -> None:
144144
assert not statement.arg_list
145145
assert statement.args == statement
146146
assert statement.raw == line
147-
assert statement.multiline_command == ''
147+
assert not statement.multiline_command
148148
assert statement.terminator == ''
149149
assert statement.suffix == ''
150150
assert statement.pipe_to == ''
@@ -527,7 +527,7 @@ def test_parse_redirect_inside_terminator(parser) -> None:
527527
)
528528
def test_parse_multiple_terminators(parser, line, terminator) -> None:
529529
statement = parser.parse(line)
530-
assert statement.multiline_command == 'multiline'
530+
assert statement.multiline_command
531531
assert statement == 'with | inside'
532532
assert statement.args == statement
533533
assert statement.argv == ['multiline', 'with', '|', 'inside']
@@ -538,7 +538,7 @@ def test_parse_multiple_terminators(parser, line, terminator) -> None:
538538
def test_parse_unfinished_multiliine_command(parser) -> None:
539539
line = 'multiline has > inside an unfinished command'
540540
statement = parser.parse(line)
541-
assert statement.multiline_command == 'multiline'
541+
assert statement.multiline_command
542542
assert statement.command == 'multiline'
543543
assert statement == 'has > inside an unfinished command'
544544
assert statement.args == statement
@@ -550,7 +550,7 @@ def test_parse_unfinished_multiliine_command(parser) -> None:
550550
def test_parse_basic_multiline_command(parser) -> None:
551551
line = 'multiline foo\nbar\n\n'
552552
statement = parser.parse(line)
553-
assert statement.multiline_command == 'multiline'
553+
assert statement.multiline_command
554554
assert statement.command == 'multiline'
555555
assert statement == 'foo bar'
556556
assert statement.args == statement
@@ -572,7 +572,7 @@ def test_parse_basic_multiline_command(parser) -> None:
572572
)
573573
def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, terminator) -> None:
574574
statement = parser.parse(line)
575-
assert statement.multiline_command == 'multiline'
575+
assert statement.multiline_command
576576
assert statement == 'has > inside'
577577
assert statement.args == statement
578578
assert statement.argv == ['multiline', 'has', '>', 'inside']
@@ -583,7 +583,7 @@ def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, ter
583583
def test_parse_multiline_terminated_by_empty_line(parser) -> None:
584584
line = 'multiline command ends\n\n'
585585
statement = parser.parse(line)
586-
assert statement.multiline_command == 'multiline'
586+
assert statement.multiline_command
587587
assert statement.command == 'multiline'
588588
assert statement == 'command ends'
589589
assert statement.args == statement
@@ -605,7 +605,7 @@ def test_parse_multiline_terminated_by_empty_line(parser) -> None:
605605
)
606606
def test_parse_multiline_with_embedded_newline(parser, line, terminator) -> None:
607607
statement = parser.parse(line)
608-
assert statement.multiline_command == 'multiline'
608+
assert statement.multiline_command
609609
assert statement.command == 'multiline'
610610
assert statement == 'command "with\nembedded newline"'
611611
assert statement.args == statement
@@ -617,7 +617,7 @@ def test_parse_multiline_with_embedded_newline(parser, line, terminator) -> None
617617
def test_parse_multiline_ignores_terminators_in_quotes(parser) -> None:
618618
line = 'multiline command "with term; ends" now\n\n'
619619
statement = parser.parse(line)
620-
assert statement.multiline_command == 'multiline'
620+
assert statement.multiline_command
621621
assert statement.command == 'multiline'
622622
assert statement == 'command "with term; ends" now'
623623
assert statement.args == statement
@@ -694,7 +694,7 @@ def test_parse_alias_and_shortcut_expansion(parser, line, command, args) -> None
694694
def test_parse_alias_on_multiline_command(parser) -> None:
695695
line = 'anothermultiline has > inside an unfinished command'
696696
statement = parser.parse(line)
697-
assert statement.multiline_command == 'multiline'
697+
assert statement.multiline_command
698698
assert statement.command == 'multiline'
699699
assert statement.args == statement
700700
assert statement == 'has > inside an unfinished command'
@@ -761,7 +761,7 @@ def test_parse_command_only_command_and_args(parser) -> None:
761761
assert statement.arg_list == []
762762
assert statement.command == 'help'
763763
assert statement.command_and_args == line
764-
assert statement.multiline_command == ''
764+
assert not statement.multiline_command
765765
assert statement.raw == line
766766
assert statement.terminator == ''
767767
assert statement.suffix == ''
@@ -778,7 +778,7 @@ def test_parse_command_only_strips_line(parser) -> None:
778778
assert statement.arg_list == []
779779
assert statement.command == 'help'
780780
assert statement.command_and_args == line.strip()
781-
assert statement.multiline_command == ''
781+
assert not statement.multiline_command
782782
assert statement.raw == line
783783
assert statement.terminator == ''
784784
assert statement.suffix == ''
@@ -795,7 +795,7 @@ def test_parse_command_only_expands_alias(parser) -> None:
795795
assert statement.arg_list == []
796796
assert statement.command == 'run_pyscript'
797797
assert statement.command_and_args == 'run_pyscript foobar.py "somebody.py'
798-
assert statement.multiline_command == ''
798+
assert not statement.multiline_command
799799
assert statement.raw == line
800800
assert statement.terminator == ''
801801
assert statement.suffix == ''
@@ -812,9 +812,8 @@ def test_parse_command_only_expands_shortcuts(parser) -> None:
812812
assert statement.arg_list == []
813813
assert statement.command == 'shell'
814814
assert statement.command_and_args == 'shell cat foobar.txt'
815-
assert statement.multiline_command == ''
815+
assert not statement.multiline_command
816816
assert statement.raw == line
817-
assert statement.multiline_command == ''
818817
assert statement.terminator == ''
819818
assert statement.suffix == ''
820819
assert statement.pipe_to == ''
@@ -830,9 +829,8 @@ def test_parse_command_only_quoted_args(parser) -> None:
830829
assert statement.arg_list == []
831830
assert statement.command == 'shell'
832831
assert statement.command_and_args == line.replace('l', 'shell ls -al')
833-
assert statement.multiline_command == ''
832+
assert not statement.multiline_command
834833
assert statement.raw == line
835-
assert statement.multiline_command == ''
836834
assert statement.terminator == ''
837835
assert statement.suffix == ''
838836
assert statement.pipe_to == ''
@@ -849,9 +847,8 @@ def test_parse_command_only_unclosed_quote(parser) -> None:
849847
assert statement.arg_list == []
850848
assert statement.command == 'command'
851849
assert statement.command_and_args == line
852-
assert statement.multiline_command == ''
850+
assert not statement.multiline_command
853851
assert statement.raw == line
854-
assert statement.multiline_command == ''
855852
assert statement.terminator == ''
856853
assert statement.suffix == ''
857854
assert statement.pipe_to == ''
@@ -877,9 +874,8 @@ def test_parse_command_only_specialchars(parser, line, args) -> None:
877874
assert statement == args
878875
assert statement.args == args
879876
assert statement.command == 'help'
880-
assert statement.multiline_command == ''
877+
assert not statement.multiline_command
881878
assert statement.raw == line
882-
assert statement.multiline_command == ''
883879
assert statement.terminator == ''
884880
assert statement.suffix == ''
885881
assert statement.pipe_to == ''
@@ -910,9 +906,8 @@ def test_parse_command_only_empty(parser, line) -> None:
910906
assert statement.arg_list == []
911907
assert statement.command == ''
912908
assert statement.command_and_args == ''
913-
assert statement.multiline_command == ''
909+
assert not statement.multiline_command
914910
assert statement.raw == line
915-
assert statement.multiline_command == ''
916911
assert statement.terminator == ''
917912
assert statement.suffix == ''
918913
assert statement.pipe_to == ''
@@ -924,7 +919,7 @@ def test_parse_command_only_multiline(parser) -> None:
924919
line = 'multiline with partially "open quotes and no terminator'
925920
statement = parser.parse_command_only(line)
926921
assert statement.command == 'multiline'
927-
assert statement.multiline_command == 'multiline'
922+
assert statement.multiline_command
928923
assert statement == 'with partially "open quotes and no terminator'
929924
assert statement.command_and_args == line
930925
assert statement.args == statement
@@ -941,7 +936,7 @@ def test_statement_initialization() -> None:
941936
assert not statement.arg_list
942937
assert isinstance(statement.argv, list)
943938
assert not statement.argv
944-
assert statement.multiline_command == ''
939+
assert not statement.multiline_command
945940
assert statement.terminator == ''
946941
assert statement.suffix == ''
947942
assert isinstance(statement.pipe_to, str)

0 commit comments

Comments
 (0)