Cheat Sheet速查表
跳转到主要内容

Meow 速查表

本文根据 Devhints / Meow 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2017-10-30。

meow 是为 Node.js 编写命令行应用程序最简单的方式。

常用设置

const cli = require('meow')(`
  Usage: appname [options]

  Options:
        --lang LANG    set the language

  Other options:
    -h, --help         show usage information
    -v, --version      print version info and exit
`, {
  string: ['lang'],
  boolean: ['help', 'version'],
  alias: { h: 'help', v: 'version' }
})

string 以及 boolean 让 meow/minimist 知道哪些标志(flag)需要参数(string),哪些不需要(boolean).

使用结果

cli.flags   // { lang: 'en' }
cli.input   // []

没错,标志会自动成为 camelCased!

较少使用的设置

meow(`...`, {
  // Default values if flags are not specified
  default: { lang: 'en' },

  // allow using -- to stop processing flags
  '--': true,

  // Populate `_` with first non-option
  stopEarly: true,

  // Invoked on unknown param
  unknown: function () { ... }
})

另请参阅 minimist.