• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This is an example of using tokens to add a custom behaviour.
4//
5// Throw an error if an option is used more than once.
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  ding: { type: 'boolean', short: 'd' },
13  beep: { type: 'boolean', short: 'b' }
14};
15const { values, tokens } = parseArgs({ options, tokens: true });
16
17const seenBefore = new Set();
18tokens.forEach((token) => {
19  if (token.kind !== 'option') return;
20  if (seenBefore.has(token.name)) {
21    throw new Error(`option '${token.name}' used multiple times`);
22  }
23  seenBefore.add(token.name);
24});
25
26console.log(values);
27
28// Try the following:
29//    node no-repeated-options --ding --beep
30//    node no-repeated-options --beep -b
31//    node no-repeated-options -ddd
32