Last active
November 23, 2021 15:32
-
-
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
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
#!/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