parseArgs tokens


通过在配置中指定 tokens: true,可以获取用于添加自定义行为的详细解析信息。返回的令牌具有描述以下内容的属性:

【Detailed parse information is available for adding custom behaviors by specifying tokens: true in the configuration. The returned tokens have properties describing:】

  • 所有令牌
    • kind <string> 可选值之一:'option'、'positional' 或 'option-terminator'。
    • index <number> 包含该 token 的 args 中元素的索引。因此,token 的来源参数是 args[token.index]
  • 选项令牌
    • name <string> 选项的完整名称。
    • rawName <string> 如何在参数中使用选项,比如 --foo-f
    • value <string> | <undefined> 在参数中指定的选项值。 布尔选项未定义。
    • inlineValue <boolean> | <undefined> 是否在行内指定选项值,例如 --foo=bar
  • 位置标记
    • value <string> args 中位置参数的值(即 args[index])。
  • 选项终止符标记

返回的标记按输入参数中出现的顺序排列。在参数中出现多次的选项会为每次出现生成一个标记。像 -xy 这样的短选项组会扩展为每个选项一个标记。因此,-xxx 会生成三个标记。

【The returned tokens are in the order encountered in the input args. Options that appear more than once in args produce a token for each use. Short option groups like -xy expand to a token for each option. So -xxx produces three tokens.】

例如,要添加对像 --no-color 这样的否定选项的支持(当选项类型为 boolean 时,allowNegative 支持这种用法),可以对返回的标记进行重新处理,以更改存储的否定选项的值。

【For example, to add support for a negated option like --no-color (which allowNegative supports when the option is of boolean type), the returned tokens can be reprocessed to change the value stored for the negated option.】

import { parseArgs } from 'node:util';

const options = {
  'color': { type: 'boolean' },
  'no-color': { type: 'boolean' },
  'logfile': { type: 'string' },
  'no-logfile': { type: 'boolean' },
};
const { values, tokens } = parseArgs({ options, tokens: true });

// Reprocess the option tokens and overwrite the returned values.
tokens
  .filter((token) => token.kind === 'option')
  .forEach((token) => {
    if (token.name.startsWith('no-')) {
      // Store foo:false for --no-foo
      const positiveName = token.name.slice(3);
      values[positiveName] = false;
      delete values[token.name];
    } else {
      // Resave value so last one wins if both --foo and --no-foo.
      values[token.name] = token.value ?? true;
    }
  });

const color = values.color;
const logfile = values.logfile ?? 'default.log';

console.log({ logfile, color });const { parseArgs } = require('node:util');

const options = {
  'color': { type: 'boolean' },
  'no-color': { type: 'boolean' },
  'logfile': { type: 'string' },
  'no-logfile': { type: 'boolean' },
};
const { values, tokens } = parseArgs({ options, tokens: true });

// Reprocess the option tokens and overwrite the returned values.
tokens
  .filter((token) => token.kind === 'option')
  .forEach((token) => {
    if (token.name.startsWith('no-')) {
      // Store foo:false for --no-foo
      const positiveName = token.name.slice(3);
      values[positiveName] = false;
      delete values[token.name];
    } else {
      // Resave value so last one wins if both --foo and --no-foo.
      values[token.name] = token.value ?? true;
    }
  });

const color = values.color;
const logfile = values.logfile ?? 'default.log';

console.log({ logfile, color });

示例用法显示否定选项,以及当一个选项被多次使用时,最后一次生效。

【Example usage showing negated options, and when an option is used multiple ways then last one wins.】

$ node negate.js
{ logfile: 'default.log', color: undefined }
$ node negate.js --no-logfile --no-color
{ logfile: false, color: false }
$ node negate.js --logfile=test.log --color
{ logfile: 'test.log', color: true }
$ node negate.js --no-logfile --logfile=test.log --color --no-color
{ logfile: 'test.log', color: false }