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
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
News
====

0.7.3
-----

*Release date: TBD*

* Bug fixes
* Fixed a bug in display a span of history items when only an end index is supplied
* Enhancements
* Added the ability to exclude commands from the help menu (**eof** included by default)
* Redundant list command removed and features merged into history command

0.7.2
-----

Expand Down
58 changes: 20 additions & 38 deletions cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,12 @@ def do_cmdenvironment(self, args):
Commands may be terminated with: {}
Command-line arguments allowed: {}
Output redirection and pipes allowed: {}
Settable parameters: {}\n""".format(not self.case_insensitive, str(self.terminators),
self.allow_cli_args,
self.allow_redirection, ' '.join(self.settable)))
Parsing of @options commands:
Use POSIX-style argument parser (vs Windows): {}
Strip Quotes when using Windows-style argument parser: {}
Use a list of arguments instead of a single argument string: {}
\n""".format(not self.case_insensitive, str(self.terminators), self.allow_cli_args, self.allow_redirection,
POSIX_SHLEX, STRIP_QUOTES_FOR_NON_POSIX, USE_ARG_LIST))

def do_help(self, arg):
"""List available commands with "help" or detailed help with "help cmd"."""
Expand Down Expand Up @@ -1592,7 +1595,8 @@ def do_history(self, arg, opts):

| no arg: list all
| arg is integer: list one history item, by index
| arg is string: string search
| a..b, a:b, a:, ..b -> list history items by a span of indices (inclusive)
| arg is string: list all commands matching string search
| arg is /enclosed in forward-slashes/: regular expression search
"""
# If arguments are being passed as a list instead of as a string
Expand All @@ -1602,10 +1606,20 @@ def do_history(self, arg, opts):
else:
arg = ''

# If an argument was supplied, then retrieve partial contents of the history
if arg:
history = self.history.get(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)
else:
# Get item(s) from history by index or string search
history = self.history.get(arg)
else:
# If no arg given, then retrieve the entire history
history = self.history

# Display the history items retrieved
for hi in history:
if opts.script:
self.poutput(hi)
Expand All @@ -1628,38 +1642,6 @@ def _last_matching(self, arg):
except IndexError:
return None

def do_list(self, arg):
"""list [arg]: lists command(s) from history in a flexible/searchable way.

:param arg: str - behavior varies as follows:

* no arg -> list most recent command
* arg is integer -> list one history item, by index
* a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end)
* arg is string -> list all commands matching string search
* arg is /enclosed in forward-slashes/ -> regular expression search
"""
try:
history = self.history.span(arg or '-1')
except IndexError:
history = self.history.search(arg)
for hi in history:
self.poutput(hi.pr())

def help_list(self):
"""Print help for do_list()."""
help_str = """Lists command(s) from history in a flexible/searchable way.

Usage: list [arg]

Where arg is:
no arg -> list most recent command
arg is integer -> list one history item, by index
a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end)
arg is string -> list all commands matching string search
arg is /enclosed in forward-slashes/ -> regular expression search"""
self.stdout.write("{}\n".format(help_str))

def do_edit(self, arg):
"""Edit a file or command in a text editor.

Expand Down Expand Up @@ -2170,7 +2152,7 @@ def span(self, raw):
raise IndexError
if not results.group('separator'):
return [self[self._to_index(results.group('start'))]]
start = self._to_index(results.group('start'))
start = self._to_index(results.group('start')) or 0 # Ensure start is not None
end = self._to_index(results.group('end'))
reverse = False
if end is not None:
Expand Down
2 changes: 0 additions & 2 deletions docs/freefeatures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ also provide `bash-like history list editing`_.

.. automethod:: cmd2.Cmd.do_history

.. automethod:: cmd2.Cmd.do_list

.. automethod:: cmd2.Cmd.do_run

Quitting the application
Expand Down
4 changes: 2 additions & 2 deletions examples/exampleSession.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

Documented commands (type help <topic>):
========================================
_relative_load edit help list orate py run say shell show
cmdenvironment eof history load pause quit save set shortcuts speak
_relative_load edit history orate py run say shell show
cmdenvironment help load pause quit save set shortcuts speak

(Cmd) help say
Repeats what you tell me to.
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
# Help text for base cmd2.Cmd application
BASE_HELP = """Documented commands (type help <topic>):
========================================
_relative_load edit history load py run set shortcuts
cmdenvironment help list pause quit save shell show
_relative_load edit history pause quit save shell show
cmdenvironment help load py run set shortcuts
"""

