@curlconverter/yargs-parser

中文文档
0.0.1发布于 2021/10/21ISCESM + CJS零依赖近 30 天下载 6.9万

yargs 使用的强大选项解析器分叉

中文使用文档
文档目录28 节
  1. 示例
  2. 德诺示例
  3. 无害环境管理示例
  4. API 密码
  5. parser( args, 选项%%% )
  6. 需要('yargs-parser')。详细(args, opts-%)
  7. 配置
  8. 短选项组
  9. 骆驼案扩展
  10. 点注
  11. 解析数字
  12. 解析位置编号
  13. 布尔否定
  14. 组合数组
  15. 重复参数数组
  16. 平整重复阵列
  17. 贪婪的阵列
  18. nargs 选择权
  19. 否定前缀
  20. 居中区 --
  21. 设置占位符密钥
  22. 非选项时停止
  23. 条形别名
  24. 已脱光
  25. 作为参数的未知选项
  26. 支持的节点.js版本
  27. 特别 谢谢
  28. 许可证

雅尔格-派瑟

ci NPM version Conventional Commits nycrc config on GitHub

强大的选项解析器 rg。 。 。 .

访问 雅尔格斯网站 更多的例子, 和彻底的用法指示.

示例

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

或分析一个字符串!

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

将混合类型数组转换为 yargs-parser数字 :

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

德诺示例

截止 v19 yargs-parser 支持 德诺数字 :

import parser from "https://deno.land/x/yargs_parser/deno.ts";

const argv = parser('--foo=99 --bar=9987930', {
  string: ['bar']
})
console.log(argv)

无害环境管理示例

截止 v19 yargs-parser 支持无害环境管理在节点.js和浏览器中:

节点.js:

import parser from 'yargs-parser'

const argv = parser('--foo=99 --bar=9987930', {
  string: ['bar']
})
console.log(argv)

浏览器 :

<!doctype html>
<body>
  <script type="module">
    import parser from "https://unpkg.com/[email protected]/browser.js";

    const argv = parser('--foo=99 --bar=9987930', {
      string: ['bar']
    })
    console.log(argv)
  </script>
</body>

API 密码

parser( args, 选项%%% )

解析命令行参数返回一个简单的密钥和值映射 .

期望:

  • args: 代表选项解析的字符串或数组.
  • opts: 提供一组提示 args 应解析 :
    • opts.alias: 代表密钥同名词组的对象: {alias: {foo: ['f']}}。 。 。 .
    • opts.array: 表示按键应被解析为数组: {array: ['foo', 'bar']}。 。 。 .
      指示键应被解析为数组并被强制取出 / 数字 :
      {array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}。 。 。 .
    • opts.boolean: 参数应被解为布尔: {boolean: ['x', 'y']}。 。 。 .
    • opts.coerce: 提供自定义同步函数,从提供的参数返回被强制值 (或掷出一出出错. 对于数组,该函数只对整个数组调用一次:
      {coerce: {foo: function (arg) {return modifiedArg}}}。 。 。 .
    • opts.config: 表示一个代表配置文件路径的密钥( 此文件将被加载并解析) .
    • opts.configObjects: 配置对象来解析,其属性将被设定为参数 :
      {configObjects: [{'x': 5, 'y': 33}, {'z': 44}]}。 。 。 .
    • opts.configuration: 为 args-parser 提供配置选项(参见 : 配置) (中文(简体) ).
    • opts.count:表示应当用作计数器的密钥,例如, -vvv =: {v: 3}。 。 。 .
    • opts.default: 提供密钥的默认值 : {default: {x: 33, y: 'hello world!'}}。 。 。 .
    • opts.envPrefix环境变量(process.env) 提供前缀的,应当解析.
    • opts.narg: 指定密钥需要 n 参数 : {narg: {x: 2}}。 。 。 .
    • opts.normalize数字 : path.normalize() 将应用到为此密钥设定的值.
    • opts.number: 键应作为数字处理.
    • opts.string: 键应作为字符串处理(即使它们类似数字) -x 33) (中文(简体) ).

