Created
March 23, 2020 13:05
-
-
Save jamesseanwright/5ccedb4a2fad74d93352ef4a5fbaa516 to your computer and use it in GitHub Desktop.
A simple Node.js command line args parser
This file contains hidden or 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
| // Invoked with node script.js --db-url=postgres://0.0.0.0:5432/db --commit --item-count=5 | |
| const parseArgs = (args) => | |
| new Map(args.map((arg) => arg.match(/^--([a-z0-9-]+)=?(.*)$/i).slice(1))); | |
| const args = parseArgs(process.argv.slice(2)); | |
| const dbUrl = args.get('db-url'); // 'postgres://0.0.0.0:5432/db' | |
| const shouldCommit = args.has('commit'); // true | |
| const itemCount = Number.parseInt(args.get('item-count'), 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment