minimistjs / minimist

parse argument options
MIT License
550 stars 30 forks source link

Fix short option with an embedded `=` later in the argument #6

Closed shadowspawn closed 1 year ago

shadowspawn commented 2 years ago

Problem

A short option with an embedded = later in the argument is parsed inconsistently and with data loss.

In particular:

-a=b=c gives same result as -a=b (lost trailing =c)

-ab=c gives same result as -a=c (lost leading b)

Fixes #5

Solution

Make code more explicit and test for = at start when looking for possible value.

-a=b=c makes option a have value b=c.

-ab=c makes option a true and option b have value c.

(In my first commit I made -ab=c give option b the value =c by only treating the = as special at the start. I checked Yargs and found it set value to c. I can see a rationale for the parsing to treat it like an expansion, so -ab=c => -a -b=c, and option b has value c. More comfortable matching Yargs with this behaviour.)

codecov-commenter commented 1 year ago

Codecov Report

Merging #6 (b62a195) into main (3124ed3) will not change coverage. The diff coverage is 100.00%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##             main       #6   +/-   ##
=======================================
  Coverage   98.75%   98.75%           
=======================================
  Files           1        1           
  Lines         161      161           
  Branches       71       71           
=======================================
  Hits          159      159           
  Misses          2        2           
Impacted Files Coverage Δ
index.js 98.75% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

shadowspawn commented 5 months ago

For historical interest, this was reported as an issue on the original Minimist repo by @poppinlp:

Seems parse error for this case https://web.archive.org/web/20200904203628/https://github.com/substack/minimist/issues/154/


I've run the code like node index.js -a="aaa=bbb" --a2="aaa=bbb" And here are the output for process.argv, minimist parsed result and yargs-parser parsed result:

origin argv [
  'blabla/.nvm/versions/node/v14.7.0/bin/node',
  'blabla not important',
  '-a=aaa=bbb',
  '--a2=aaa=bbb'
]
minimist { _: [], a: 'aaa', a2: 'aaa=bbb' }
yargs parser { _: [], a: 'aaa=bbb', a2: 'aaa=bbb' }

You could see, for -a, minimist seems to get the wrong result.