angular

yargs-parser

[圖片上傳失敗...(image-dac86d-1515917243151)]
[圖片上傳失敗...(image-7c1481-1515917243151)]
[圖片上傳失敗...(image-c75f3c-1515917243151)]
[圖片上傳失敗...(image-2324d9-1515917243151)]
[圖片上傳失敗...(image-1e4af-1515917243151)]

The mighty option parser used by yargs.

visit the yargs website for more examples, and thorough usage instructions.

<img width="250" src="yargs-logo.png">

Example

npm i yargs-parser --save
var argv = require('yargs-parser')(process.argv.slice(2))
console.log(argv)
node example.js --foo=33 --bar hello
{ _: [], foo: 33, bar: 'hello' }

or parse a string!

var argv = require('./')('--foo=99 --bar=33')
console.log(argv)
{ _: [], foo: 99, bar: 33 }

Convert an array of mixed types before passing to yargs-parser:

var parse = require('yargs-parser')
parse(['-f', 11, '--zoom', 55].join(' '))   // <-- array to string
parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings

API

require('yargs-parser')(args, opts={})

Parses command line arguments returning a simple mapping of keys and values.

expects:

  • args: a string or array of strings representing the options to parse.
  • opts: provide a set of hints indicating how args should be parsed:
    • opts.alias: an object representing the set of aliases for a key: {alias: {foo: ['f']}}.
    • opts.array: indicate that keys should be parsed as an array: {array: ['foo', 'bar']}.
    • opts.boolean: arguments should be parsed as booleans: {boolean: ['x', 'y']}.
    • opts.config: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
    • opts.coerce: provide a custom synchronous function that returns a coerced value from the argument provided
      (or throws an error), e.g. {coerce: {foo: function (arg) {return modifiedArg}}}.
    • opts.count: indicate a key that should be used as a counter, e.g., -vvv = {v: 3}.
    • opts.default: provide default values for keys: {default: {x: 33, y: 'hello world!'}}.
    • opts.envPrefix: environment variables (process.env) with the prefix provided should be parsed.
    • opts.narg: specify that a key requires n arguments: {narg: {x: 2}}.
    • opts.normalize: path.normalize() will be applied to values set to this key.
    • opts.string: keys should be treated as strings (even if they resemble a number -x 33).
    • opts.configuration: provide configuration options to the yargs-parser (see: configuration).
    • opts.number: keys should be treated as numbers.

returns:

  • obj: an object representing the parsed value of args
    • key/value: key value pairs for each argument and their aliases.
    • _: an array representing the positional arguments.

require('yargs-parser').detailed(args, opts={})

Parses a command line string, returning detailed information required by the
yargs engine.

expects:

  • args: a string or array of strings representing options to parse.
  • opts: provide a set of hints indicating how args, inputs are identical to require('yargs-parser')(args, opts={}).

returns:

  • argv: an object representing the parsed value of args
    • key/value: key value pairs for each argument and their aliases.
    • _: an array representing the positional arguments.
  • error: populated with an error object if an exception occurred during parsing.
  • aliases: the inferred list of aliases built by combining lists in opts.alias.
  • newAliases: any new aliases added via camel-case expansion.
  • configuration: the configuration loaded from the yargs stanza in package.json.

<a name="configuration"></a>

Configuration

The yargs-parser applies several automated transformations on the keys provided
in args. These features can be turned on and off using the configuration field
of opts.

var parsed = parser(['--no-dice'], {
  configuration: {
    'boolean-negation': false
  }
})

short option groups

  • default: true.
  • key: short-option-groups.

Should a group of short-options be treated as boolean flags?

node example.js -abc
{ _: [], a: true, b: true, c: true }

if disabled:

node example.js -abc
{ _: [], abc: true }

camel-case expansion

  • default: true.
  • key: camel-case-expansion.

Should hyphenated arguments be expanded into camel-case aliases?

node example.js --foo-bar
{ _: [], 'foo-bar': true, fooBar: true }

if disabled:

node example.js --foo-bar
{ _: [], 'foo-bar': true }

dot-notation

  • default: true
  • key: dot-notation

Should keys that contain . be treated as objects?

node example.js --foo.bar
{ _: [], foo: { bar: true } }

if disabled:

node example.js --foo.bar
{ _: [], "foo.bar": true }

parse numbers

  • default: true
  • key: 'parse-numbers'

Should keys that look like numbers be treated as such?

node example.js --foo=99.3
{ _: [], foo: 99.3 }

if disabled:

node example.js --foo=99.3
{ _: [], foo: "99.3" }

boolean negation

  • default: true
  • key: 'boolean-negation'

Should variables prefixed with --no be treated as negations?

node example.js --no-foo
{ _: [], foo: false }

if disabled:

node example.js --no-foo
{ _: [], "no-foo": true }

Special Thanks

The yargs project evolves from optimist and minimist. It owes its
existence to a lot of James Halliday's hard work. Thanks substack beep boop \o/

License

ISC

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子玄货,更是在濱河造成了極大的恐慌专钉,老刑警劉巖代乃,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耻涛,死亡現(xiàn)場(chǎng)離奇詭異烘绽,居然都是意外死亡颈渊,警方通過(guò)查閱死者的電腦和手機(jī)遂黍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)俊嗽,“玉大人雾家,你說(shuō)我怎么就攤上這事∩芑恚” “怎么了芯咧?”我有些...
    開(kāi)封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)竹揍。 經(jīng)常有香客問(wèn)我唬党,道長(zhǎng),這世上最難降的妖魔是什么鬼佣? 我笑而不...
    開(kāi)封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任驶拱,我火速辦了婚禮,結(jié)果婚禮上晶衷,老公的妹妹穿的比我還像新娘蓝纲。我一直安慰自己,他們只是感情好晌纫,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布税迷。 她就那樣靜靜地躺著,像睡著了一般锹漱。 火紅的嫁衣襯著肌膚如雪箭养。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天哥牍,我揣著相機(jī)與錄音毕泌,去河邊找鬼。 笑死嗅辣,一個(gè)胖子當(dāng)著我的面吹牛撼泛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播澡谭,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼愿题,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起潘酗,我...
    開(kāi)封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤杆兵,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后仔夺,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體拧咳,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年囚灼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了骆膝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡灶体,死狀恐怖阅签,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蝎抽,我是刑警寧澤政钟,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站樟结,受9級(jí)特大地震影響养交,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瓢宦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一碎连、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧驮履,春花似錦鱼辙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至恐似,卻和暖如春杜跷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背矫夷。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工葛闷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人口四。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓孵运,卻偏偏與公主長(zhǎng)得像秦陋,于是被迫代替她去往敵國(guó)和親蔓彩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容