From 7997891cfcc76f07fd6f28ffd3091ad5f941ed32 Mon Sep 17 00:00:00 2001 From: Lightling Date: Fri, 9 Feb 2024 17:27:51 -0500 Subject: [PATCH] arg parser --- args.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 args.js diff --git a/args.js b/args.js new file mode 100644 index 0000000..e786010 --- /dev/null +++ b/args.js @@ -0,0 +1,26 @@ +let argRegex = new RegExp('(\-\-[a-zA-Z0-9]+)(=)(.*)'); + +const parseArgs = () => { + let _args = { '_': [] }; + [...process.argv.slice(2)].forEach(arg => { + let result = argRegex.exec(arg); + if (!!result) { + _args[result[1].replace('--','')] = result[3]; + } else { + _args['_'].push(arg); + } + }); + + return _args; +} + +export const args = parseArgs(); + +export const getArg = (argName) => { + let arg = args[argName]; + if (!!arg) { + return arg; + } else { + throw(`${argName} was not supplied!`); + } +}