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
19 changes: 9 additions & 10 deletions cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,13 +1589,11 @@ def _last_matching(self, arg):
except IndexError:
return None

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

Usage: edit [N]|[file_path]

@options([], arg_desc="""[N]|[file_path]
* N - Number of command (from history), or `*` for all commands in history (default: last command)
* file_path - path to a file to open in editor
* file_path - path to a file to open in editor""")
def do_edit(self, arg, opts=None):
"""Edit a file or command in a text editor.

The editor used is determined by the ``editor`` settable parameter.
"set editor (program-name)" to change or set the EDITOR environment variable.
Expand All @@ -1605,15 +1603,16 @@ def do_edit(self, arg):

Edited commands are always run after the editor is closed.

Edited files are run on close if the ``autorun_on_edit`` settable parameter is True."""
Edited files are run on close if the ``autorun_on_edit`` settable parameter is True.
"""
if not self.editor:
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
filename = None
if arg:
if arg and arg[0]:
try:
history_item = self._last_matching(int(arg))
history_item = self._last_matching(int(arg[0]))
except ValueError:
filename = arg
filename = arg[0]
history_item = ''
else:
try:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def test_edit_file_with_spaces(base_app, request, monkeypatch):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'my commands.txt')

run_cmd(base_app, 'edit {}'.format(filename))
run_cmd(base_app, 'edit "{}"'.format(filename))

# We think we have an editor, so should expect a system call
m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename))
Expand Down Expand Up @@ -911,7 +911,10 @@ def test_default_to_shell_unknown(shell_app):
def test_default_to_shell_good(capsys):
app = cmd2.Cmd()
app.default_to_shell = True
line = 'ls'
if sys.platform.startswith('win'):
line = 'dir'
else:
line = 'ls'
statement = app.parser_manager.parsed(line)
retval = app.default(statement)
assert not retval
Expand Down