From 84093aacd44e375cb00f24d5692b8f4f11a1b206 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Sun, 30 Jul 2023 06:11:08 -0600 Subject: [PATCH] feat: support equal-style arguments in command line parser --- bin/run.js | 3 +- debugger/util/argument_parser.js | 80 ++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/bin/run.js b/bin/run.js index d7dd393..3698482 100755 --- a/bin/run.js +++ b/bin/run.js @@ -1,7 +1,6 @@ #!/usr/bin/env node --expose-internals -r internal/test/binding -// process.env.NODE_INSPECT_RESUME_ON_START ||= 1; -// process.env.NODE_INSPECT_PAUSE_ON_EXCEPTION_STATE ||= 'uncaught'; +process.env.NODE_INSPECT_OPTIONS = '--inspect-resume-on-start=true' // hijack `require` to load debugger module from this package const Mod = require('module'); diff --git a/debugger/util/argument_parser.js b/debugger/util/argument_parser.js index 0512d0e..dc11524 100644 --- a/debugger/util/argument_parser.js +++ b/debugger/util/argument_parser.js @@ -1,9 +1,12 @@ const { ArrayPrototypeShift, - ArrayPrototypeSplice, ArrayPrototypeIncludes, StringPrototypeSplit, RegExpPrototypeExec, + RegExpPrototypeTest, + StringPrototypeIndexOf, + StringPrototypeSlice, + ArrayPrototypeJoin, } = primordials; function parseBoolean(value) { @@ -18,52 +21,59 @@ function validatePauseOnExceptionState(value) { return value; } -function parseArguments(argv) { - const legacyArguments = processLegacyArgs(argv) +function splitOption(option) { + const index = StringPrototypeIndexOf(option, '='); + return [StringPrototypeSlice(option, 0, index), StringPrototypeSlice(option, index + 1)]; +} - let options = { - pauseOnExceptionState: undefined, - inspectResumeOnStart: undefined +function handleArg(options, arg, nextArg) { + let key, value; + if (RegExpPrototypeTest(/=.+/, arg)) { + [key, value] = splitOption(arg); + } else { + key = arg; + value = nextArg; } - // `NODE_INSPECT_OPTIONS` is parsed first and can be overwritten by command line arguments - - 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 '--pause-on-exception-state': - options.pauseOnExceptionState = validatePauseOnExceptionState(envOptions[++i]); - break; - case '--inspect-resume-on-start': - options.inspectResumeOnStart = parseBoolean(envOptions[++i]); - break; - } - } + switch (key) { + case '--pause-on-exception-state': + options.pauseOnExceptionState = validatePauseOnExceptionState(value); + break; + case '--inspect-resume-on-start': + options.inspectResumeOnStart = parseBoolean(value); + break; } + return options; +} - for (let i = 0; i < argv.length;) { - switch (argv[i]) { - case '--pause-on-exception-state': - options.pauseOnExceptionState = validatePauseOnExceptionState(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; +function parseOptions(source, options) { + if (source) { + const optionsArr = StringPrototypeSplit(source, ' '); + for (let i = 0; i < optionsArr.length; i++) { + options = handleArg(options, optionsArr[i], optionsArr[i+1]); } } - - return {...options, ...legacyArguments}; + return options; } // the legacy `node inspect` options assumed the first argument was the target // to avoid breaking existing scripts, we maintain this behavior +function parseArguments(argv) { + const legacyArguments = processLegacyArgs(argv) + + let options = { + pauseOnExceptionState: undefined, + inspectResumeOnStart: undefined + } + + // arguments passed over the CLI take precedence over environment variables + options = parseOptions(process.env.NODE_INSPECT_OPTIONS, options); + options = parseOptions(ArrayPrototypeJoin(argv, ' '), options); + + return {...options, ...legacyArguments}; +} + function processLegacyArgs(args) { const target = ArrayPrototypeShift(args); let host = '127.0.0.1';