Skip to content

Commit 2bac6bc

Browse files
committed
Made some optional arguments with defaults keyword-only.
Added unit test for echo argument to pyscript app() command. Removed _relative_load from hidden commands since that command was renamed.
1 parent 0d4be64 commit 2bac6bc

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

cmd2/cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
213213
self.self_in_py = False
214214

215215
# Commands to exclude from the help menu and tab completion
216-
self.hidden_commands = ['eof', '_relative_load', '_relative_run_script']
216+
self.hidden_commands = ['eof', '_relative_run_script']
217217

218218
# Initialize history
219219
self._persistent_history_length = persistent_history_length
@@ -258,7 +258,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
258258
self.sigint_protection = utils.ContextFlag()
259259

260260
# If the current command created a process to pipe to, then this will be a ProcReader object.
261-
# Otherwise it will be None. Its used to know when a pipe process can be killed and/or waited upon.
261+
# Otherwise it will be None. It's used to know when a pipe process can be killed and/or waited upon.
262262
self._cur_pipe_proc_reader = None
263263

264264
# Used to keep track of whether we are redirecting or piping output
@@ -3904,7 +3904,7 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
39043904
alert_msg += '\n'
39053905
update_terminal = True
39063906

3907-
# Set the prompt if its changed
3907+
# Set the prompt if it's changed
39083908
if new_prompt is not None and new_prompt != self.prompt:
39093909
self.prompt = new_prompt
39103910

cmd2/py_bridge.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,26 @@ def __dir__(self):
7474
attributes.insert(0, 'cmd_echo')
7575
return attributes
7676

77-
def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult:
77+
def __call__(self, command: str, *, echo: Optional[bool] = None) -> CommandResult:
7878
"""
7979
Provide functionality to call application commands by calling PyBridge
8080
ex: app('help')
8181
:param command: command line being run
82-
:param echo: if True, output will be echoed to stdout/stderr while the command runs
83-
this temporarily overrides the value of self.cmd_echo
82+
:param echo: If provided, this temporarily overrides the value of self.cmd_echo while the
83+
command runs. If True, output will be echoed to stdout/stderr. (Defaults to None)
84+
8485
"""
8586
if echo is None:
8687
echo = self.cmd_echo
8788

8889
# This will be used to capture _cmd2_app.stdout and sys.stdout
89-
copy_cmd_stdout = StdSim(self._cmd2_app.stdout, echo)
90+
copy_cmd_stdout = StdSim(self._cmd2_app.stdout, echo=echo)
9091

9192
# Pause the storing of stdout until onecmd_plus_hooks enables it
9293
copy_cmd_stdout.pause_storage = True
9394

9495
# This will be used to capture sys.stderr
95-
copy_stderr = StdSim(sys.stderr, echo)
96+
copy_stderr = StdSim(sys.stderr, echo=echo)
9697

9798
self._cmd2_app.last_result = None
9899

cmd2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ class StdSim:
434434
Class to simulate behavior of sys.stdout or sys.stderr.
435435
Stores contents in internal buffer and optionally echos to the inner stream it is simulating.
436436
"""
437-
def __init__(self, inner_stream, echo: bool = False,
437+
def __init__(self, inner_stream, *, echo: bool = False,
438438
encoding: str = 'utf-8', errors: str = 'replace') -> None:
439439
"""
440440
StdSim Initializer

tests/pyscript/echo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# flake8: noqa F821
2+
# Tests echo argument to app()
3+
app.cmd_echo = False
4+
5+
# echo defaults to current setting which is False, so this help text should not be echoed to pytest's stdout
6+
app('help alias')
7+
8+
# pytest's stdout should have this help text written to it
9+
app('help edit', echo=True)

tests/test_run_pyscript.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def test_run_pyscript_dir(base_app, request):
9191
python_script = os.path.join(test_dir, 'pyscript', 'pyscript_dir.py')
9292

9393
out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
94-
assert out
9594
assert out[0] == "['cmd_echo']"
9695

9796
def test_run_pyscript_stdout_capture(base_app, request):
@@ -123,3 +122,11 @@ def test_run_pyscript_environment(base_app, request):
123122
out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
124123

125124
assert out[0] == "PASSED"
125+
126+
def test_run_pyscript_echp(base_app, request):
127+
test_dir = os.path.dirname(request.module.__file__)
128+
python_script = os.path.join(test_dir, 'pyscript', 'echo.py')
129+
out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
130+
131+
# Only the edit help text should have been echoed to pytest's stdout
132+
assert out[0] == "Usage: edit [-h] [file_path]"

0 commit comments

Comments
 (0)