/meow-zh

🇨🇳翻译: <meow> cli 命令行帮助库 ❤️ 校对 ✅

meow explain translate-svg

「 CLI 命令行 帮助 库 」

中文 | english


校对 ✅

翻译的原文 与日期 最新更新 更多
commit ⏰ 2018 5.22 last 中文翻译

贡献

欢迎 👏 勘误/校对/更新贡献 😊 具体贡献请看

生活

If help, buy me coffee —— 营养跟不上了,给我来瓶营养快线吧! 💰


meow Build Status

CLI 应用 帮助库

特征

  • 解析命令参数
  • 将参数转换为骆驼风格
  • 使用--no-,能反转参数
  • 输出版本--version
  • 输出描述和提供的帮助文本--help
  • 让未经处理的失败的Promise大声说出而不是默认的静默失败
  • 将进程标题设置为 package.json 中定义的二进制名称

目录

安装

$ npm install meow

用法

$ ./foo-app.js unicorns --rainbow
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const foo = require('.');

const cli = meow(
  `
	Usage
	  $ foo <input>

	Options
	  --rainbow, -r  Include a rainbow

	Examples
	  $ foo unicorns --rainbow
	  🌈 unicorns 🌈
`,
  {
    flags: {
      rainbow: {
        type: 'boolean',
        alias: 'r',
      },
    },
  }
);
/*
{
	input: ['unicorns'],
	flags: {rainbow: true},
	...
}
*/

foo(cli.input[0], cli.flags);

API

meow(options,[minimistOptions])

返回的Object据有:

  • input (Array) - 非标志参数
  • flags (Object) - 标志参数转换为 驼峰字形
  • pkg (Object) - package.json对象
  • help (string) - 使用的帮助文本--help
  • showHelp([code=2]) (Function)- 显示帮助文本并退出代码为code
  • showVersion() (Function)- 显示版本文本并退出

options

type: Object Array string
Desc: 可以是一个字符串/数组的help文本或options对象.
flags
type: Object
Desc: 定义参数标志.

字段关键字是标志参数名称,值是下面对象结构:

  • type:值的类型.(可能的值:string boolean)
  • alias:通常用于定义短标志别名.
  • default:未指定标志时的默认值.

例:

flags: {
	unicorn: {
		type: 'string',
		alias: 'u',
		default: 'rainbow'
	}
}
description
type: string boolean
默认值: package.json的"description"属性
Desc: 在帮助文本上方显示.

将其设置为false完全禁用它.

help
type: string boolean
Desc: 您想要显示的帮助文本.

输入是重新缩进的,并且头尾的换行符剪掉,这意味着您可以使用模板文字无需关心使用正确数量的缩进.

此文本自动显示在帮助文本上方.

version
type: string boolean
默认值: package.json的"version"属性
Desc: 设置自定义版本输出
autoHelp
type: boolean
默认: true

自动显示帮助文本,当--help使用时.设置为false有用于当 CLI 的子 CLI 要使用自己的帮助文本时.

autoVersion
type: boolean
默认: true

自动显示版本文本,当--version使用时.设置为false有用于当 CLI 的子 CLI 要使用自己的版本文本时.

pkg
type: Object
默认值: 最近的 package.json
Desc: package.json 作为Object.

您很可能不需要此options.

argv
type: Array
默认: process.argv.slice(2)
Desc: 自定义参数对象.
inferType
type: boolean
默认: false
Desc: 推断参数类型.

默认情况下,参数5$ foo 5会变成一个字符串.启用此函数会将其推断为数字.

booleanDefault
type: boolean null undefined
默认: false

boolean类型的参数值若未定义在argv中.如果设置为undefined,那在argv没有定义的标志,将被排除在结果之外.

default值设定boolean类型的参数会优先于booleanDefault.

例:

const cli = meow(
  `
	Usage
	  $ foo

	Options
	  --rainbow, -r  Include a rainbow
	  --unicorn, -u  Include a unicorn
	  --no-sparkles  Exclude sparkles

	Examples
	  $ foo
	  🌈 unicorns✨🌈
`,
  {
    booleanDefault: undefined,
    flags: {
      rainbow: {
        type: 'boolean',
        default: true,
        alias: 'r',
      },
      unicorn: {
        type: 'boolean',
        default: false,
        alias: 'u',
      },
      cake: {
        type: 'boolean',
        alias: 'c',
      },
      sparkles: {
        type: 'boolean',
        default: true,
      },
    },
  }
);
/*
{
	flags: { 
		rainbow: true,
		r: true,
		unicorn: false,
		u: false,
		sparkles: true },

}
*/

Promise

meow会做出未经处理的失败的promise大声失败而不是默认的静默失败.这意味着您不必手动操作CLI 中使用的promise的.catch().

提示

查阅chalk如果要为终端输出着色.

查阅get-stdin如果你想接受来自 stdin 的输入.

查阅conf如果你需要保留一些数据.

查阅update-notifier如果你想要更新通知.

更有用的 CLI 实用程序......

执照

MIT ©Sindre Sorhus