-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnegate.js
41 lines (34 loc) · 1.25 KB
/
negate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
// 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: 'string' },
['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 --logfile=test.log --color=red
// node negate.js --no-logfile --no-color
// node negate.js --no-logfile --logfile=test.log --color=red --no-color