目标
把 argv 解析为 flags、values、positionals 三段结构:已声明的旗标集合与数值键集合之外的选项一律拒绝,数值键的值必须可转数字。
要点
双横线之后的全部进位置参数;demo 解析 –verbose –count 3 file.txt 输出结构化 JSON。
提示
不支持短旗标与 –no- 取反,只认 spec 声明的长形式。
验证
node –test 四个用例覆盖解析表、未知选项、非法数值与双横线;demo 输出 JSON 含 verbose true 与 count 3。 复现命令:
node --test argv.test.mjs
node argv.mjs --verbose --count 3 file.txt
预期输出要点:The demo parses –verbose –count 3 file.txt into flags, numbers, and positionals and prints the structured result as JSON.
实测证据:Node 22.23.2: node –test -> 4 passed; CLI parsed –verbose –count 3 file.txt into flags.verbose true, count 3, and one positional.