From ac0c9bd7e8fad5f0947ffb63b3bed3e01432cd1c Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Sun, 25 Aug 2024 04:08:42 +0700 Subject: [PATCH] BUGFIX: Missing error handler when calling libarg (#1625) --- src/Terminal/commands/runScript.ts | 14 ++++++++++---- src/Terminal/getTabCompletionPossibilities.ts | 19 +++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Terminal/commands/runScript.ts b/src/Terminal/commands/runScript.ts index d34fcdbb6..2d7310daa 100644 --- a/src/Terminal/commands/runScript.ts +++ b/src/Terminal/commands/runScript.ts @@ -18,10 +18,16 @@ export function runScript(path: ScriptFilePath, commandArgs: (string | number | if (!script) return Terminal.error(`Script ${path} does not exist on this server.`); const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; - const flags = libarg(runArgs, { - permissive: true, - argv: commandArgs, - }); + let flags; + try { + flags = libarg(runArgs, { + permissive: true, + argv: commandArgs, + }); + } catch (error) { + Terminal.error(`Invalid arguments. ${String(error)}.`); + return; + } const tailFlag = flags["--tail"] === true; const numThreads = parseFloat(flags["-t"] ?? 1); const ramOverride = flags["--ram-override"] != null ? roundToTwo(parseFloat(flags["--ram-override"])) : null; diff --git a/src/Terminal/getTabCompletionPossibilities.ts b/src/Terminal/getTabCompletionPossibilities.ts index 8cf8e44a1..1d61f6a98 100644 --- a/src/Terminal/getTabCompletionPossibilities.ts +++ b/src/Terminal/getTabCompletionPossibilities.ts @@ -276,10 +276,21 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi if (!loadedModule || !loadedModule.autocomplete) return; // Doesn't have an autocomplete function. const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; - const flags = libarg(runArgs, { - permissive: true, - argv: command.slice(2), - }); + let flags = { + _: [], + }; + try { + flags = libarg(runArgs, { + permissive: true, + argv: command.slice(2), + }); + } catch (error) { + /** + * This error can only happen when the player specifies "-t" or "--ram-override", then presses [Tab] without + * giving a number. We don't need to show an error here. + */ + console.warn(error); + } const flagFunc = Flags(flags._); const autocompleteData: AutocompleteData = { servers: GetAllServers()