Last active
August 15, 2019 13:54
-
-
Save robertkowalski/e4c54cb48b5937579adcd80fde365bb8 to your computer and use it in GitHub Desktop.
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
node test.js --debug 1 --foo=2 --blerg | |
// { debug: '1', foo: '2', blerg: true } |
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
'use strict' | |
function miniparse (argv) { | |
argv = argv || process.argv | |
const args = argv.splice(2).reduce((acc, el, i, src) => { | |
const next = src[i + 1] | |
if (/^--/.test(el) && next && !/^--/.test(next)) { | |
el = el + '=' + next | |
acc.push(el) | |
return acc | |
} | |
if (/^--/.test(el)) { | |
acc.push(el) | |
} | |
return acc | |
}, []).reduce((acc, el) => { | |
const cleaned = el.replace(/^--/, '').split('=') | |
acc[cleaned[0]] = cleaned[1] || true | |
return acc | |
}, {}) | |
return args | |
} | |
console.log(miniparse()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment