minimistjs / minimist

parse argument options
MIT License
515 stars 30 forks source link

Boolean argument detection is too aggressive #64

Open shadowspawn opened 2 months ago

shadowspawn commented 2 months ago

Copied from original repo. Single comment from @sampsyo with no upvotes: https://web.archive.org/web/20200904203601/https://github.com/substack/minimist/issues/75/


Consider this tiny program that uses a single boolean flag:

var minimist = require('minimist');
console.log(minimist(process.argv.slice(2), {
  boolean: ['f']
}));

It works as expected for most arguments:

$ node foo.js -f xxx
{ _: [ 'xxx' ], f: true }

But, shockingly, if the argument happens to contain the strings "true" or "false" anywhere in the string, then the argument gets reinterpreted as an argument for the -f flag:

$ node foo.js -f xxxtruexxx
{ _: [], f: false }
$ node foo.js -f xxxfalsexxx
{ _: [], f: false }

The value of the -f flag is false in both cases, but the argument mysteriously vanishes from the _ array.

This is especially surprising if, for example, your program works fine until you happen to use a file with "true" in its name. I would expect the argument list to be left alone entirely, regardless of its content, when used with a boolean flag.

Tests run with minimist@1.2.0 using Node v4.2.1 on OS X.

shadowspawn commented 2 months ago

I think this should be covered in the documentation, like has been done for --no-* in #48

ljharb commented 2 months ago

This one seems like it’s worth fixing.