Node minimist

原文地址

parse argument options
解析參數(shù)選項(xiàng)
This module is the guts of optimist's argument parser without all the fanciful decoration.

Paste_Image.png

example

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

methods

var parseArgs = require('minimist')
var argv = parseArgs(args, opts={})

Return an argument object argv populated with the array arguments from args.
argv._ contains all the arguments that didn't have an option associated with them.
Numeric-looking arguments will be returned as numbers unless opts.string or opts.boolean is set for that argument name.
Any arguments after '--' will not be parsed and will end up in argv._.
options can be:

  • opts.string - a string or array of strings argument names to always treat as strings
  • opts.boolean - a boolean, string or array of strings to always treat as booleans. if true
    will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar)
  • opts.alias - an object mapping string names to strings or arrays of string argument names to use as aliases
  • opts.default - an object mapping string argument names to default values
  • opts.stopEarly - when true, populate argv._ with everything after the first non-option
  • opts['--'] - when true, populate argv._ with everything before the -- and argv['--']
    with everything after the --. Here's an example:
  • opts.unknown - a function which is invoked with a command line parameter not defined in the opts configuration object. If the function returns false, the unknown option is not added to argv.

require('./')('one two three -- four five --six'.split(' '), { '--': true })
{ _: [ 'one', 'two', 'three' ],
'--': [ 'four', 'five', '--six' ] }

Note that with opts['--'] set, parsing for arguments still stops after the --.

install

With npm do:

npm install minimist --save-dev

license

MIT

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末禾锤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子宴抚,更是在濱河造成了極大的恐慌,老刑警劉巖凝赛,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡首繁,警方通過查閱死者的電腦和手機(jī)坞琴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門哨查,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剧辐,你說我怎么就攤上這事寒亥∮矢” “怎么了?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵溉奕,是天一觀的道長褂傀。 經(jīng)常有香客問我,道長加勤,這世上最難降的妖魔是什么仙辟? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮鳄梅,結(jié)果婚禮上欺嗤,老公的妹妹穿的比我還像新娘。我一直安慰自己卫枝,他們只是感情好煎饼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著校赤,像睡著了一般吆玖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上马篮,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天沾乘,我揣著相機(jī)與錄音,去河邊找鬼浑测。 笑死翅阵,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的迁央。 我是一名探鬼主播掷匠,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼岖圈!你這毒婦竟也來了讹语?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤蜂科,失蹤者是張志新(化名)和其女友劉穎顽决,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體导匣,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡才菠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了贡定。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赋访。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出进每,到底是詐尸還是另有隱情,我是刑警寧澤命斧,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布田晚,位于F島的核電站,受9級特大地震影響国葬,放射性物質(zhì)發(fā)生泄漏贤徒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一汇四、第九天 我趴在偏房一處隱蔽的房頂上張望接奈。 院中可真熱鬧,春花似錦通孽、人聲如沸序宦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽互捌。三九已至,卻和暖如春行剂,著一層夾襖步出監(jiān)牢的瞬間秕噪,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工厚宰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腌巾,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓铲觉,卻偏偏與公主長得像澈蝙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子撵幽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

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