Open FM-96 opened 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:
@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.
Calling this example code:
with these arguments:
node example.js --arr foo 2 bar
gives (as expected) the following output:
Adjusting the parsing options to treat the array as strings:
gives the following output (also as expected):
However, if I tell it to treat the array as numbers:
then I would expect to get all array elements parsed as numbers:
Instead, I get this result:
Notably, this is also the case if all array elements are valid numbers:
node example.js --arr 1 2 3
gives the same result: