Skip to content

Commit

Permalink
feat: more flexible argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Jul 28, 2023
1 parent 87dce20 commit 7a497f5
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions debugger/util/argument_parser.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 7a497f5

Please sign in to comment.