Umama-at-Bluchip/Quick-UI
0
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 node8// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package9const { parseArgs } = require('..'); // in repo10 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.20tokens21 .filter((token) => token.kind === 'option')22 .forEach((token) => {23 if (token.name.startsWith('no-')) {24 // Store foo:false for --no-foo25 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.js41// node negate.js --no-logfile --no-color42// negate.js --logfile=test.log --color43// node negate.js --no-logfile --logfile=test.log --color --no-color44 