Skip to content

Commit 3a842ab

Browse files
authored
Merge pull request #215 from python-cmd2/edit_options
edit command now uses options decorator
2 parents eae9157 + d0acb8d commit 3a842ab

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

cmd2.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,13 +1589,11 @@ def _last_matching(self, arg):
15891589
except IndexError:
15901590
return None
15911591

1592-
def do_edit(self, arg):
1593-
"""Edit a file or command in a text editor.
1594-
1595-
Usage: edit [N]|[file_path]
1596-
1592+
@options([], arg_desc="""[N]|[file_path]
15971593
* N - Number of command (from history), or `*` for all commands in history (default: last command)
1598-
* file_path - path to a file to open in editor
1594+
* file_path - path to a file to open in editor""")
1595+
def do_edit(self, arg, opts=None):
1596+
"""Edit a file or command in a text editor.
15991597
16001598
The editor used is determined by the ``editor`` settable parameter.
16011599
"set editor (program-name)" to change or set the EDITOR environment variable.
@@ -1605,15 +1603,16 @@ def do_edit(self, arg):
16051603
16061604
Edited commands are always run after the editor is closed.
16071605
1608-
Edited files are run on close if the ``autorun_on_edit`` settable parameter is True."""
1606+
Edited files are run on close if the ``autorun_on_edit`` settable parameter is True.
1607+
"""
16091608
if not self.editor:
16101609
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
16111610
filename = None
1612-
if arg:
1611+
if arg and arg[0]:
16131612
try:
1614-
history_item = self._last_matching(int(arg))
1613+
history_item = self._last_matching(int(arg[0]))
16151614
except ValueError:
1616-
filename = arg
1615+
filename = arg[0]
16171616
history_item = ''
16181617
else:
16191618
try:

tests/test_cmd2.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def test_edit_file_with_spaces(base_app, request, monkeypatch):
751751
test_dir = os.path.dirname(request.module.__file__)
752752
filename = os.path.join(test_dir, 'my commands.txt')
753753

754-
run_cmd(base_app, 'edit {}'.format(filename))
754+
run_cmd(base_app, 'edit "{}"'.format(filename))
755755

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

0 commit comments

Comments
 (0)