Created
September 17, 2018 13:29
-
-
Save PofMagicfingers/c45fc1dff94c6491205bf2b972fd2d52 to your computer and use it in GitHub Desktop.
Really simple and naive nodejs args parsing
This file contains hidden or 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
const parseArgs = (args, parsers) => | |
Object.assign( | |
{}, | |
...parsers.map(parser => { | |
const parsed = Object.assign( | |
{}, | |
...args.map(arg => { | |
const parsed = arg.match(parser); | |
return (parsed && parsed.groups) || {}; | |
}) | |
); | |
return parsed || {}; | |
}) | |
); | |
const parsedArgs = parseArgs(process.argv, [ | |
/--host=(?<host>[^\s]+)/, | |
/--port=(?<port>[0-9]+)/ | |
]); | |
const host = parsedArgs.host || "localhost"; | |
const port = parsedArgs.port || "port"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment