Parse Node Arguments with named arguments
function parseArgs(args) { | |
let parsed = {}; | |
try { | |
parsed = Object.assign( | |
...args | |
.slice(2) | |
.reduce((l, r) => { | |
if (!Array.isArray(l)) l = [[l, r]]; | |
else if (r.startsWith('--')) l.push([r]); | |
else l[l.length - 1].push(r); | |
return l; | |
}) | |
.map(p => ({ [p[0].substring(2)]: p[1] })) | |
); | |
} catch (e) { | |
console.error('Unknown argument passed', e); | |
} | |
return parsed; | |
} | |
const opts = parseArgs(process.argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Invoked as:
opts will contain:
That's just an example password, if it matches your real password. Change your password.