Commander.js: BUG: defining an option which includes `no-` makes the option true by default

Created on 17 Jul 2020  路  4Comments  路  Source: tj/commander.js

Current Behavior -
If we define an option whose name contains no- for e.g. module-no-parse, it makes the option true by default.

Expected Behavior -
It is supposed to do this only for flags with leading no- as mentioned in docs. https://github.com/tj/commander.js/#other-option-types-negatable-boolean-and-flagvalue

You can specify a boolean option long name with a leading no- to set the option value to false when used. Defined alone this also makes the option true by default.

Steps to reproduce -
Define an option whose name includes no- e.g. module-no-parse.
Check its default value in the parser.
Screenshot at 2020-07-16 20-28-38

Addition Info
Bug found via https://github.com/webpack/webpack-cli/issues/1679

bug

All 4 comments

it should be this.negate = flags.indexOf('--no-') !== -1;

Reproduced.

I did a bit of digging to see how the code has evolved. Back in Commander v2.3.0, the -no- support was undocumented and simplistic, and looked for the -no- anywhere in the long option flag. The handling of no- got a big rework in #795 and (mostly) switched to --no- being just at the start of the option flag. The initial test didn't get changed though, and hence the current bug!

Fixed in Commander v6.0.0 which has been released: https://github.com/tj/commander.js/releases/tag/v6.0.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hossamelmansy picture hossamelmansy  路  4Comments

oknoorap picture oknoorap  路  4Comments

winys picture winys  路  5Comments

mathiasbynens picture mathiasbynens  路  3Comments

RoXioTD picture RoXioTD  路  4Comments