util.parseArgs([config])
¥Stability: 1 - Experimental
-
config
<Object> 用于提供解析参数和配置解析器。config
支持以下属性:¥
config
<Object> Used to provide arguments for parsing and to configure the parser.config
supports the following properties:-
args
<string[]> 参数字符串数组。默认值:process.argv
删除了execPath
和filename
。¥
args
<string[]> array of argument strings. Default:process.argv
withexecPath
andfilename
removed. -
options
<Object> 用于描述解析器已知的参数。options
的键是选项的长名称,值是 <Object>,接受以下属性:¥
options
<Object> Used to describe arguments known to the parser. Keys ofoptions
are the long names of options and values are an <Object> accepting the following properties:-
type
<string> 参数类型,必须是boolean
或string
。¥
type
<string> Type of argument, which must be eitherboolean
orstring
. -
multiple
<boolean> 是否可以多次提供该选项。如果为true
,则所有的值都会被收集到一个数组中。如果为false
,则选项的值是最后获胜的。默认值:false
。¥
multiple
<boolean> Whether this option can be provided multiple times. Iftrue
, all values will be collected in an array. Iffalse
, values for the option are last-wins. Default:false
. -
short
<string> 选项的单个字符别名。¥
short
<string> A single character alias for the option. -
default
<string> | <boolean> | <string[]> | <boolean[]> 未由 args 设置时的默认选项值。它必须与type
属性的类型相同。当multiple
为true
时,它必须是一个数组。¥
default
<string> | <boolean> | <string[]> | <boolean[]> The default option value when it is not set by args. It must be of the same type as thetype
property. Whenmultiple
istrue
, it must be an array.
-
-
strict
<boolean> 当遇到未知参数时,或者当传入的参数与options
中配置的type
不匹配时,是否应该抛出错误。默认值:true
。¥
strict
<boolean> Should an error be thrown when unknown arguments are encountered, or when arguments are passed that do not match thetype
configured inoptions
. Default:true
. -
allowPositionals
<boolean> 此命令是否接受位置参数。默认值:如果strict
是true
,则为false
,否则为true
。¥
allowPositionals
<boolean> Whether this command accepts positional arguments. Default:false
ifstrict
istrue
, otherwisetrue
. -
tokens
<boolean> 返回解析的令牌。这对于扩展内置行为很有用,从添加额外检查到以不同方式重新处理令牌。默认值:false
。¥
tokens
<boolean> Return the parsed tokens. This is useful for extending the built-in behavior, from adding additional checks through to reprocessing the tokens in different ways. Default:false
.
-
-
返回:<Object> 解析后的命令行参数:
¥Returns: <Object> The parsed command line arguments:
-
values
<Object> 解析的选项名称与其 <string> 或 <boolean> 值的映射。¥
values
<Object> A mapping of parsed option names with their <string> or <boolean> values. -
positionals
<string[]> 位置参数。¥
positionals
<string[]> Positional arguments. -
tokens
<Object[]> | <undefined> 参见 parseArgs 标记 部分。仅当config
包含tokens: true
时才返回。¥
tokens
<Object[]> | <undefined> See parseArgs tokens section. Only returned ifconfig
includestokens: true
.
-
为命令行参数解析提供比直接与 process.argv
交互更高级别的 API。采用预期参数的规范并返回带有解析选项和位置的结构化对象。
¥Provides a higher level API for command-line argument parsing than interacting
with process.argv
directly. Takes a specification for the expected arguments
and returns a structured object with the parsed options and positionals.
import { parseArgs } from 'node:util';
const args = ['-f', '--bar', 'b'];
const options = {
foo: {
type: 'boolean',
short: 'f'
},
bar: {
type: 'string'
}
};
const {
values,
positionals
} = parseArgs({ args, options });
console.log(values, positionals);
// Prints: [Object: null prototype] { foo: true, bar: 'b' } []
const { parseArgs } = require('node:util');
const args = ['-f', '--bar', 'b'];
const options = {
foo: {
type: 'boolean',
short: 'f'
},
bar: {
type: 'string'
}
};
const {
values,
positionals
} = parseArgs({ args, options });
console.log(values, positionals);
// Prints: [Object: null prototype] { foo: true, bar: 'b' } []
util.parseArgs
是实验性的,行为可能会改变。加入 pkgjs/parseargs 中的对话,为设计做出贡献。
¥util.parseArgs
is experimental and behavior may change. Join the
conversation in pkgjs/parseargs to contribute to the design.