# @curlconverter/yargs-parser 中文文档

规范地址：https://npmcn.com/package/%40curlconverter/yargs-parser/

版本：0\.0\.1
npm 元数据更新时间：2022-04-05T02:20:05.480Z
中文译文更新时间：2026-11-11T04:58:03.167Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40curlconverter/yargs-parser
原文对照：https://npmcn.com/package/%40curlconverter/yargs-parser/?lang=original
项目主页：https://github.com/curlconverter/yargs-parser
源码仓库：https://github.com/curlconverter/yargs-parser

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：126 kB，11 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- Node.js 要求：\>=10
- 运行依赖：0 个
- 首次发布：2021-10-20T21:39:50.063Z
- 最新版本发布：2021-10-20T21:39:50.272Z
- 近 30 天下载量：68529（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

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

## 安装

```sh
npm install @curlconverter/yargs-parser
```

也可以使用：`pnpm add @curlconverter/yargs-parser`、`yarn add @curlconverter/yargs-parser`、`bun add @curlconverter/yargs-parser`

## README

# 雅尔格-派瑟

![ci](https://github.com/yargs/yargs-parser/workflows/ci/badge.svg) [![NPM version](https://img.shields.io/npm/v/yargs-parser.svg)](https://www.npmjs.com/package/yargs-parser) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org/) ![nycrc config on GitHub](https://img.shields.io/nycrc/yargs/yargs-parser)

强大的选项解析器 [rg](https://github.com/yargs/yargs)。 。 。 .

访问 [雅尔格斯网站](http://yargs.js.org/) 更多的例子, 和彻底的用法指示.

![](https://raw.githubusercontent.com/yargs/yargs-parser/main/yargs-logo.png)

## 示例

```sh
npm i yargs-parser --save
```

```js
const argv = require('yargs-parser')(process.argv.slice(2))
console.log(argv)
```

```console
$ node example.js --foo=33 --bar hello
{ _: [], foo: 33, bar: 'hello' }
```

_或分析一个字符串!_

```js
const argv = require('yargs-parser')('--foo=99 --bar=33')
console.log(argv)
```

```console
{ _: [], foo: 99, bar: 33 }
```

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

```js
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` 支持 [德诺](https://github.com/denoland/deno)数字 :

```typescript
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:**

```js
import parser from 'yargs-parser'

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

**浏览器 :**

```html
<!doctype html>
<body>
  <script type="module">
    import parser from "https://unpkg.com/yargs-parser@19.0.0/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 提供配置选项(参见 : [配置](https://npmcn.com/package/%40curlconverter/yargs-parser/#readme-configuration)) (中文(简体) ).
    -   `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`。 。 。 .

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

### 短选项组

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

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

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

_如果已禁用:_

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

### 骆驼案扩展

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

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

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

_如果已禁用:_

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

### 点注

-   默认 : `true`
-   键 : `dot-notation`

包含 `.` 作为物体对待?

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

_如果已禁用:_

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

### 解析数字

-   默认 : `true`
-   键 : `parse-numbers`

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

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

_如果已禁用:_

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

### 解析位置编号

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

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

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

_如果已禁用:_

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

### 布尔否定

-   默认 : `true`
-   键 : `boolean-negation`

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

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

_如果已禁用:_

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

### 组合数组

-   默认 : `false`
-   键 : `combine-arrays`

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

### 重复参数数组

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

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

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

_如果已禁用:_

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

### 平整重复阵列

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

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

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

_如果已禁用:_

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

### 贪婪的阵列

-   默认 : `true`
-   键 : `greedy-arrays`

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

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

_如果已禁用:_

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

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

### nargs 选择权

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

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

### 否定前缀

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

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

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

_如果设置为 `quux`数字 :_

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

### 居中区 --

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

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

_如果禁用:_

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

_如果启用:_

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

### 设置占位符密钥

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

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

_如果禁用:_

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

_如果启用:_

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

### 非选项时停止

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

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

_如果禁用:_

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

_如果启用:_

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

### 条形别名

-   默认 : `false`
-   键 : `strip-aliased`

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

_如果禁用:_

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

_如果启用:_

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

### 已脱光

-   默认 : `false`
-   键 : `strip-dashed`

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

_如果禁用:_

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

_如果启用:_

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

### 作为参数的未知选项

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

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

_如果已禁用_

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

_如果启用_

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

## 支持的节点.js版本

这一生态系统的图书馆尽最大努力追踪 [节点Js的发布时间表](https://nodejs.org/en/about/releases/)这里 [(单位:千美元) 贴上我们为什么认为 这很重要](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a)。 。 。 .

## 特别 谢谢

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

## 许可证

公务员制度委员会

## 常见问题

### @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 和提交记录判断。

