Skip to content

Commit 89e1b63

Browse files
authored
docs: add example uses of tokens (#136)
1 parent c512875 commit 89e1b63

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

examples/limit-long-syntax.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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
13+
const { parseArgs } = require('..'); // in repo
14+
15+
const options = {
16+
file: { short: 'f', type: 'string' },
17+
log: { type: 'string' },
18+
};
19+
20+
const { values, tokens } = parseArgs({ options, tokens: true });
21+
22+
const badToken = tokens.find((token) => token.kind === 'option' &&
23+
token.value != null &&
24+
token.rawName.startsWith('--') &&
25+
!token.inlineValue
26+
);
27+
if (badToken) {
28+
throw new Error(`Option value for '${badToken.rawName}' must be inline, like '${badToken.rawName}=VALUE'`);
29+
}
30+
31+
console.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

examples/no-repeated-options.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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
9+
const { parseArgs } = require('..'); // in repo
10+
11+
const options = {
12+
ding: { type: 'boolean', short: 'd' },
13+
beep: { type: 'boolean', short: 'b' }
14+
};
15+
const { values, tokens } = parseArgs({ options, tokens: true });
16+
17+
const seenBefore = new Set();
18+
tokens.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+
26+
console.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

examples/ordered-options.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This is an example of using tokens to add a custom behaviour.
2+
//
3+
// This adds a option order check so that --some-unstable-option
4+
// may only be used after --enable-experimental-options
5+
//
6+
// Note: this is not a common behaviour, the order of different options
7+
// does not usually matter.
8+
9+
import { parseArgs } from '../index.js';
10+
11+
function findTokenIndex(tokens, target) {
12+
return tokens.findIndex((token) => token.kind === 'option' &&
13+
token.name === target
14+
);
15+
}
16+
17+
const experimentalName = 'enable-experimental-options';
18+
const unstableName = 'some-unstable-option';
19+
20+
const options = {
21+
[experimentalName]: { type: 'boolean' },
22+
[unstableName]: { type: 'boolean' },
23+
};
24+
25+
const { values, tokens } = parseArgs({ options, tokens: true });
26+
27+
const experimentalIndex = findTokenIndex(tokens, experimentalName);
28+
const unstableIndex = findTokenIndex(tokens, unstableName);
29+
if (unstableIndex !== -1 &&
30+
((experimentalIndex === -1) || (unstableIndex < experimentalIndex))) {
31+
throw new Error(`'--${experimentalName}' must be specified before '--${unstableName}'`);
32+
}
33+
34+
console.log(values);
35+
36+
/* eslint-disable max-len */
37+
// Try the following:
38+
// node ordered-options.mjs
39+
// node ordered-options.mjs --some-unstable-option
40+
// node ordered-options.mjs --some-unstable-option --enable-experimental-options
41+
// node ordered-options.mjs --enable-experimental-options --some-unstable-option

0 commit comments

Comments
 (0)