-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add parsed meta-data to returned properties (#129)
- Loading branch information
1 parent
6a7e969
commit 91bfb4d
Showing
10 changed files
with
597 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'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 |
Oops, something went wrong.