Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Created March 23, 2020 13:05
Show Gist options
  • Select an option

  • Save jamesseanwright/5ccedb4a2fad74d93352ef4a5fbaa516 to your computer and use it in GitHub Desktop.

Select an option

Save jamesseanwright/5ccedb4a2fad74d93352ef4a5fbaa516 to your computer and use it in GitHub Desktop.
A simple Node.js command line args parser
// 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