返回 :

  • obj: 表示解析值的对象 args
    • key/value:每个参数及其别名的键值对.
    • _: 代表位置参数的数组.
    • [可选 --: 选项尾旗后带有参数的数组 --。 。 。 .

需要('yargs-parser')。详细(args, opts-%)

解析命令行字符串,返回命令所需的详细信息 雅格士发动机.

期望:

  • args: 代表选项的字符串或数组.
  • opts: 提供一组提示 args,输入与 require('yargs-parser')(args, opts={})。 。 。 .

返回 :

  • argv: 表示解析值的对象 args
    • key/value:每个参数及其别名的键值对.
    • _: 代表位置参数的数组.
    • [可选 --: 选项尾旗后带有参数的数组 --。 。 。 .
  • error: 如果在解析过程中发生了例外,则包含一个出错对象.
  • aliases: 合并下列列表后得出的别名清单: opts.alias。 。 。 .
  • newAliases: 通过骆驼案扩展添加的任何新的别名 :
    • boolean数字 : { fooBar: true }
  • defaulted: 由 opts.default,不包括别名.
    • boolean数字 : { foo: true }
  • configuration: 默认设置和 opts.configuration。 。 。 .

配置

Yargs-parser 在所提供的密钥上应用了多个自动转换 内 args这些功能可以打开或关闭 configuration 字段 联 合 国 opts。 。 。 .

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

短选项组

  • 默认 : true。 。 。 .
  • 键 : short-option-groups。 。 。 .

是否应该把一组短选项当作布尔旗来对待?

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

如果已禁用:

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

骆驼案扩展

  • 默认 : true。 。 。 .
  • 键 : camel-case-expansion。 。 。 .

是否应该将连字符化的论点扩展为骆驼案件别名?

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

如果已禁用:

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

点注

  • 默认 : true
  • 键 : dot-notation

包含 . 作为物体对待?

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

如果已禁用:

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

解析数字

  • 默认 : true
  • 键 : parse-numbers

看上去像数字的钥匙应该这样对待吗?

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

如果已禁用:

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

解析位置编号

  • 默认 : true
  • 键 : parse-positional-numbers

应把看起来像数字的定位键当作数字处理.

$ node example.js 99.3
{ _: [99.3] }

如果已禁用:

$ node example.js 99.3
{ _: ['99.3'] }

布尔否定

  • 默认 : true
  • 键 : boolean-negation

变量应使用前缀 --no 被视为否定?

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

如果已禁用:

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

组合数组

  • 默认 : false
  • 键 : combine-arrays

当命令行参数和 一个配置文件.

重复参数数组

  • 默认 : true
  • 键 : duplicate-arguments-array

如果重复时, 参数会被强制输入数组 :

$ node example.js -x 1 -x 2
{ _: [], x: [1, 2] }

如果已禁用:

$ node example.js -x 1 -x 2
{ _: [], x: 2 }

平整重复阵列

  • 默认 : true
  • 键 : flatten-duplicate-arrays

重复时将数组参数强制为单个数组时:

$ node example.js -x 1 2 -x 3 4
{ _: [], x: [1, 2, 3, 4] }

如果已禁用:

$ node example.js -x 1 2 -x 3 4
{ _: [], x: [[1, 2], [3, 4]] }

贪婪的阵列

  • 默认 : true
  • 键 : greedy-arrays

如果数组会按其旗帜消耗不止一个位置参数 .

$ node example --arr 1 2
{ _: [], arr: [1, 2] }

如果已禁用:

$ node example --arr 1 2
{ _: [2], arr: [1] }

说明: v18.0.0 我们正在考虑 默认贪婪的阵列 false。 。 。 .

nargs 选择权

  • 默认 : false
  • 键 : nargs-eats-options

如果 nargs 消耗了破损选项和位置参数 .

否定前缀

  • 默认 : no-
  • 键 : negation-prefix

用于否定布尔变量的前缀 .

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

如果设置为 quux数字 :

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

居中区 --

  • 默认 : false。 。 。 .
  • 键 : populate--

未解析的旗帜是否存储于 -- 或者说 _。 。 。 .

如果禁用:

$ node example.js a -b -- x y
{ _: [ 'a', 'x', 'y' ], b: true }

如果启用:

$ node example.js a -b -- x y
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }

设置占位符密钥

  • 默认 : false。 。 。 .
  • 键 : set-placeholder-key。 。 。 .

不通过相应的 CLI 参数设置密钥的占位符是否应当添加 ?

如果禁用:

$ node example.js -a 1 -c 2
{ _: [], a: 1, c: 2 }

如果启用:

$ node example.js -a 1 -c 2
{ _: [], a: 1, b: undefined, c: 2 }

非选项时停止

  • 默认 : false。 。 。 .
  • 键 : halt-at-non-option。 。 。 .

解析是否应该停止于第一个立场论证? 这类似于例如:. ssh 解析其命令行.

如果禁用:

$ node example.js -a run b -x y
{ _: [ 'b' ], a: 'run', x: 'y' }

如果启用:

$ node example.js -a run b -x y
{ _: [ 'b', '-x', 'y' ], a: 'run' }

条形别名

  • 默认 : false
  • 键 : strip-aliased

在返回结果之前是否应当去除别名?

如果禁用:

$ node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }

如果启用:

$ node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1 }

已脱光

  • 默认 : false
  • 键 : strip-dashed

在返回结果之前是否应当删除已破碎的密钥 ? 如果 camel-case-expansion 已禁用.

如果禁用:

$ node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1 }

如果启用:

$ node example.js --test-field 1
{ _: [], testField: 1 }

作为参数的未知选项

  • 默认 : false
  • 键 : unknown-options-as-args

是否应将未知选项作为常规论据对待? 未知选项是非 配置于 opts。 。 。 .

如果已禁用

$ node example.js --unknown-option --known-option 2 --string-option --unknown-option2
{ _: [], unknownOption: true, knownOption: 2, stringOption: '', unknownOption2: true }

如果启用

$ node example.js --unknown-option --known-option 2 --string-option --unknown-option2
{ _: ['--unknown-option'], knownOption: 2, stringOption: '--unknown-option2' }

支持的节点.js版本

这一生态系统的图书馆尽最大努力追踪 节点Js的发布时间表这里 (单位:千美元) 贴上我们为什么认为 这很重要。 。 。 .

特别 谢谢

雅格斯项目从乐观主义和最小化演变而来. 它欠自己的钱 詹姆斯·哈利迪的辛勤工作 谢谢 子存储 哔声 嘘 \o/ (% 1)

许可证

公务员制度委员会

版本与发布记录

@curlconverter/yargs-parser 共发布 1 个版本,首次发布于 2021/10/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2021/10/215 年前

关于 @curlconverter/yargs-parser 的常见问题

@curlconverter/yargs-parser 是什么?

yargs 使用的强大选项解析器分叉

如何安装 @curlconverter/yargs-parser?

在项目目录运行 npm install @curlconverter/yargs-parser,或使用 pnpm add / yarn add / bun add。

@curlconverter/yargs-parser 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/curlconverter__yargs-parser 是否存在。

@curlconverter/yargs-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@curlconverter/yargs-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@curlconverter/yargs-parser 可以商用吗?

@curlconverter/yargs-parser 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@curlconverter/yargs-parser 的下载量有多少?

根据 npm 官方下载统计,@curlconverter/yargs-parser 在 2026/10/11 – 2026/11/09 这 30 天内被下载 68,529 次,日均约 2,284 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@curlconverter/yargs-parser 还在维护吗?

最新版本 0.0.1 发布于 2021/10/21(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/10/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。