yargs / yargs-parser

:muscle: the mighty option parser used by yargs
http://yargs.js.org/
ISC License
492 stars 119 forks source link

Setting an argument to both array and number gives unexpected results #135

Open FM-96 opened 5 years ago

FM-96 commented 5 years ago

Calling this example code:

const yargsParser = require('yargs-parser');

const argv = yargsParser(process.argv.slice(2), {
    array: ['arr'],
});

console.log(JSON.stringify(argv, null, 2));

with these arguments:

node example.js --arr foo 2 bar

gives (as expected) the following output:

{
  "_": [],
  "arr": [
    "foo",
    2,
    "bar"
  ]
}

Adjusting the parsing options to treat the array as strings:

const yargsParser = require('yargs-parser');

const argv = yargsParser(process.argv.slice(2), {
    array: ['arr'],
    string: ['arr'],
});

console.log(JSON.stringify(argv, null, 2));

gives the following output (also as expected):

{
  "_": [],
  "arr": [
    "foo",
    "2",
    "bar"
  ]
}

However, if I tell it to treat the array as numbers:

const yargsParser = require('yargs-parser');

const argv = yargsParser(process.argv.slice(2), {
    array: ['arr'],
    number: ['arr'],
});

console.log(JSON.stringify(argv, null, 2));

then I would expect to get all array elements parsed as numbers:

{
  "_": [],
  "arr": [
    null,
    2,
    null
  ]
}

Instead, I get this result:

{
  "_": [],
  "arr": [
    null
  ]
}

Notably, this is also the case if all array elements are valid numbers:

node example.js --arr 1 2 3

gives the same result:

{
  "_": [],
  "arr": [
    null
  ]
}
bcoe commented 5 years ago

@FM-96 at this time we don't yet support typed arrays, I think this would be a pretty neat enhancement however and is worth thinking about supporting.