-
Notifications
You must be signed in to change notification settings - Fork 10
docs: add example uses of tokens #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
786e149
docs: add example uses of tokens
shadowspawn 5a85c38
Merge branch 'main' into feature/token-examples
shadowspawn b1b632c
Merge branch 'main' into feature/token-examples
shadowspawn 0da08c8
Lint and feedback
shadowspawn 2c71fcd
Merge remote-tracking branch 'upstream/main' into feature/token-examples
shadowspawn 8d9844d
Leave negate.js alone
shadowspawn 14d9515
Merge branch 'main' into feature/token-examples
shadowspawn d98c9a7
Clarify the examples that are more about doing something interesting …
shadowspawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,30 @@ | ||
'use strict'; | ||
|
||
// How might I require long options with values use '='? | ||
// So allow `--foo=bar`, and not allow `--foo bar`. | ||
|
||
// 1. const { parseArgs } = require('node:util'); // from node | ||
// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package | ||
const { parseArgs } = require('..'); // in repo | ||
|
||
const options = { | ||
file: { short: 'f', type: 'string' }, | ||
log: { type: 'string' }, | ||
}; | ||
|
||
const { values, tokens } = parseArgs({ options, tokens: true }); | ||
|
||
const badToken = tokens.find((token) => token.kind === 'option' && | ||
token.value != null && | ||
token.rawName.startsWith('--') && | ||
!token.inlineValue | ||
); | ||
if (badToken) { | ||
throw new Error(`Option value for '${badToken.rawName}' must be inline, like '${badToken.rawName}=VALUE'`); | ||
} | ||
|
||
console.log(values); | ||
|
||
// Try the following: | ||
// node limit-long-syntax.js -f FILE --log=LOG | ||
// node limit-long-syntax.js --file FILE |
This file contains hidden or 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,29 @@ | ||
'use strict'; | ||
|
||
// How might I throw if an option is repeated? | ||
shadowspawn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 1. const { parseArgs } = require('node:util'); // from node | ||
// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package | ||
const { parseArgs } = require('..'); // in repo | ||
|
||
const options = { | ||
ding: { type: 'boolean', short: 'd' }, | ||
beep: { type: 'boolean', short: 'b' } | ||
Eomm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
const { values, tokens } = parseArgs({ options, tokens: true }); | ||
|
||
const seenBefore = new Set(); | ||
tokens.forEach((token) => { | ||
if (token.kind !== 'option') return; | ||
if (seenBefore.has(token.name)) { | ||
throw new Error(`option '${token.name}' used multiple times`); | ||
} | ||
seenBefore.add(token.name); | ||
}); | ||
|
||
console.log(values); | ||
|
||
// Try the following: | ||
// node no-repeated-options --ding --beep | ||
// node no-repeated-options --beep -b | ||
// node no-repeated-options -ddd |
This file contains hidden or 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,35 @@ | ||
// Now might I enforce that two flags are specified in a specific order? | ||
shadowspawn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { parseArgs } from '../index.js'; | ||
|
||
function findTokenIndex(tokens, target) { | ||
return tokens.findIndex((token) => token.kind === 'option' && | ||
token.name === target | ||
); | ||
} | ||
|
||
const experimentalName = 'enable-experimental-options'; | ||
const unstableName = 'some-unstable-option'; | ||
|
||
const options = { | ||
[experimentalName]: { type: 'boolean' }, | ||
[unstableName]: { type: 'boolean' }, | ||
}; | ||
|
||
const { values, tokens } = parseArgs({ options, tokens: true }); | ||
|
||
const experimentalIndex = findTokenIndex(tokens, experimentalName); | ||
const unstableIndex = findTokenIndex(tokens, unstableName); | ||
if (unstableIndex !== -1 && | ||
((experimentalIndex === -1) || (unstableIndex < experimentalIndex))) { | ||
throw new Error(`'--${experimentalName}' must be specified before '--${unstableName}'`); | ||
} | ||
|
||
console.log(values); | ||
|
||
/* eslint-disable max-len */ | ||
// Try the following: | ||
// node ordered-options.mjs | ||
// node ordered-options.mjs --some-unstable-option | ||
// node ordered-options.mjs --some-unstable-option --enable-experimental-options | ||
// node ordered-options.mjs --enable-experimental-options --some-unstable-option |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.