Skip to content

Instantly share code, notes, and snippets.

@mekwall
Created August 7, 2020 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekwall/a43bcdf7416f088402a1aab8bcfd78b2 to your computer and use it in GitHub Desktop.
Save mekwall/a43bcdf7416f088402a1aab8bcfd78b2 to your computer and use it in GitHub Desktop.
A dead simple NodeJS argument parser
/*
parseArgs works for the following:
-f bar
--foo bar
-f=bar
--f=bar
*/
const parseArgs = () => {
const args = {};
const rxp = /-{1,2}(?<flag>[\w\n]+)[= ]{1}(?<value>[^\s]+)/gi;
const argv = process.argv.slice(2).join(" ");
let matches;
while ((matches = rxp.exec(argv))) {
const { flag, value } = matches.groups;
args[flag] = value;
}
return args;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment