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!
![5 Awesome New Mozilla Technologies You’ve Never Heard Of]()
My trip to Mozilla Summit 2013 was incredible. I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out. MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...
![From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!]()
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
![AJAX For Evil: Spyjax with jQuery]()
Last year I wrote a popular post titled AJAX For Evil: Spyjax when I described a technique called "Spyjax":
Spyjax, as I know it, is taking information from the user's computer for your own use — specifically their browsing habits. By using CSS and JavaScript, I...
![MooTools FontChecker Plugin]()
There's a very interesting piece of code on Google Code called FontAvailable which does a jQuery-based JavaScript check on a string to check whether or not your system has a specific font based upon its output width. I've ported this functionality to MooTools.
The MooTools...
[pirateAccent]Yaaaarrrrrg!![/pirateAccent]