fix: parse short option with trailing equals and empty value (-o=) as empty string - #541
Open
youdie006 wants to merge 1 commit into
Open
fix: parse short option with trailing equals and empty value (-o=) as empty string#541youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
… empty string
A short option written with a trailing '=' and no value (e.g. -o=) mis-parsed:
the '=' became a standalone key and the option was set to boolean true instead
of an empty string. The equivalent long option (--opt=) already yields '',
which is the tell that this is short-option-only.
In the short-option branch, letters = arg.slice(1, -1).split('') strips the
trailing '=', so the letters[j + 1] === '=' guard never matches for -o=.
next (arg.slice(j + 2)) equals exactly '=' only on the final letter when the
arg ends in a trailing '='; adding that as an alternative trigger routes -o=
through the existing '='-handling branch where value resolves to ''.
Fixes yargs#367
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.
Fixes #367
Problem
A short option written with a trailing
=and no value mis-parses. The=is treated as a standalone key and the option becomes booleantrue:The equivalent long option already behaves correctly, which is the tell that this is a short-option-only bug:
Root cause
In the short-option branch of
lib/yargs-parser.ts,letters = arg.slice(1, -1).split('')strips the trailing=. For-o=that leavesletters = ['o'], so theletters[j + 1] === '='guard never matches. Parsing falls through tosetArg('o', defaultValue('o'))(booleantrue) and thenkey = arg.slice(-1)[0]picks the trailing=up as a spurious key.Fix
nextis already computed asarg.slice(j + 2)and equals exactly'='only on the final letter when the arg ends in a trailing=. Adding that as an alternative trigger routes-o=through the existing=-handling branch, wherevalue = arg.slice(j + 3)correctly resolves to'':Non-empty values (
-o=x) are unaffected -- they still match the originalletters[j + 1] === '='condition.Tests
Added a case next to the existing empty-value short-option test covering both
-o=(empty) and the-o=xcontrol. Full suite: 363 passing, lint clean.This change was prepared with AI assistance and reviewed by the author.