Skip to content

Commit 44eb9d4

Browse files
authored
Merge pull request #1045 from python-cmd2/silent_start
Added option to run startup scripts silently
2 parents 5f1ea98 + bc01fec commit 44eb9d4

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* Bug Fixes
33
* Fixed bug where setting `always_show_hint=True` did not show a hint when completing `Settables`
44
* Fixed bug in editor detection logic on Linux systems that do not have `which`
5+
* Enhancements
6+
* Added `silent_startup_script` option to `cmd2.Cmd.__init__()`. If `True`, then the startup script's
7+
output will be suppressed. Anything written to stderr will still display.
58

69
## 1.4.0 (November 11, 2020)
710
* Bug Fixes

cmd2/cmd2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class Cmd(cmd.Cmd):
199199

200200
def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
201201
persistent_history_file: str = '', persistent_history_length: int = 1000,
202-
startup_script: str = '', use_ipython: bool = False,
202+
startup_script: str = '', silent_startup_script: bool = False, use_ipython: bool = False,
203203
allow_cli_args: bool = True, transcript_files: Optional[List[str]] = None,
204204
allow_redirection: bool = True, multiline_commands: Optional[List[str]] = None,
205205
terminators: Optional[List[str]] = None, shortcuts: Optional[Dict[str, str]] = None,
@@ -215,6 +215,8 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
215215
:param persistent_history_length: max number of history items to write
216216
to the persistent history file
217217
:param startup_script: file path to a script to execute at startup
218+
:param silent_startup_script: if ``True``, then the startup script's output will be
219+
suppressed. Anything written to stderr will still display.
218220
:param use_ipython: should the "ipy" command be included for an embedded IPython shell
219221
:param allow_cli_args: if ``True``, then :meth:`cmd2.Cmd.__init__` will process command
220222
line arguments as either commands to be run or, if ``-t`` or
@@ -363,7 +365,10 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
363365
if startup_script:
364366
startup_script = os.path.abspath(os.path.expanduser(startup_script))
365367
if os.path.exists(startup_script):
366-
self._startup_commands.append("run_script {}".format(utils.quote_string(startup_script)))
368+
script_cmd = "run_script {}".format(utils.quote_string(startup_script))
369+
if silent_startup_script:
370+
script_cmd += "> {}".format(os.devnull)
371+
self._startup_commands.append(script_cmd)
367372

368373
# Transcript files to run instead of interactive command loop
369374
self._transcript_files = None # type: Optional[List[str]]

tests/test_cmd2.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,15 +2399,24 @@ def test_disabled_message_command_name(disable_commands_app):
23992399
out, err = run_cmd(disable_commands_app, 'has_helper_funcs')
24002400
assert err[0].startswith('has_helper_funcs is currently disabled')
24012401

2402-
2403-
def test_startup_script(request):
2402+
@pytest.mark.parametrize('silent_startup_script', [
2403+
True,
2404+
False
2405+
])
2406+
def test_startup_script(request, capsys, silent_startup_script):
24042407
test_dir = os.path.dirname(request.module.__file__)
24052408
startup_script = os.path.join(test_dir, '.cmd2rc')
2406-
app = cmd2.Cmd(allow_cli_args=False, startup_script=startup_script)
2409+
app = cmd2.Cmd(allow_cli_args=False, startup_script=startup_script, silent_startup_script=silent_startup_script)
24072410
assert len(app._startup_commands) == 1
2408-
assert app._startup_commands[0] == "run_script {}".format(utils.quote_string(startup_script))
24092411
app._startup_commands.append('quit')
24102412
app.cmdloop()
2413+
2414+
out, err = capsys.readouterr()
2415+
if silent_startup_script:
2416+
assert not out
2417+
else:
2418+
assert out
2419+
24112420
out, err = run_cmd(app, 'alias list')
24122421
assert len(out) > 1
24132422
assert 'alias create ls' in out[0]

0 commit comments

Comments
 (0)