分类: Node.js

NodeJS Reading Arguments – optimist






Github 地址 : https://github.com/substack/node-optimist

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

for (var i=0; i < argv.times; i++) {
console.log(argv._[0] + ' on loop number ' + (i + 1)); } 
//command line: 
$ node./script.js --time=55 hello 
//output: 
hello on loop number 0 
hello on loop number 1 
hello on loop number 2 
hello on loop number 3 
hello on loop number 4 

argv 后面的作为parameter , –[parameter]

 
var argv = require('optimist').argv; 
if (argv.rif - 5 * argv.xup > 7.138) 
{
    console.log('Buy more riffiwobbles');
}
else {
    console.log('Sell the xupptumblers');
}
//////////

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

$ node ./xup.js --rif 12 --xup 8.1
Sell the xupptumblers
var argv = require('optimist').argv;
console.log('(%d,%d)', argv.x, argv.y);
////////
$ ./short.js -x 10 -y 21
(10,21)




发表评论

Webmentions

  • Node.JS Module 应用 | Ian's Blog

    […] optimist – Reading Arguments […]