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:
-
所有令牌
\all tokens
-
kind<string> '选项'、'positional' 或 'option-terminator' 之一。\
kind<string> One of 'option', 'positional', or 'option-terminator'. -
index<number>args中包含标记的元素索引。所以令牌的源参数是args[token.index]。\
index<number> Index of element inargscontaining token. So the source argument for a token isargs[token.index].
-
-
选项令牌
\option tokens
-
name<string> 选项的长名称。\
name<string> Long name of option. -
rawName<string> 如何在 args 中使用选项,例如--foo的-f。\
rawName<string> How option used in args, like-fof--foo. -
value<string> | <undefined> 参数中指定的选项值。布尔选项未定义。\
value<string> | <undefined> Option value specified in args. Undefined for boolean options. -
inlineValue<boolean> | <undefined> 是否内联指定选项值,如--foo=bar。\
inlineValue<boolean> | <undefined> Whether option value specified inline, like--foo=bar.
-
-
位置标记
\positional tokens
-
选项终止令牌
\option-terminator token
返回的令牌按照输入参数中遇到的顺序。在 args 中出现多次的选项每次使用都会产生一个令牌。像 -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 }