• 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// Require the use of `=` for long options and values by blocking
6// the use of space separated values.
7// So allow `--foo=bar`, and not allow `--foo bar`.
8//
9// Note: this is not a common behaviour, most CLIs allow both forms.
10
11// 1. const { parseArgs } = require('node:util'); // from node
12// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package
13const { parseArgs } = require('..'); // in repo
14
15const options = {
16  file: { short: 'f', type: 'string' },
17  log: { type: 'string' },
18};
19
20const { values, tokens } = parseArgs({ options, tokens: true });
21
22const badToken = tokens.find((token) => token.kind === 'option' &&
23  token.value != null &&
24  token.rawName.startsWith('--') &&
25  !token.inlineValue
26);
27if (badToken) {
28  throw new Error(`Option value for '${badToken.rawName}' must be inline, like '${badToken.rawName}=VALUE'`);
29}
30
31console.log(values);
32
33// Try the following:
34//    node limit-long-syntax.js -f FILE --log=LOG
35//    node limit-long-syntax.js --file FILE
36