1'use strict'; 2 3// This example is used in the documentation. 4 5// How might I add my own support for --no-foo? 6 7// 1. const { parseArgs } = require('node:util'); // from node 8// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package 9const { parseArgs } = require('..'); // in repo 10 11const options = { 12 'color': { type: 'boolean' }, 13 'no-color': { type: 'boolean' }, 14 'logfile': { type: 'string' }, 15 'no-logfile': { type: 'boolean' }, 16}; 17const { values, tokens } = parseArgs({ options, tokens: true }); 18 19// Reprocess the option tokens and overwrite the returned values. 20tokens 21 .filter((token) => token.kind === 'option') 22 .forEach((token) => { 23 if (token.name.startsWith('no-')) { 24 // Store foo:false for --no-foo 25 const positiveName = token.name.slice(3); 26 values[positiveName] = false; 27 delete values[token.name]; 28 } else { 29 // Resave value so last one wins if both --foo and --no-foo. 30 values[token.name] = token.value ?? true; 31 } 32 }); 33 34const color = values.color; 35const logfile = values.logfile ?? 'default.log'; 36 37console.log({ logfile, color }); 38 39// Try the following: 40// node negate.js 41// node negate.js --no-logfile --no-color 42// negate.js --logfile=test.log --color 43// node negate.js --no-logfile --logfile=test.log --color --no-color 44