Sha256: c0a19806fabd2322a5e59c32acbf1b533730c7c63b6f2fa91ed234ac26acdb1d

Contents?: true

Size: 1.28 KB

Versions: 33

Compression:

Stored size: 1.28 KB

Contents

'use strict';

// This example is used in the documentation.

// How might I add my own support for --no-foo?

// 1. const { parseArgs } = require('node:util'); // from node
// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package
const { parseArgs } = require('..'); // in repo

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 });

// Try the following:
//    node negate.js
//    node negate.js --no-logfile --no-color
//    negate.js --logfile=test.log --color
//    node negate.js --no-logfile --logfile=test.log --color --no-color

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.59 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.58 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.57 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.56 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.55 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.54 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.53 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.52 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.51 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.50 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.49 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.48 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.47 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.46 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.45 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.44 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.43 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.42 node_modules/@pkgjs/parseargs/examples/negate.js
immosquare-cleaner-0.1.41 node_modules/@pkgjs/parseargs/examples/negate.js