Created
August 7, 2020 21:36
-
-
Save mekwall/a43bcdf7416f088402a1aab8bcfd78b2 to your computer and use it in GitHub Desktop.
A dead simple NodeJS argument parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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