Skip to content

Instantly share code, notes, and snippets.

@robertkowalski

robertkowalski/1.txt

Last active Aug 15, 2019
Embed
What would you like to do?
node test.js --debug 1 --foo=2 --blerg
// { debug: '1', foo: '2', blerg: true }
'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
You can’t perform that action at this time.