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.
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
util: add tokens to parseArgs #43459
util: add tokens to parseArgs #43459
Changes from all commits
1060df0
ed54c30
fb2cd00
2570047
33dbe57
27d9e4d
454bd33
baccf9b
d51dcb7
f8bc77d
0bd9da6
25cb98c
a097b80
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
kind
property:With the classes exposed on the
parseArgs
export:Then I think you could just document the tokens as classes, similar to
Buffer.blob
, nested under the top levelparseArgs
docs, similar to how the docs are laid out for the JavaScript embedder API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if these are made classes they should definitely expose a
kind
property - it's really awkward to work with tree nodes which lack such a property, which is why pretty much every parser in the world has them. (Similarly, the DOM hasnodeName
in addition to nodes being instances of distinct classes.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did cross my mind as soon as I added the
kind
property. 😄 .The examples of
Blob
andAsyncResource
andAsyncLocalStorage
are all objects that authors will create themselves, with methods and state, where I think a Class adds more value.I'm not against classes as such, but not seeing benefits in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that we could map the interface based approach directly to the TypeScript types we release on DefinitelyTyped.
But I don't feel super strongly, and would rather not block progress on the API (as @aduh95 says, we can edit the docs later).
Reapproving this PR 👍, @aduh95 are you happy as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 expanded
A returned token has two properties which are always defined,
and some other properties which vary depending on the
kind
:kind
{string} One of 'option', 'positional', or 'option-terminator'.index
{number} Index of element inargs
containing token. So thesource argument for a token is
args[token.index]
.An option token has additional parse details
for an option detected in the input
args
:kind
='option'
index
{number} Index of element inargs
containing token.name
{string} Long name of option.rawName
{string} How option used in args, like-f
of--foo
.value
{string | undefined} Option value specified in args.Undefined for boolean options.
inlineValue
{boolean | undefined} Whether option value specified inline,like
--foo=bar
.A positional token has just one additional property with the positional value:
kind
='positional'
index
{number} Index of element inargs
containing token.value
{string} The value of the positional argument in args (i.e.args[index]
).An option-terminator token has only the base properties:
kind
='option-terminator'
index
{number} Index of element inargs
containing token.Old compact
The returned tokens have properties describing:
kind
{string} One of 'option', 'positional', or 'option-terminator'.index
{number} Index of element inargs
containing token. So thesource argument for a token is
args[token.index]
.name
{string} Long name of option.rawName
{string} How option used in args, like-f
of--foo
.value
{string | undefined} Option value specified in args.Undefined for boolean options.
inlineValue
{boolean | undefined} Whether option value specified inline,like
--foo=bar
.value
{string} The value of the positional argument in args (i.e.args[index]
).(Duplicate post of: pkgjs/parseargs#129 (comment))