Node.Js中的optimist模块有什么用

Kev*_*vin -1 javascript command-line-arguments node.js command-line-parsing

https://github.com/substack/node-optimist

Optimist是一个node.js库,用于解析那些讨厌选项解析的人的选项.更具体地说,这个模块适用于那些喜欢所有程序使用--bells和-whistlz的人,但认为optstrings是浪费时间.

var argv = require('optimist').argv;

if (argv.rif - 5 * argv.xup > 7.138) {
  console.log('Buy more riffiwobbles');
}
else {
  console.log('Sell the xupptumblers');
}
Run Code Online (Sandbox Code Playgroud)
  1. 在这段代码中,是rifxup两个参数.
  2. 是否将命令行参数传递给脚本,而不是在脚本本身中执行此操作.
  3. 这个模块的主要目的是什么,用其他一些语言构建的任何实时示例都会有所帮助.

Thi*_*ter 5

访问命令行参数是一种快速而又脏的方法,而无需在实际使用它们之前在代码中定义它们.示例的第二部分(您在问题中省略)实际上回答了您的第一个问题:

$ ./xup.js --rif=55 --xup=9.52
Buy more riffiwobbles

$ ./xup.js --rif 12 --xup 8.1
Sell the xupptumblers
Run Code Online (Sandbox Code Playgroud)

因此,这些参数在结束了argv.rifargv.xup.