Last active
August 29, 2015 14:17
-
-
Save shamasis/9c010918ec19d239cb31 to your computer and use it in GitHub Desktop.
Quick parsing of CLI arguments
This file contains 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
/** | |
* @example | |
* var args = require('./quickargs.js')(process.argv); | |
*/ | |
module.exports = function (argv) { | |
var args = {}, // object to store all key-value argument extraction | |
lastarg; // args are split by space, so we keep a track of last key detected | |
argv && argv.slice && argv.slice(2).forEach(function (item) { | |
lastarg = /^-/.test(item) ? item.slice(1) : (lastarg && (args[lastarg] = item), undefined); | |
}); | |
return args; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment