minimist: Ambiguous behavior
This is confusing
Case 1: as expected
$ node example/parse.js -a1
{ _: [], a: 1 }
Case 2: everything turned into boolean flags
$ node example/parse.js -a1b
{ '1': true, _: [], a: true, b: true }
Case 3: no more flags, just value again
node example/parse.js -a1b2
{ _: [], a: '1b2' }
Case 4: back to booleans
node example/parse.js -a1b2c
{ '1': true, '2': true, _: [], a: true, b: true, c: true }
So, when it ends with a letter - its booleans, and when it ends with a number its value Any thoughts?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (14 by maintainers)
Commits related to this issue
- Fix detection of numeric value when parsing short options (#2) — committed to shadowspawn/minimist by shadowspawn 2 years ago
- Fix detection of numeric value when parsing short options (#2) — committed to shadowspawn/minimist by shadowspawn 2 years ago
- Fix detection of numeric value when parsing short options (#2) — committed to shadowspawn/minimist by shadowspawn 2 years ago
Please open
-a=b=cas a new issue so it can be tracked separately.(For interest, I fixed a similar bug in early iteration of
parseArgs! https://github.com/pkgjs/parseargs/pull/43)A parser has to decide how to process
--foo bar. The default approach in minimist is that options are assumed to take an option-argument if one is available, so this is by default parsed like--foo=bar.The
booleanconfiguration option changes this default behaviour. A boolean option is assumed not to take an option argument, so--foo baris parsed likebar --foo.An argument with an explicit
=like--foo=baris parsed as an option and its value, whatever the configuration.I’m comfortable with the current behaviour as being reasonable, but it will be a while before I can double-check my understanding and put a story together.
With the input configuration specifying both
opts.boolean:trueandopts.stringthe result is almost by definition ambiguous, so that does fit under the issue title of “Ambiguous behaviour”, whether or not it is intended. 😄I think this is by design.
This falls under the non-alphanumeric handling of the first case, and probably as intended.
Not sure this is intentional, but the first character after the
-is assumed to be an option. Likewise for-+.In this case the parsing of
-1as an option is intended behaviour. Utilities can use a digit as a short option. For example:This is tested here: https://github.com/minimistjs/minimist/blob/980d7ac61a0b4bd552711251ac107d506b23e41f/test/short.js#L8
Thank you very much for the assist!