Skip to content

Instantly share code, notes, and snippets.

@robertkowalski
Last active August 15, 2019 13:54
Show Gist options
  • Save robertkowalski/e4c54cb48b5937579adcd80fde365bb8 to your computer and use it in GitHub Desktop.
Save robertkowalski/e4c54cb48b5937579adcd80fde365bb8 to your computer and use it in GitHub Desktop.
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