# Help text for the history command
HELP_HISTORY = """history [arg]: lists past commands issued

| no arg: list all
| arg is integer: list one history item, by index
| arg is string: string search
| a..b, a:b, a:, ..b -> list history items by a span of indices (inclusive)
| arg is string: list all commands matching string search
| arg is /enclosed in forward-slashes/: regular expression search

Usage: history [options] (limit on which commands to include)
Expand Down
60 changes: 35 additions & 25 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,48 +142,62 @@ def test_base_history(base_app):
""")
assert out == expected


def test_base_list(base_app):
def test_history_with_string_argument(base_app):
run_cmd(base_app, 'help')
run_cmd(base_app, 'shortcuts')
out = run_cmd(base_app, 'list')
run_cmd(base_app, 'help history')
out = run_cmd(base_app, 'history help')
expected = normalize("""
-------------------------[2]
shortcuts
-------------------------[1]
help
-------------------------[3]
help history
""")
assert out == expected


def test_list_with_string_argument(base_app):
def test_history_with_integer_argument(base_app):
run_cmd(base_app, 'help')
run_cmd(base_app, 'shortcuts')
run_cmd(base_app, 'help list')
out = run_cmd(base_app, 'list help')
out = run_cmd(base_app, 'history 1')
expected = normalize("""
-------------------------[1]
help
-------------------------[3]
help list
""")
assert out == expected


def test_list_with_integer_argument(base_app):
def test_history_with_integer_span(base_app):
run_cmd(base_app, 'help')
run_cmd(base_app, 'shortcuts')
out = run_cmd(base_app, 'list 1')
run_cmd(base_app, 'help history')
out = run_cmd(base_app, 'history 1..2')
expected = normalize("""
-------------------------[1]
help
-------------------------[2]
shortcuts
""")
assert out == expected

def test_history_with_span_start(base_app):
run_cmd(base_app, 'help')
run_cmd(base_app, 'shortcuts')
run_cmd(base_app, 'help history')
out = run_cmd(base_app, 'history 2:')
expected = normalize("""
-------------------------[2]
shortcuts
-------------------------[3]
help history
""")
assert out == expected

def test_list_with_integer_span(base_app):
def test_history_with_span_end(base_app):
run_cmd(base_app, 'help')
run_cmd(base_app, 'shortcuts')
run_cmd(base_app, 'help list')
out = run_cmd(base_app, 'list 1..2')
run_cmd(base_app, 'help history')
out = run_cmd(base_app, 'history :2')
expected = normalize("""
-------------------------[1]
help
Expand All @@ -201,17 +215,13 @@ def test_base_cmdenvironment(base_app):
Commands may be terminated with: [';']
Command-line arguments allowed: True
Output redirection and pipes allowed: True
Parsing of @options commands:
Use POSIX-style argument parser (vs Windows): False
Strip Quotes when using Windows-style argument parser: True
Use a list of arguments instead of a single argument string: False

""")
assert out[:4] == expected[:4]
assert out[4].strip().startswith('Settable parameters: ')

# Settable parameters can be listed in any order, so need to validate carefully using unordered sets
settable_params = {'continuation_prompt', 'default_file_name', 'prompt', 'abbrev', 'quiet', 'case_insensitive',
'colors', 'echo', 'timing', 'editor', 'feedback_to_output', 'debug', 'autorun_on_edit',
'locals_in_py'}
out_params = set(out[4].split("Settable parameters: ")[1].split())
assert settable_params == out_params

assert out == expected

def test_base_load(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ def test_base_with_transcript(_cmdline_app):

Documented commands (type help <topic>):
========================================
_relative_load help load py save shell speak
cmdenvironment history orate quit say shortcuts
edit list pause run set show
_relative_load edit history orate py run say shell show
cmdenvironment help load pause quit save set shortcuts speak

(Cmd) help say
Repeats what you tell me to.
Expand Down
5 changes: 2 additions & 3 deletions tests/transcript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

Documented commands (type help <topic>):
========================================
_relative_load help load py save shell speak
cmdenvironment history orate quit say shortcuts
edit list pause run set show
_relative_load edit history orate py run say shell show
cmdenvironment help load pause quit save set shortcuts speak

(Cmd) help say
Repeats what you tell me to.
Expand Down