util.parseArgs([config])
稳定性: 1 - 实验
-
config
<Object> 用于提供解析参数和配置解析器。config
支持以下属性:args
<string[]> 参数字符串数组。 默认值:process.argv
删除了execPath
和filename
。options
<Object> 用于描述解析器已知的参数。options
的键是选项的长名称,值是 <Object>,接受以下属性:strict
<boolean> 当遇到未知参数时,或者当传入的参数与options
中配置的type
不匹配时,是否应该抛出错误。 默认值:true
。allowPositionals
<boolean> 此命令是否接受位置参数。 默认值:false
如果strict
是true
,否则true
。tokens
<boolean> 返回解析的令牌。 这对于扩展内置行为很有用,从添加额外检查到以不同方式重新处理令牌。 默认值:false
。
-
返回: <Object> 解析后的命令行参数:
values
<Object> 解析的选项名称与其 <string> 或 <boolean> 值的映射。positionals
<string[]> 位置参数。tokens
<Object[]> | <undefined> 参见 parseArgs 令牌章节。 仅当config
包含tokens: true
时才返回。
为命令行参数解析提供比直接与 process.argv
交互更高级别的 API。
采用预期参数的规范并返回带有解析选项和位置的结构化对象。
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);
// 打印: [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);
// 打印: [Object: null prototype] { foo: true, bar: 'b' } []
util.parseArgs
是实验性的,行为可能会改变。
加入 pkgjs/parseargs 中的对话,为设计做出贡献。
Stability: 1 - Experimental
-
config
<Object> Used to provide arguments for parsing and to configure the parser.config
supports the following properties:args
<string[]> array of argument strings. Default:process.argv
withexecPath
andfilename
removed.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> Type of argument, which must be eitherboolean
orstring
.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> A single character alias for the option.
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> Whether this command accepts positional arguments. Default:false
ifstrict
istrue
, otherwisetrue
.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
.
-
Returns: <Object> The parsed command line arguments:
values
<Object> A mapping of parsed option names with their <string> or <boolean> values.positionals
<string[]> Positional arguments.tokens
<Object[]> | <undefined> See parseArgs tokens section. Only returned ifconfig
includestokens: true
.
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
is experimental and behavior may change. Join the
conversation in pkgjs/parseargs to contribute to the design.