Repository navigation
fix: parser should preserve inner quotes - #407
Merged
bcoe merged 8 commits intoNov 15, 2021
Merged
Conversation
jly36963
marked this pull request as ready for review
July 21, 2021 07:06
…ly36963/yargs-parser into fix-parser-not-preserving-inner-quotes
bcoe
requested changes
Nov 15, 2021
| if (shouldStripQuotes) { | ||
| setArg(m[1], m[2]) | ||
| } else { | ||
| setArg(m[1], stripQuotes(m[2])) |
Member
There was a problem hiding this comment.
Since setArgs calls processValue, could this be simplified to:
setArg(m[1],, m[2], shouldStripQuotes);
Contributor
Author
There was a problem hiding this comment.
shouldStripQuotes should always be forwarded by setArg to processValue, except at this line. In this invocation, shouldStripQuotes should always be true.
I'm not a fan of the solution I have here
I try to keep code purely functional (whenever possible). In order to avoid reading global variables in setArg, I dynamically set the default value for shouldStripQuotes. I don't particularly like that pattern, so I'm down to change it.
bcoe
approved these changes
Nov 15, 2021
Member
|
@jly36963 thanks for the patch 👌 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses: yargs/yargs#1324
Problem
Inner quotes are handled differently by
parse, depending on theargsInputargument's type.tokenizeArgStringwrapsargStringwith extra quotes if it is a string.tokenizeArgStringdoes not do the same forargString, if it is an array.The first code block in
processValueremoves quotes from the value.TLDR
If parse was given a string, extra quotes are added (tokenizeArgString) and then removed (processValue).
If parse was given an array, quotes are removed (processValue). This causes loss of inner quotes.
Example (using tests)
logs for string with inner quotes
logs for array with inner quotes
Solution
I added conditional logic around removing quotes. I feel like my current solution is messy. I would love to hear your thoughts/advice on this. I know you're busy, so no worries if you don't have time. I will try to find a better solution.
There might be follow up work in yargs for handling positionals. The parsed object that gets returned by yargs-parser still has the positional arguments in
argv._,const parsed = this.#shim.Parser.detailed(...)