Skip to content

Instantly share code, notes, and snippets.

@Jonathan-Dobson
Last active November 23, 2021 15:32
Show Gist options
  • Save Jonathan-Dobson/4511ba57bf11235019da8e6137056f0d to your computer and use it in GitHub Desktop.
Save Jonathan-Dobson/4511ba57bf11235019da8e6137056f0d to your computer and use it in GitHub Desktop.
use flags in nodejs shell script. Converts all flags that have double-dash into keys with the value being the following argument
#!/usr/bin/env node
const args = process.argv.reduce((p, c, i, a) => {
const key = a[i - 1];
const value = /--\w/.test(key) && c;
return value ? { ...p, [key.slice(2)]: value } : p;
}, {});
# example
# ./bin/example.js --port 3001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment