Skip to content

Commit ce40bc4

Browse files
committed
Converted edit command to an @options command for the better argument parsing related to spaces and quotes
The recent change of edit to allow spaces means that it can benefit from the better argument parsing cmd2 has for commands implemented using the @options decorator.
1 parent eae9157 commit ce40bc4

2 files changed

Lines changed: 10 additions & 11 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: 1 addition & 1 deletion
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 {!r}'.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))

0 commit comments

Comments
 (0)