util.parseArgs([config])


稳定性: 1 - 实验

  • config <Object> 用于提供解析参数和配置解析器。 config 支持以下属性:

    • args <string[]> 参数字符串数组。 默认值: process.argv 删除了 execPathfilename
    • options <Object> 用于描述解析器已知的参数。 options 的键是选项的长名称,值是 <Object>,接受以下属性:
      • type <string> 参数类型,必须是 booleanstring
      • multiple <boolean> 是否可以多次提供该选项。 如果为 true,则所有的值都会被收集到一个数组中。 如果为 false,则选项的值是最后获胜的。 默认值: false
      • short <string> 选项的单个字符别名。
      • default <string> | <boolean> | <string[]> | <boolean[]> 未由 args 设置时的默认选项值。 它必须与 type 属性的类型相同。 当 multipletrue 时,它必须是一个数组。
    • strict <boolean> 当遇到未知参数时,或者当传入的参数与 options 中配置的 type 不匹配时,是否应该抛出错误。 默认值: true
    • allowPositionals <boolean> 此命令是否接受位置参数。 默认值: false 如果 stricttrue,否则 true
    • tokens <boolean> 返回解析的令牌。 这对于扩展内置行为很有用,从添加额外检查到以不同方式重新处理令牌。 默认值: false
  • 返回: <Object> 解析后的命令行参数:

为命令行参数解析提供比直接与 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 with execPath and filename removed.
    • options <Object> Used to describe arguments known to the parser. Keys of options are the long names of options and values are an <Object> accepting the following properties:
      • type <string> Type of argument, which must be either boolean or string.
      • multiple <boolean> Whether this option can be provided multiple times. If true, all values will be collected in an array. If false, values for the option are last-wins. Default: false.
      • short <string> A single character alias for the option.
      • default <string> | <boolean> | <string[]> | <boolean[]> The default option value when it is not set by args. It must be of the same type as the type property. When multiple is true, it must be an array.
    • strict <boolean> Should an error be thrown when unknown arguments are encountered, or when arguments are passed that do not match the type configured in options. Default: true.
    • allowPositionals <boolean> Whether this command accepts positional arguments. Default: false if strict is true, otherwise true.
    • 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:

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.