yargs
========
Yargs be a node.js library fer hearties tryin' ter parse optstrings.
With yargs, ye be havin' a map that leads straight to yer treasure! Treasure of course, being a simple option hash.
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![NPM version][npm-image]][npm-url]
[![Windows Tests][windows-image]][windows-url]
[![js-standard-style][standard-image]][standard-url]
[![standard-version][standard-version-image]][standard-version-url]
[![Gitter][gitter-image]][gitter-url]
> Yargs is the official successor to optimist. Please feel free to submit issues and pull requests. If you'd like to contribute and don't know where to start, have a look at [the issue list](https://github.com/yargs/yargs/issues) :)
examples
========
With yargs, the options be just a hash!
-------------------------------------------------------------------
plunder.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!');
} else {
console.log('Retreat from the xupptumblers!');
}
````
***
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!
$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!
![Joe was one optimistic pirate.](https://i.imgur.com/4WFGVJ9.png)
But don't walk the plank just yet! There be more! You can do short options:
-------------------------------------------------
short.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
````
***
$ ./short.js -x 10 -y 21
(10,21)
And booleans, both long, short, and even grouped:
----------------------------------
bool.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
if (argv.s) {
process.stdout.write(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: ');
}
console.log(
(argv.fr ? 'couac' : 'squawk') + (argv.p ? '!' : '')
);
````
***
$ ./bool.js -s
The parrot says: squawk
$ ./bool.js -sp
The parrot says: squawk!
$ ./bool.js -sp --fr
Le perroquet dit: couac!
And non-hyphenated options too! Just use `argv._`!
-------------------------------------------------
nonopt.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
````
***
$ ./nonopt.js -x 6.82 -y 3.35 rum
(6.82,3.35)
[ 'rum' ]
$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho
(0.54,1.12)
[ 'me hearties', 'yo', 'ho' ]
Yargs even counts your booleans!
----------------------------------------------------------------------
count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.count('verbose')
.alias('v', 'verbose')
.argv;
VERBOSE_LEVEL = argv.verbose;
function WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }
function INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }
function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }
WARN("Showing only important stuff");
INFO("Showing semi-important stuff too");
DEBUG("Extra chatty mode");
````
***
$ node count.js
Showing only important stuff
$ node count.js -v
Showing only important stuff
Showing semi-important stuff too
$ node count.js -vv
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode
$ node count.js -v --verbose
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode
Tell users how to use yer options and make demands.
-------------------------------------------------
area.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.usage('Usage: $0 -w [num] -h [num]')
.demandOption(['w','h'])
.argv;
console.log("The area is:", argv.w * argv.h);
````
***
$ ./area.js -w 55 -h 11
The area is: 605
$ node ./area.js -w 4.91 -w 2.51
Usage: area.js -w [num] -h [num]
Options:
-w [required]
-h [required]
Missing required arguments: h
After yer demands have been met, demand more! Ask for non-hyphenated arguments!
-----------------------------------------
demand_count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.demandCommand(2)
.argv;
console.dir(argv);
````
***
$ ./demand_count.js a
Not enough non-option arguments: got 1, need at least 2
$ ./demand_count.js a b
{ _: [ 'a', 'b' ], '$0': 'demand_count.js' }
$ ./demand_count.js a b c
{ _: [ 'a', 'b', 'c' ], '$0': 'demand_count.js' }
EVEN MORE SHIVER ME TIMBERS!
------------------
default_singles.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.default('x', 10)
.default('y', 10)
.argv
;
console.log(argv.x + argv.y);
````
***
$ ./default_singles.js -x 5
15
default_hash.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.default({ x : 10, y : 10 })
.argv
;
console.log(argv.x + argv.y);
````
***
$ ./default_hash.js -y 7
17
And if you really want to get all descriptive about it...
---------------------------------------------------------
boolean_single.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.boolean('v')
.argv
;
console.dir(argv.v);
console.dir(argv._);
````
***
$ ./boolean_single.js -v "me hearties" yo ho
true
[ 'me hearties', 'yo', 'ho' ]
boolean_double.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.boolean(['x','y','z'])
.argv
;
console.dir([ argv.x, argv.y, argv.z ]);
console.dir(argv._);
````
***
$ ./boolean_double.js -x -z one two three
[ true, false, true ]
[ 'one', 'two', 'three' ]
Yargs is here to help you...
---------------------------
Ye can describe parameters fer help messages and set aliases. Yargs figures
out how ter format a handy help string automatically.
line_count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.command('count', 'Count the lines in a file')
.example('$0 count -f foo.js', 'count the lines in the given file')
.alias('f', 'file')
.nargs('f', 1)
.describe('f', 'Load a file')
.demandOption(['f'])
.help('h')
.alias('h', 'help')
.epilog('copyright 2015')
.argv;
var fs = require('fs');
var s = fs.createReadStream(argv.file);
var lines = 0;
s.on('data', function (buf) {
lines += buf.toString().match(/\n/g).length;
});
s.on('end', function () {
console.log(lines);
});
````
***
$ node line_count.js count
Usage: line_count.js <command> [options]
Commands:
count Count the lines in a file
Options:
-f, --file Load a file [required]
-h, --help Show help [boolean]
Examples:
line_count.js count -f foo.js count the lines in the given file
copyright 2015
Missing required arguments: f
$ node line_count.js count --file line_count.js
26
$ node line_count.js count -f line_count.js
26
methods
=======
By itself,
````javascript
require('yargs').argv
````
will use the `process.argv` array to construct the `argv` object.
You can pass in the `process.argv` yourself:
````javascript
require('yargs')([ '-x', '1', '-y', '2' ]).argv
````
or use `.parse()` to do the same thing:
````javascript
require('yargs').parse([ '-x', '1', '-y', '2' ])
````
The rest of these methods below come in just before the terminating `.argv`.
<a name="alias"></a>.alias(key, alias)
------------------
Set key names as equivalent such that updates to a key will propagate to aliases
and vice-versa.
Optionally `.alias()` can take an object that maps keys to aliases.
Each key of this object should be the canonical version of the option, and each
value should be a string or an array of strings.
.argv
-----
Get the arguments as a pl
没有合适的资源?快使用搜索试试~ 我知道了~
ReactNativeApp:1. Native中集成的ReactNative,实现Android与React Native3种...
共15441个文件
js:11075个
json:709个
java:668个
需积分: 16 1 下载量 109 浏览量
2021-02-02
23:06:08
上传
评论
收藏 24.37MB ZIP 举报
温馨提示
Android Native App集成React Native 详情浏览: : React Native与Android通信 详情浏览: : React Native实现热部署,增量热更新 详情浏览: : React Native(最新版本)实现JsBundle预加载,界面秒开优化 详情浏览: : Android布局加载React Native视图交互 详情浏览: :
资源详情
资源评论
资源推荐
收起资源包目录
ReactNativeApp:1. Native中集成的ReactNative,实现Android与React Native3种交互方式,2。使用google-diff-match-patch生成补丁包,实现React Native热部署,增量热更新,3。封装ReactActivity,实现JsBundle预加载,界面秒开优化,4。自定义ReactDegelate,实现Android现有布局添加RN视图 (15441个子文件)
almond.0 2KB
sshpk-conv.1 4KB
jsesc.1 3KB
jsesc.1 3KB
sshpk-sign.1 2KB
sshpk-verify.1 2KB
almond.1 558B
_babelrc 31B
_buckconfig 114B
_flowconfig 1KB
_gitattributes 16B
_gitignore 791B
_watchmanconfig 2B
react-native-0.41.2.aar 2.4MB
address 673B
LICENSE.APACHE2 586B
AUTHORS 235B
AUTHORS 217B
AUTHORS 217B
AUTHORS 217B
AUTHORS 217B
AUTHORS 169B
AUTHORS 163B
AUTHORS 151B
AUTHORS 138B
.babelrc 187B
.babelrc 187B
.babelrc 186B
.babelrc 186B
.babelrc 181B
.babelrc 181B
.babelrc 174B
.babelrc 174B
.babelrc 170B
.babelrc 170B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 122B
.babelrc 45B
babylon 315B
gradlew.bat 2KB
gradlew.bat 2KB
launchPackager.bat 382B
setup_env.bat 301B
range.bnf 629B
iTunes-small.bplist 24KB
iTunes-small.bplist 24KB
binaryData.bplist 7KB
utf16.bplist 1KB
utf16.bplist 1KB
sample1.bplist 605B
sample1.bplist 605B
sample2.bplist 384B
sample2.bplist 384B
uid.bplist 365B
uid.bplist 365B
airplay.bplist 341B
airplay.bplist 341B
LICENSE.BSD 1KB
LICENSE.BSD 1KB
BUCK 5KB
BUCK 4KB
BUCK 4KB
BUCK 4KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 2KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
BUCK 1KB
共 15441 条
- 1
- 2
- 3
- 4
- 5
- 6
- 155
合众丰城
- 粉丝: 21
- 资源: 4651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0