Skip to content

Commit

Permalink
feat: support equal-style arguments in command line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Jul 30, 2023
1 parent b1f3026 commit 84093aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
3 changes: 1 addition & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
80 changes: 45 additions & 35 deletions debugger/util/argument_parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const {
ArrayPrototypeShift,
ArrayPrototypeSplice,
ArrayPrototypeIncludes,
StringPrototypeSplit,
RegExpPrototypeExec,
RegExpPrototypeTest,
StringPrototypeIndexOf,
StringPrototypeSlice,
ArrayPrototypeJoin,
} = primordials;

function parseBoolean(value) {
Expand All @@ -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';
Expand Down

0 comments on commit 84093aa

Please sign in to comment.