1 Star 0 Fork 0

wutubin / node-optimist

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Optimist

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.

还有非连字符选项!只要用' argv._ ' !

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' ]

另外,Optimist自带.usage()和.demand()!

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的启发。

Copyright 2010 James Halliday (mail@substack.net) This project is free software released under the MIT/X11 license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/wutubin/node-optimist.git
git@gitee.com:wutubin/node-optimist.git
wutubin
node-optimist
node-optimist
master

搜索帮助