Get Node.js Command Line Arguments with yargs

Using command line arguments within Node.js apps is par for the course, especially when you're like me and you use JavaScript to code tasks (instead of bash scripts). Node.js provides process.argv but that doesn't provide a key: value object like you'd expect:

/* $ node myscript.js --key1=value1 --key2=value2 [ 'node', '/path/to/myscript.js', '--key1=value1', '--key2=value2' ] */

Bleh. If you want to work with a sane API for command line arguments, use yargs:

// Get the yargs resource var yargs = require('yargs').argv; // Check for arguments if(yargs.someKey === expectedValue) { // Do whatever } /* yargs = { key1: value1 key2: value2 }; */