代码拉取完成,页面将自动刷新
Optimist是一个node.js库,为讨厌选项解析的人提供选项解析。更具体地说,这个模块是为那些喜欢在程序使用--bells
和
-whistlz
,但认为optstring是浪费时间的人。
对于optimist来说,选项解析不必那么糟糕。
xup.js:
#!/usr/bin/env node
var argv = require('optimist').argv;
if (argv.rif - 5 * argv.xup > 7.138) {
console.log('Buy more riffiwobbles');
}
else {
console.log('Sell the xupptumblers');
}
$ ./xup.js --rif=55 --xup=9.52
Buy more riffiwobbles
$ ./xup.js --rif 12 --xup 8.1
Sell the xupptumblers
short.js:
#!/usr/bin/env node
var argv = require('optimist').argv;
console.log('(%d,%d)', argv.x, argv.y);
$ ./short.js -x 10 -y 21
(10,21)
bool.js:
#!/usr/bin/env node
var sys = require('sys');
var argv = require('optimist').argv;
if (argv.s) {
sys.print(argv.fr
? 'Le chat dit: '
: 'The cow says: '
);
}
console.log(
(argv.fr ? 'miaou' : 'meow') + (argv.p ? '.' : '')
);
$ ./bool.js -s
The cat says: meow
$ ./bool.js -sp
The cat says: meow.
$ ./bool.js -sp --fr
Le chat dit: miaou.
nonopt.js:
#!/usr/bin/env node
var argv = require('optimist').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
$ ./nonopt.js -x 6.82 -y 3.35 moo
(6.82,3.35)
[ 'moo' ]
$ ./nonopt.js foo -x 0.54 bar -y 1.12 baz
(0.54,1.12)
[ 'foo', 'bar', 'baz' ]
divide.js: #!/usr/bin/env node var argv = require('optimist') .usage('Usage: $0 -x [num] -y [num]') .demand(['x','y']) .argv;
console.log(argv.x / argv.y);
$ ./divide.js -x 55 -y 11
5
$ ./divide.js -x 4.91 -z 2.51
Usage: ./divide.js -x [num] -y [num]
Missing arguments: y
default_singles.js:
#!/usr/bin/env node
var argv = require('optimist')
.default('x', 10)
.default('y', 10)
.argv
;
console.log(argv.x + argv.y);
$ ./default_singles.js -x 5
15
default_hash.js:
#!/usr/bin/env node
var argv = require('optimist')
.default({ x : 10, y : 10 })
.argv
;
console.log(argv.x + argv.y);
$ ./default_hash.js -y 7
17
每个看起来像数字的参数(' !isNaN(number (arg)) ')被转换为一个。 这样你就可以' net.createConnection(argv.port) ',你可以用' + '从' argv '中添加数字,而不需要进行平均连接, 这是非常令人沮丧的。
用 npm, 只做:
npm install optimist
或者 在github上克隆这个项目
git clone http://github.com/substack/node-optimist.git
用expresso做测试, just do:
expresso
这个模块受到Perl的Getopt::Casual的启发。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。