summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py29
1 files changed, 16 insertions, 13 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 6a600107..1af5e932 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -433,7 +433,7 @@ class Cmd(cmd.Cmd):
self._initialize_history(persistent_history_file)
# Commands to exclude from the history command
- self.exclude_from_history = '''history edit eof'''.split()
+ self.exclude_from_history = ['eof', 'history']
# Dictionary of macro names and their values
self.macros = dict()
@@ -3520,14 +3520,9 @@ class Cmd(cmd.Cmd):
else:
fobj.write('{}\n'.format(command.raw))
try:
- # Handle potential edge case where the temp file needs to be quoted on the command line
- quoted_fname = utils.quote_string(fname)
-
- # noinspection PyTypeChecker
- self.do_edit(quoted_fname)
-
+ self._run_editor(fname)
# noinspection PyTypeChecker
- self.do_run_script(quoted_fname)
+ self.do_run_script(utils.quote_string(fname))
finally:
os.remove(fname)
elif args.output_file:
@@ -3719,7 +3714,7 @@ class Cmd(cmd.Cmd):
msg = '{} {} saved to transcript file {!r}'
self.pfeedback(msg.format(commands_run, plural, transcript_file))
- edit_description = ("Edit a file in a text editor\n"
+ edit_description = ("Run a text editor and optionally open a file with it\n"
"\n"
"The editor used is determined by a settable parameter. To set it:\n"
"\n"
@@ -3727,17 +3722,25 @@ class Cmd(cmd.Cmd):
edit_parser = Cmd2ArgumentParser(description=edit_description)
edit_parser.add_argument('file_path', nargs=argparse.OPTIONAL,
- help="path to a file to open in editor", completer_method=path_complete)
+ help="optional path to a file to open in editor", completer_method=path_complete)
@with_argparser(edit_parser)
def do_edit(self, args: argparse.Namespace) -> None:
- """Edit a file in a text editor"""
+ """Run a text editor and optionally open a file with it"""
+ self._run_editor(args.file_path)
+
+ def _run_editor(self, file_path: Optional[str]) -> None:
+ """
+ Run a text editor and optionally open a file with it
+ :param file_path: optional path of the file to edit
+ :raises EnvironmentError if self.editor is not set
+ """
if not self.editor:
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
command = utils.quote_string(os.path.expanduser(self.editor))
- if args.file_path:
- command += " " + utils.quote_string(os.path.expanduser(args.file_path))
+ if file_path:
+ command += " " + utils.quote_string(os.path.expanduser(file_path))
# noinspection PyTypeChecker
self.do_shell(command)