Repository: python-cmd2/cmd2

Issue:
Unhandled exception in history command introduced by recent changes
A bug was introduced by the recent changes which merged the list functionality into the history command.

When attempting to do a span of history indices, an index error can occur and that exception needs to be caught and default history behavior needs to occur at that point instead.

Discussion:

Patch:
diff --git a/cmd2.py b/cmd2.py
index 3755ef56..13169a17 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1610,8 +1610,11 @@ class Cmd(cmd.Cmd):
         if arg:
             # If a character indicating a slice is present, retrieve a slice of the history
             if '..' in arg or ':' in arg:
-                # Get a slice of history
-                history = self.history.span(arg)
+                try:
+                    # Get a slice of history
+                    history = self.history.span(arg)
+                except IndexError:
+                    history = self.history.get(arg)
             else:
                 # Get item(s) from history by index or string search
                 history = self.history.get(arg)


Test patch:
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 5783d944..2198d5c0 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -206,6 +206,17 @@ shortcuts
 """)
     assert out == expected
 
+def test_history_with_span_index_error(base_app):
+    run_cmd(base_app, 'help')
+    run_cmd(base_app, 'help history')
+    run_cmd(base_app, '!ls -hal :')
+    out = run_cmd(base_app, 'history "hal :"')
+    expected = normalize("""
+-------------------------[3]
+!ls -hal :
+""")
+    assert out == expected
+
 
 def test_base_cmdenvironment(base_app):
     out = run_cmd(base_app, 'cmdenvironment')