Skip to content

Commit 0551fa8

Browse files
committed
Addressed PR comments
1 parent 9c50259 commit 0551fa8

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
filters,
7474
print_formatted_text,
7575
)
76-
from prompt_toolkit.application import get_app
76+
from prompt_toolkit.application import create_app_session, get_app
7777
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
7878
from prompt_toolkit.completion import Completer, DummyCompleter
7979
from prompt_toolkit.formatted_text import ANSI, FormattedText
@@ -4399,10 +4399,11 @@ def select(self, opts: str | Iterable[str] | Iterable[tuple[Any, str | None]], p
43994399
except (IndexError, TypeError):
44004400
fulloptions.append((opt[0], str(opt[0])))
44014401

4402-
if self.stdin.isatty() and self.stdout.isatty():
4402+
if self._is_tty_session(self.main_session):
44034403
try:
44044404
while True:
4405-
result = choice(message=prompt, options=fulloptions)
4405+
with create_app_session(input=self.main_session.input, output=self.main_session.output):
4406+
result = choice(message=prompt, options=fulloptions)
44064407
if result is not None:
44074408
return result
44084409
except KeyboardInterrupt:

docs/features/misc.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Sauce? 2
3434
wheaties with salty sauce, yum!
3535
```
3636

37+
See the `do_eat` method in the
38+
[read_input.py](https://github.com/python-cmd2/cmd2/blob/main/examples/read_input.py) file for a
39+
example of how to use `select.
40+
3741
## Disabling Commands
3842

3943
`cmd2` supports disabling commands during runtime. This is useful if certain commands should only be

examples/read_input.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env python
2-
"""A simple example demonstrating the various ways to call cmd2.Cmd.read_input() for input history and tab completion."""
2+
"""A simple example demonstrating the various ways to call cmd2.Cmd.read_input() for input history and tab completion.
3+
4+
It also demonstrates how to use the cmd2.Cmd.select method.
5+
"""
36

47
import contextlib
58

@@ -94,6 +97,16 @@ def do_custom_parser(self, _) -> None:
9497
else:
9598
self.custom_history.append(input_str)
9699

100+
def do_eat(self, arg):
101+
"""Example of using the select method for reading multiple choice input.
102+
103+
Usage: eat wheatties
104+
"""
105+
sauce = self.select('sweet salty', 'Sauce? ')
106+
result = '{food} with {sauce} sauce, yum!'
107+
result = result.format(food=arg, sauce=sauce)
108+
self.stdout.write(result + '\n')
109+
97110

98111
if __name__ == '__main__':
99112
import sys

examples/remove_settable.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/usr/bin/env python
2-
"""A sample application for cmd2 demonstrating how to remove one of the built-in runtime settable parameters.
3-
4-
It also demonstrates how to use the cmd2.Cmd.select method.
5-
"""
2+
"""A sample application for cmd2 demonstrating how to remove one of the built-in runtime settable parameters."""
63

74
import cmd2
85

@@ -12,12 +9,6 @@ def __init__(self) -> None:
129
super().__init__()
1310
self.remove_settable('debug')
1411

15-
def do_eat(self, arg):
16-
sauce = self.select('sweet salty', 'Sauce? ')
17-
result = '{food} with {sauce} sauce, yum!'
18-
result = result.format(food=arg, sauce=sauce)
19-
self.stdout.write(result + '\n')
20-
2112

2213
if __name__ == '__main__':
2314
import sys

tests/test_cmd2.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,13 +1750,16 @@ def test_select_choice_tty(outsim_app, monkeypatch) -> None:
17501750
choice_mock = mock.MagicMock(name='choice', return_value='sweet')
17511751
monkeypatch.setattr("cmd2.cmd2.choice", choice_mock)
17521752

1753-
# Mock isatty to be True for both stdin and stdout
1754-
monkeypatch.setattr(outsim_app.stdin, "isatty", lambda: True)
1755-
monkeypatch.setattr(outsim_app.stdout, "isatty", lambda: True)
1756-
17571753
prompt = 'Sauce? '
17581754
options = ['sweet', 'salty']
1759-
result = outsim_app.select(options, prompt)
1755+
1756+
with create_pipe_input() as pipe_input:
1757+
outsim_app.main_session = PromptSession(
1758+
input=pipe_input,
1759+
output=DummyOutput(),
1760+
)
1761+
1762+
result = outsim_app.select(options, prompt)
17601763

17611764
assert result == 'sweet'
17621765
choice_mock.assert_called_once_with(message=prompt, options=[('sweet', 'sweet'), ('salty', 'salty')])
@@ -1767,17 +1770,20 @@ def test_select_choice_tty_ctrl_c(outsim_app, monkeypatch) -> None:
17671770
choice_mock = mock.MagicMock(name='choice', side_effect=KeyboardInterrupt)
17681771
monkeypatch.setattr("cmd2.cmd2.choice", choice_mock)
17691772

1770-
# Mock isatty to be True for both stdin and stdout
1771-
monkeypatch.setattr(outsim_app.stdin, "isatty", lambda: True)
1772-
monkeypatch.setattr(outsim_app.stdout, "isatty", lambda: True)
1773-
17741773
prompt = 'Sauce? '
17751774
options = ['sweet', 'salty']
17761775

1777-
with pytest.raises(KeyboardInterrupt):
1778-
outsim_app.select(options, prompt)
1776+
# Mock isatty to be True for both stdin and stdout
1777+
with create_pipe_input() as pipe_input:
1778+
outsim_app.main_session = PromptSession(
1779+
input=pipe_input,
1780+
output=DummyOutput(),
1781+
)
17791782

1780-
out = outsim_app.stdout.getvalue()
1783+
with pytest.raises(KeyboardInterrupt):
1784+
outsim_app.select(options, prompt)
1785+
1786+
out = outsim_app.stdout.getvalue()
17811787
assert out.rstrip().endswith('^C')
17821788

17831789

0 commit comments

Comments
 (0)