From 7a497f56b998591726b8504648c2f3cef4faeaf9 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Fri, 28 Jul 2023 15:23:08 -0600 Subject: [PATCH] feat: more flexible argument parser --- debugger/util/argument_parser.js | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 debugger/util/argument_parser.js diff --git a/debugger/util/argument_parser.js b/debugger/util/argument_parser.js new file mode 100644 index 0000000..e78ea6e --- /dev/null +++ b/debugger/util/argument_parser.js @@ -0,0 +1,61 @@ +const { + ArrayPrototypeSplice, + StringPrototypeSplit +} = primordials; + +// Helper function to parse a boolean +function parseBoolean(value) { + return value === 'true' || value === '1' || value === 'yes'; +} + +function parseArguments(argv) { + // Default options + let options = { + port: undefined, + pauseOnExceptionState: undefined, + inspectResumeOnStart: undefined + } + + // Parsing process.env.NODE_INSPECT_OPTIONS + if (process.env.NODE_INSPECT_OPTIONS) { + const envOptions = StringPrototypeSplit(process.env.NODE_INSPECT_OPTIONS, ' '); + for (let i = 0; i < envOptions.length; i++) { + switch (envOptions[i]) { + case '--port': + options.port = envOptions[++i]; + break; + case '--pause-on-exception-state': + options.pauseOnExceptionState = envOptions[++i]; + break; + case '--inspect-resume-on-start': + options.inspectResumeOnStart = parseBoolean(envOptions[++i]); + break; + } + } + } + + // Parsing argv + for (let i = 0; i < argv.length;) { + switch (argv[i]) { + case '--port': + options.port = argv[i+1]; + ArrayPrototypeSplice(argv, i, 2); + break; + case '--pause-on-exception-state': + options.pauseOnExceptionState = argv[i+1]; + ArrayPrototypeSplice(argv, i, 2); + break; + case '--inspect-resume-on-start': + options.inspectResumeOnStart = parseBoolean(argv[i+1]); + ArrayPrototypeSplice(argv, i, 2); + break; + default: + i++; + break; + } + } + + return options; +} + +module.exports = { parseArguments };