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

First argument is lost when variadic arg is explicitly specified #1637

Closed
Benjin opened this issue May 1, 2020 · 5 comments
Closed

First argument is lost when variadic arg is explicitly specified #1637

Benjin opened this issue May 1, 2020 · 5 comments
Assignees
Labels

Comments

@Benjin
Copy link

Benjin commented May 1, 2020

let argv = require('yargs')
	.command('$0 [foods...]')
	.help().wrap(null).version(false).argv;

console.log(argv);

Arg not explicitly specified (good output):

PS F:\Source\scripts> node trial.js apples carrots
{ _: [], '$0': 'trial.js', foods: [ 'apples', 'carrots' ] }

Arg explicitly specified (bad output, expected to match above):

PS F:\Source\scripts> node trial.js --foods apples carrots
{ _: [], foods: [ 'carrots' ], '$0': 'trial.js' }
@mleguen
Copy link
Member

mleguen commented May 6, 2020

Also note that:

PS F:\Source\scripts> node trial.js --foods apples --foods carrots
{ _: [], '$0': 'trial.js', foods: [ 'apples', 'carrots' ] }

So:

  • either positionals should overwrite options, and foods should be empty here
  • or they should be concatenated and foods should equal [apple, carrots] in @Benjin 's 2nd example

@mleguen mleguen added the bug label May 6, 2020
@bcoe bcoe added the p1 label May 6, 2020
@bcoe
Copy link
Member

bcoe commented May 6, 2020

thanks for the bug report, definitely seems like something we should address.

@Benjin
Copy link
Author

Benjin commented May 6, 2020

Also note that:

PS F:\Source\scripts> node trial.js --foods apples --foods carrots
{ _: [], '$0': 'trial.js', foods: [ 'apples', 'carrots' ] }

So:

* either positionals should overwrite options, and `foods` should be empty here

* or they should be concatenated and `foods` should equal `[apple, carrots]` in @Benjin 's 2nd example

Wouldn't "overwriting options" mean foods would just be 'carrots' in that example?

Should it even be valid for a parameter to be specified twice? I think I'd expect an error in that case, because otherwise we're guessing at the user's intention, as I think both concatenation and overwriting are equally iffy.

@jly36963
Copy link
Contributor

jly36963 commented Jan 3, 2022

I believe my fix was released, can you use yargs 17.3.1 and confirm that it solves the issue you reported?

The solution I chose is as follows:

  • A user provides options and positionals under the same namespace
  • If neither the option or the positional is array/variadic type, overwrite.
  • If at least one is array/variadic, combine.

NOTE: The fix had a side-effect that concerns using array/variadic options/positionals in combination with yargs.config(). If you come across that behavior, I'm aware of it and currently working on a fix for it.

@jly36963 jly36963 self-assigned this Jan 3, 2022
@shadowspawn
Copy link
Member

This issue has been resolved. Thanks for comments, and PR from @jly36963

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

No branches or pull requests

5 participants