util.parseArgs([config])
-
config<Object> 用于提供解析参数和配置解析器。config支持以下属性:¥
config<Object> Used to provide arguments for parsing and to configure the parser.configsupports the following properties:-
args<string[]> 参数字符串数组。默认值:process.argv删除了execPath和filename。¥
args<string[]> array of argument strings. Default:process.argvwithexecPathandfilenameremoved. -
options<Object> 用于描述解析器已知的参数。options的键是选项的长名称,值是 <Object>,接受以下属性:¥
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> 参数类型,必须是boolean或string。¥
type<string> Type of argument, which must be eitherbooleanorstring. -
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[]> 当(且仅当)选项未出现在要解析的参数中时,要使用的默认值。它必须与type属性的类型相同。当multiple为true时,它必须是一个数组。¥
default<string> | <boolean> | <string[]> | <boolean[]> The default value to be used if (and only if) the option does not appear in the arguments to be parsed. It must be of the same type as thetypeproperty. Whenmultipleistrue, 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 thetypeconfigured inoptions. Default:true. -
allowPositionals<boolean> 此命令是否接受位置参数。默认值:如果strict是true,则为false,否则为true。¥
allowPositionals<boolean> Whether this command accepts positional arguments. Default:falseifstrictistrue, otherwisetrue. -
allowNegative<boolean> 如果是true,则允许通过在选项名称前加上--no-来明确将布尔选项设置为false。默认值:false。¥
allowNegative<boolean> Iftrue, allows explicitly setting boolean options tofalseby prefixing the option name with--no-. Default:false. -
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 ifconfigincludestokens: 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' } []