Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Enhancements
* Added `silent_startup_script` option to `cmd2.Cmd.__init__()`. If `True`, then the startup script's
output will be suppressed. Anything written to stderr will still display.
* cmd2 now uses pyreadline3 when running Python 3.8 or greater on Windows

## 1.4.0 (November 11, 2020)
* Bug Fixes
Expand Down
3 changes: 3 additions & 0 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3993,6 +3993,9 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
os.remove(self.persistent_history_file)
except FileNotFoundError:
pass
except OSError as ex:
self.pexcept("Error removing history file '{}': {}".format(self.persistent_history_file, ex))
return

if rl_type != RlType.NONE:
readline.clear_history()
Expand Down
4 changes: 4 additions & 0 deletions cmd2/table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def __init__(self, cols: Sequence[Column], *, tab_width: int = 4) -> None:
:param cols: column definitions for this table
:param tab_width: all tabs will be replaced with this many spaces. If a row's fill_char is a tab,
then it will be converted to one space.
:raises: ValueError if tab_width is less than 1
"""
if tab_width < 1:
raise ValueError("Tab width cannot be less than 1")

self.cols = copy.copy(cols)
self.tab_width = tab_width

Expand Down
9 changes: 9 additions & 0 deletions docs/features/startup_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ like so::
This text file should contain a :ref:`Command Script
<features/scripting:Command Scripts>`. See the AliasStartup_ example for a
demonstration.

You can silence a startup script's output by setting ``silent_startup_script``
to True::

cmd2.Cmd.__init__(self, startup_script='.cmd2rc', silent_startup_script=True)

Anything written to stderr will still print. Additionally, a startup script
cannot be silenced if ``allow_redirection`` is False since silencing works
by redirecting a script's output to ``os.devnull``.
13 changes: 12 additions & 1 deletion tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_history_run_one_command(base_app):
out2, err2 = run_cmd(base_app, 'history -r 1')
assert out1 == out2

def test_history_clear(hist_file):
def test_history_clear(mocker, hist_file):
# Add commands to history
app = cmd2.Cmd(persistent_history_file=hist_file)
run_cmd(app, 'help')
Expand All @@ -538,6 +538,17 @@ def test_history_clear(hist_file):
assert out == []
assert not os.path.exists(hist_file)

# Clear the history again and make sure the FileNotFoundError from trying to delete missing history file is silent
run_cmd(app, 'history --clear')

# Cause os.remove to fail and make sure error gets printed
mock_remove = mocker.patch('os.remove')
mock_remove.side_effect = OSError

out, err = run_cmd(app, 'history --clear')
assert out == []
assert 'Error removing history file' in err[0]

def test_history_verbose_with_other_options(base_app):
# make sure -v shows a usage error if any other options are present
options_to_test = ['-r', '-e', '-o file', '-t file', '-c', '-x']
Expand Down
4 changes: 4 additions & 0 deletions tests/test_table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def test_tabs():
inter_cell='\t', post_line='\t')
assert row == ' Col 1 Col 2 '

with pytest.raises(ValueError) as excinfo:
TableCreator([column_1, column_2], tab_width=0)
assert "Tab width cannot be less than 1" in str(excinfo.value)


def test_simple_table_creation():
column_1 = Column("Col 1", width=16)
Expand Down