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。
-
返回: <Object> 解析后的命令行参数:
values<Object> 解析的选项名称与其 <string> 或 <boolean> 值的映射。positionals<string[]> 位置参数。
为命令行参数解析提供比直接与 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.configsupports the following properties:args<string[]> array of argument strings. Default:process.argvwithexecPathandfilenameremoved.options<Object> Used to describe arguments known to the parser. Keys ofoptionsare the long names of options and values are an <Object> accepting the following properties:type<string> Type of argument, which must be eitherbooleanorstring.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 thetypeconfigured inoptions. Default:true.allowPositionals: <boolean> Whether this command accepts positional arguments. Default:falseifstrictistrue, otherwisetrue.
-
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.
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.