Skip to content

Commit efe5310

Browse files
committed
Renamed load to run_script and _relative_load to _relative_run_script
1 parent b375999 commit efe5310

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

cmd2/cmd2.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
were using the standard library's cmd, while enjoying the extra features.
77
88
Searchable command history (commands: "history")
9-
Load commands from file, save to file, edit commands in file
9+
Run commands from file, save to file, edit commands in file
1010
Multi-line commands
11-
Special-character shortcut commands (beyond cmd's "@" and "!")
11+
Special-character shortcut commands (beyond cmd's "?" and "!")
1212
Settable environment parameters
1313
Parsing commands with `argparse` argument parsers (flags)
1414
Redirection to file or paste buffer (clipboard) with > or >>
@@ -327,7 +327,7 @@ class Cmd(cmd.Cmd):
327327
328328
Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes.
329329
"""
330-
DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
330+
DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'run_script', '@@': '_relative_run_script'}
331331
DEFAULT_EDITOR = utils.find_editor()
332332

333333
def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
@@ -343,7 +343,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
343343
:param stdout: (optional) alternate output file object, if not specified, sys.stdout is used
344344
:param persistent_history_file: (optional) file path to load a persistent cmd2 command history from
345345
:param persistent_history_length: (optional) max number of history items to write to the persistent history file
346-
:param startup_script: (optional) file path to a a script to load and execute at startup
346+
:param startup_script: (optional) file path to a script to execute at startup
347347
:param use_ipython: (optional) should the "ipy" command be included for an embedded IPython shell
348348
:param allow_cli_args: (optional) if True, then cmd2 will process command line arguments as either
349349
commands to be run or, if -t is specified, transcript files to run.
@@ -398,7 +398,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
398398
'timing': 'Report execution times'}
399399

400400
# Commands to exclude from the help menu and tab completion
401-
self.hidden_commands = ['eof', '_relative_load']
401+
self.hidden_commands = ['eof', '_relative_run_script']
402402

403403
# Commands to exclude from the history command
404404
# initialize history
@@ -429,7 +429,8 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
429429
# Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
430430
self._last_result = None
431431

432-
# Used load command to store the current script dir as a LIFO queue to support _relative_load command
432+
# Used by run_script command to store the current script dir as
433+
# a LIFO queue to support _relative_run_script command
433434
self._script_dir = []
434435

435436
# Context manager used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt
@@ -460,11 +461,11 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
460461
# Commands that will run at the beginning of the command loop
461462
self._startup_commands = []
462463

463-
# If a startup script is provided, then add it in the queue to load
464+
# If a startup script is provided, then execute it in the startup commands
464465
if startup_script is not None:
465466
startup_script = os.path.abspath(os.path.expanduser(startup_script))
466467
if os.path.exists(startup_script) and os.path.getsize(startup_script) > 0:
467-
self._startup_commands.append("load '{}'".format(startup_script))
468+
self._startup_commands.append("run_script '{}'".format(startup_script))
468469

469470
# Transcript files to run instead of interactive command loop
470471
self._transcript_files = None
@@ -3412,7 +3413,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
34123413
fobj.write('{}\n'.format(command.raw))
34133414
try:
34143415
self.do_edit(fname)
3415-
return self.do_load(fname)
3416+
return self.do_run_script(fname)
34163417
finally:
34173418
os.remove(fname)
34183419
elif args.output_file:
@@ -3623,27 +3624,33 @@ def _current_script_dir(self) -> Optional[str]:
36233624
else:
36243625
return None
36253626

3626-
load_description = ("Run commands in script file that is encoded as either ASCII or UTF-8 text\n"
3627-
"\n"
3628-
"Script should contain one command per line, just like the command would be\n"
3629-
"typed in the console.\n"
3630-
"\n"
3631-
"If the -r/--record_transcript flag is used, this command instead records\n"
3632-
"the output of the script commands to a transcript for testing purposes.\n"
3633-
)
3627+
run_script_description = ("Run commands in script file that is encoded as either ASCII or UTF-8 text\n"
3628+
"\n"
3629+
"Script should contain one command per line, just like the command would be\n"
3630+
"typed in the console.\n"
3631+
"\n"
3632+
"If the -r/--record_transcript flag is used, this command instead records\n"
3633+
"the output of the script commands to a transcript for testing purposes.\n"
3634+
)
36343635

3635-
load_parser = ACArgumentParser(description=load_description)
3636-
setattr(load_parser.add_argument('-t', '--transcript', help='record the output of the script as a transcript file'),
3636+
run_script_parser = ACArgumentParser(description=run_script_description)
3637+
setattr(run_script_parser.add_argument('-t', '--transcript',
3638+
help='record the output of the script as a transcript file'),
36373639
ACTION_ARG_CHOICES, ('path_complete',))
3638-
setattr(load_parser.add_argument('script_path', help="path to the script file"),
3640+
setattr(run_script_parser.add_argument('script_path', help="path to the script file"),
36393641
ACTION_ARG_CHOICES, ('path_complete',))
36403642

3641-
@with_argparser(load_parser)
3642-
def do_load(self, args: argparse.Namespace) -> Optional[bool]:
3643+
@with_argparser(run_script_parser)
3644+
def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
36433645
"""
36443646
Run commands in script file that is encoded as either ASCII or UTF-8 text
36453647
:return: True if running of commands should stop
36463648
"""
3649+
if args.__statement__.command == "load":
3650+
self.perror("load has been renamed and will be removed in the next release,"
3651+
"please use run_script instead\n",
3652+
traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
3653+
36473654
expanded_path = os.path.abspath(os.path.expanduser(args.script_path))
36483655

36493656
# Make sure the path exists and we can access it
@@ -3690,27 +3697,35 @@ def do_load(self, args: argparse.Namespace) -> Optional[bool]:
36903697
if orig_script_dir_count != len(self._script_dir):
36913698
self._script_dir.pop()
36923699

3693-
relative_load_description = load_description
3694-
relative_load_description += ("\n\n"
3695-
"If this is called from within an already-running script, the filename will be\n"
3696-
"interpreted relative to the already-running script's directory.")
3700+
# load has been deprecated
3701+
do_load = do_run_script
3702+
3703+
relative_run_script_description = run_script_description
3704+
relative_run_script_description += (
3705+
"\n\n"
3706+
"If this is called from within an already-running script, the filename will be\n"
3707+
"interpreted relative to the already-running script's directory.")
36973708

3698-
relative_load_epilog = ("Notes:\n"
3699-
" This command is intended to only be used within text file scripts.")
3709+
relative_run_script_epilog = ("Notes:\n"
3710+
" This command is intended to only be used within text file scripts.")
37003711

3701-
relative_load_parser = ACArgumentParser(description=relative_load_description, epilog=relative_load_epilog)
3702-
relative_load_parser.add_argument('file_path', help='a file path pointing to a script')
3712+
relative_run_script_parser = ACArgumentParser(description=relative_run_script_description,
3713+
epilog=relative_run_script_epilog)
3714+
relative_run_script_parser.add_argument('file_path', help='a file path pointing to a script')
37033715

3704-
@with_argparser(relative_load_parser)
3705-
def do__relative_load(self, args: argparse.Namespace) -> Optional[bool]:
3716+
@with_argparser(relative_run_script_parser)
3717+
def do__relative_run_script(self, args: argparse.Namespace) -> Optional[bool]:
37063718
"""
37073719
Run commands in script file that is encoded as either ASCII or UTF-8 text
37083720
:return: True if running of commands should stop
37093721
"""
37103722
file_path = args.file_path
37113723
# NOTE: Relative path is an absolute path, it is just relative to the current script directory
37123724
relative_path = os.path.join(self._current_script_dir or '', file_path)
3713-
return self.do_load(relative_path)
3725+
return self.do_run_script(relative_path)
3726+
3727+
# _relative_load has been deprecated
3728+
do__relative_load = do__relative_run_script
37143729

37153730
def run_transcript_tests(self, transcript_paths: List[str]) -> None:
37163731
"""Runs transcript tests for provided file(s).

0 commit comments

Comments
 (0)