雅尔格-派瑟
强大的选项解析器 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: 表示解析值的对象argskey/value:每个参数及其别名的键值对._: 代表位置参数的数组.- [可选
--: 选项尾旗后带有参数的数组--。 。 。 .
需要('yargs-parser')。详细(args, opts-%)
解析命令行字符串,返回命令所需的详细信息 雅格士发动机.
期望:
args: 代表选项的字符串或数组.opts: 提供一组提示args,输入与require('yargs-parser')(args, opts={})。 。 。 .
返回 :
argv: 表示解析值的对象argskey/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)
许可证
公务员制度委员会