pySim-shell: obey quit command in startup commands+scripts

Startup scripts are executed using the cmd2 provided onecmd_plus_hooks
method. This method can run arbitrary commands, which also includes
the command "run_scrit" that we use to execute startup scripts.

When a script executes a quit command, or when someone issues a quit
command using the --execute-command or the command argument, then
this commands is executed. However a quit command won't actually quit
the process. All it does is to change the return code of
app.onecmd_plus_hooks (see [1]). So we must evaluate the return code
and take care of the quitting ourselves.

[1] https://cmd2.readthedocs.io/en/0.9.15/api/cmd.html#cmd2.cmd2.Cmd.onecmd_plus_hooks

Related: OS#6731
Change-Id: Ic6e9c54cdb6955d65011af3eb5a025eee5da4143
This commit is contained in:
Philipp Maier
2025-02-25 14:47:26 +01:00
parent de91b0dc97
commit c4a6b8b3e7

View File

@@ -1159,14 +1159,18 @@ if __name__ == '__main__':
# Run optional commands
for c in opts.execute_command:
if not startup_errors:
app.onecmd_plus_hooks(c)
stop = app.onecmd_plus_hooks(c)
if stop == True:
sys.exit(0)
else:
print("Errors during startup, refusing to execute command (%s)" % c)
# Run optional command
if opts.command:
if not startup_errors:
app.onecmd_plus_hooks('{} {}'.format(opts.command, ' '.join(opts.command_args)))
stop = app.onecmd_plus_hooks('{} {}'.format(opts.command, ' '.join(opts.command_args)))
if stop == True:
sys.exit(0)
else:
print("Errors during startup, refusing to execute command (%s)" % opts.command)
@@ -1177,7 +1181,9 @@ if __name__ == '__main__':
print("Error: script file (%s) not readable!" % opts.script)
startup_errors = True
else:
app.onecmd_plus_hooks('{} {}'.format('run_script', opts.script), add_to_history = False)
stop = app.onecmd_plus_hooks('{} {}'.format('run_script', opts.script), add_to_history = False)
if stop == True:
sys.exit(0)
else:
print("Errors during startup, refusing to execute script (%s)" % opts.script)