Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FM-96 opened this issue Sep 27, 2018 · 1 comment
Open

Comments

@FM-96
Copy link

FM-96 commented Sep 27, 2018

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
Copy link
Member

bcoe commented Dec 25, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants