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
};
*/
yargs provides a key:value
object for arguments instead of the native process.argv
mess. No hassle, no fuss, just access to command line arguments with a logical API. Happy noding!
Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...
Kids these days, I tell ya. All they care about is the technology. The video games. The bottled water. Oh, and the texting, always the texting. Back in my day, all we had was...OK, I had all of these things too. But I still don't get...
I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with jQuery JavaScript.
The PHP - Content & Header
The following snippet goes at the...
My favorite web technology is quickly becoming the WebSocket API. WebSocket provides a welcomed alternative to the AJAX technologies we've been making use of over the past few years. This new API provides a method to push messages from client to server efficiently...
[pirateAccent]Yaaaarrrrrg!![/pirateAccent]