I have a simple node.js backend script and I want to capture command line arguments along with the keys/values from a config.json file and environment variables. The second two I am having no problem with, but I am having nearly inexplicable trouble capturing the command line args.
我有一个简单的node.js后端脚本,我想捕获命令行参数以及config.json文件和环境变量中的键/值。第二个我没有问题,但我在捕获命令行args时遇到了几乎莫名其妙的麻烦。
I can capture the command line arguments this way:
我可以这样捕获命令行参数:
var nconf = require('nconf');
nconf.argv().env().file({file: './config.json'});
var csvFilePath = nconf.argv().get()._[0]; // var csvFilePath = process.argv[2];
var csvType = nconf.argv().get()._[1]; // var csvType = process.argv[3];
these two calls are equivalent to process.argv[index], except the index is changed.
这两个调用等同于process.argv [index],但索引已更改。
There has to be a more straightforward way to capture the command line arguments but even when I debug and look through the variables that nconf yields, I still can't figure it out.
必须有一种更简单的方法来捕获命令行参数,但即使我调试并查看nconf产生的变量,我仍然无法弄明白。
Anyone with nconf experience care to help?
有nconf经验的人可以帮忙吗?
2 个解决方案
#1
9
I believe the best way to do this is like so:
我相信这样做的最好方法是这样的:
//contents of app.js
var nconf = require('nconf').argv();
if you call your program with the follow command line command:
如果使用follow命令行命令调用程序:
node app.js --one foo --two bar
then in your program you can access these command line args like so:
然后在您的程序中,您可以访问这些命令行args,如下所示:
var nconf = require('nconf').argv();
var one = nconf.get('one'); //one = 'foo'
var two = nconf.get('two'); //two = 'bar'
so you need the -- symbol in front of the identifier, then you can access the command line args.
所以你需要在标识符前面的 - 符号,然后你可以访问命令行args。
Frankly, as a message to the nconf module author Charlie Robbins, I think it would better to not mix everything into one big hash.
坦率地说,作为向nconf模块作者Charlie Robbins发送的消息,我认为最好不要将所有内容混合成一个大哈希。
It would be better if you did this instead:
如果你这样做会更好:
var foo = nconf.argv.get('one');
var node_env = nconf.env.get('NODE_ENV');
I think it is more intuitive and less error prone.
我认为它更直观,更不容易出错。
Also, for those starting node with 'npm start':
此外,对于那些带有'npm start'的起始节点:
to my knowledge you need two extra hyphens like so:
据我所知,你需要两个额外的连字符,如下所示:
npm start -- --one foo --two bar
with the extra hyphens/dashes you let Bash know that the args are for your node.js executable, not for the NPM node.js executable
使用额外的连字符/破折号,你让Bash知道args是你的node.js可执行文件,而不是NPM node.js可执行文件
#2
7
Slightly shorter/cleaner:
略短/清洁:
var nconf = require('nconf');
nconf.argv().env().file({file: './config.json'});
var csvFilePath = nconf.get('_')[0]; // var csvFilePath = process.argv[2];
var csvType = nconf.get('_')[1]; // var csvType = process.argv[3];
If you name your parameter (say, --foo=bar
or -f bar
), then you can use .get('foo')
or .get('f')
rather than using the array index.
如果您为参数命名(例如, - foo = bar或-f bar),则可以使用.get('foo')或.get('f')而不是使用数组索引。
#1
9
I believe the best way to do this is like so:
我相信这样做的最好方法是这样的:
//contents of app.js
var nconf = require('nconf').argv();
if you call your program with the follow command line command:
如果使用follow命令行命令调用程序:
node app.js --one foo --two bar
then in your program you can access these command line args like so:
然后在您的程序中,您可以访问这些命令行args,如下所示:
var nconf = require('nconf').argv();
var one = nconf.get('one'); //one = 'foo'
var two = nconf.get('two'); //two = 'bar'
so you need the -- symbol in front of the identifier, then you can access the command line args.
所以你需要在标识符前面的 - 符号,然后你可以访问命令行args。
Frankly, as a message to the nconf module author Charlie Robbins, I think it would better to not mix everything into one big hash.
坦率地说,作为向nconf模块作者Charlie Robbins发送的消息,我认为最好不要将所有内容混合成一个大哈希。
It would be better if you did this instead:
如果你这样做会更好:
var foo = nconf.argv.get('one');
var node_env = nconf.env.get('NODE_ENV');
I think it is more intuitive and less error prone.
我认为它更直观,更不容易出错。
Also, for those starting node with 'npm start':
此外,对于那些带有'npm start'的起始节点:
to my knowledge you need two extra hyphens like so:
据我所知,你需要两个额外的连字符,如下所示:
npm start -- --one foo --two bar
with the extra hyphens/dashes you let Bash know that the args are for your node.js executable, not for the NPM node.js executable
使用额外的连字符/破折号,你让Bash知道args是你的node.js可执行文件,而不是NPM node.js可执行文件
#2
7
Slightly shorter/cleaner:
略短/清洁:
var nconf = require('nconf');
nconf.argv().env().file({file: './config.json'});
var csvFilePath = nconf.get('_')[0]; // var csvFilePath = process.argv[2];
var csvType = nconf.get('_')[1]; // var csvType = process.argv[3];
If you name your parameter (say, --foo=bar
or -f bar
), then you can use .get('foo')
or .get('f')
rather than using the array index.
如果您为参数命名(例如, - foo = bar或-f bar),则可以使用.get('foo')或.get('f')而不是使用数组索引。