node 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.js应用程序中使用命令行参数是本课程的标准操作,尤其是当您像我一样并且使用JavaScript编写任务(而不是bash脚本)时。 Node.js提供了process.argv
但是没有提供key: value
对象,就像您期望的那样:
/*
$ 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:
eh 如果要使用合理的API作为命令行参数,请使用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!
yargs为参数提供了一个key:value
对象,而不是本地的process.argv
混乱。 轻松无忧,只需使用逻辑API访问命令行参数即可。 点头高兴!
node yargs