-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
util: add tokens to parseArgs #43459
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
Changes from 1 commit
1060df0
ed54c30
fb2cd00
2570047
33dbe57
27d9e4d
454bd33
baccf9b
d51dcb7
f8bc77d
0bd9da6
25cb98c
a097b80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1056,18 +1056,16 @@ changes: | |
| * `allowPositionals` {boolean} Whether this command accepts positional | ||
| arguments. | ||
| **Default:** `false` if `strict` is `true`, otherwise `true`. | ||
| * `tokens` {boolean} Return the | ||
| parsed tokens. This is useful for extending the built-in behaviour, | ||
| from adding additional checks through to reprocessing the tokens | ||
| in different ways. | ||
| * `tokens` {boolean} Return the parsed tokens. This is useful for extending | ||
| the built-in behavior, from adding additional checks through to reprocessing | ||
| the tokens in different ways. | ||
| **Default:** `false`. | ||
|
|
||
| * Returns: {Object} The parsed command line arguments: | ||
| * `values` {Object} A mapping of parsed option names with their {string} | ||
| or {boolean} values. | ||
| * `positionals` {string\[]} Positional arguments. | ||
| * `tokens` {Object} Array of parsed tokens with detailed information | ||
| (see below for more). Only returned if `config` includes `tokens: true`. | ||
| * `tokens` {Object\[]|undefined} See [`parseArgs`' `tokens`][] section. | ||
|
|
||
| Provides a higher level API for command-line argument parsing than interacting | ||
| with `process.argv` directly. Takes a specification for the expected arguments | ||
|
|
@@ -1113,10 +1111,11 @@ console.log(values, positionals); | |
| // Prints: [Object: null prototype] { foo: true, bar: 'b' } [] | ||
| ``` | ||
|
|
||
| ### `parseArgs`' `tokens` | ||
|
|
||
| Detailed parse information is available for adding custom behaviours by | ||
| specifying `tokens: true` in the configuration. | ||
| The returned tokens have | ||
| properties describing: | ||
| The returned tokens have properties describing: | ||
|
|
||
| * all tokens | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a thought, I would be tempted to make tokens a class rather than a POJO with a class Token {}
class OptionTerminator extends Token {}
class PositionalToken extends Token {}With the classes exposed on the Then I think you could just document the tokens as classes, similar to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if these are made classes they should definitely expose a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. I think it might be worth the class based approach, so that people could opt to check instanceof, if they're so inclined? Should we consider making kind a Symbol, or is a string preferable?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strings are traditional - they're a lot easier to work with, since you don't have to import them.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It did cross my mind as soon as I added the The examples of I'm not against classes as such, but not seeing benefits in this case.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My 2 cents is we should go with the first one and make it less verbose, the "existing" version can be a bit confusing I think. But feel free to disagree, none option is perfect anyway, and we can always edit the docs later if we find a better layout.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely find the existing one massively clearer; muddling things up with unnecessary TS/Java-ish terms and structure makes things more confusing.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mixed opinions straight away! Thanks both for feedback. I will leave it as is, barring further feedback.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compact POJO description is a bit subtle to read. How about an expanded version with all the properties listed? Proposed expandedA returned token has two properties which are always defined,
An option token has additional parse details
A positional token has just one additional property with the positional value:
An option-terminator token has only the base properties:
Old compactThe returned tokens have properties describing:
(Duplicate post of: pkgjs/parseargs#129 (comment)) |
||
| * `kind` { string } One of 'option', 'positional', or 'option-terminator'. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.