summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-08-21 12:50:22 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-08-21 12:50:22 -0400
commitce40bc4dd8484c2de9eb32671df515b0d567dd4f (patch)
treeafd7b32d575f164c5f24081b7dc79df14a12852d
parenteae9157b711f333bb2895ca2b92019d7463bca8b (diff)
downloadcmd2-git-ce40bc4dd8484c2de9eb32671df515b0d567dd4f.tar.gz
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.
-rwxr-xr-xcmd2.py19
-rw-r--r--tests/test_cmd2.py2
2 files changed, 10 insertions, 11 deletions
diff --git a/cmd2.py b/cmd2.py
index ebbbf46e..1bfab961 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1589,13 +1589,11 @@ Paths or arguments that contain spaces must be enclosed in quotes
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.
@@ -1605,15 +1603,16 @@ If neither is supplied, the most recent command in the history is edited.
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:
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 96bfd22f..11af8657 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -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 {!r}'.format(filename))
# We think we have an editor, so should expect a system call
m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename))