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

A coerce function runs again for each alias #76

Closed
deployable opened this issue Jan 4, 2017 · 4 comments
Closed

A coerce function runs again for each alias #76

deployable opened this issue Jan 4, 2017 · 4 comments
Labels

Comments

@deployable
Copy link

deployable commented Jan 4, 2017

The coerce function for an argument with an alias will run multiple times, the initial run and then once more per alias.

I only noticed as I was using the file coercion example and reading a largish file.

let argv = require('yargs')
  .alias('file', 'f2')
  .alias('file', 'f3')
  .coerce('file', function (arg) {
    console.log('reading file', arg)
    return require('fs').readFileSync(arg, 'utf8')
  })
  .argv

then

$ node testyargs.js  --file test
reading file test
reading file test
reading file test

from yargs/yargs#729

@maxrimue maxrimue added the bug label Jan 4, 2017
@maxrimue
Copy link
Member

Hey, thanks again for opening the issue. Thinking about this again, I actually think this is intended behaviour. This is because .coerce() is a method that's meant to modify the value of an option after it has been given. For example, you might have a --number option and whatever value you get, you want to multiply it with 2, then you could do .coerce('number', arg => arg * 2).

This is also why your coercion function was run for each alias. Every alias should hold the same value and just serve it under another name, so a coercion defined for an option also should be applied to all of its aliases.

So finally, this behaviour is intentional, and I think you might prefer another concept to deal with your args. I would propose using commands. Here's an example of how that might look like:

In the cli:

$ myApp read hello.txt
reading file hello.txt

In code:

yargs
  .command('read <file>', // The '<file>' creates a required positional argument that does not need a flag in front of it
    'read a file', {}, argv => {
      console.log('reading file', argv.file);
      // and so on...
    })
  .argv

You can read more about commands here. If you have any more questions or anything else, feel free to ask!

@maxrimue maxrimue removed the bug label Jan 10, 2017
@deployable
Copy link
Author

@maxrimue Sure, but the function doesn't need to run multiple times, just the result needs to be applied to each alias. The underlying structure of the arg/alias processing looks like it makes running the function multiple times easier than "knowing" when an arg and it's aliases have been coerced already. i.e yargs doesn't seem to track an arg and an alias as separate things, they all get put in one big blob together and linked by the alias lookup.

Thanks for the command tip, but this is an option to a command rather than a standalone command itself.

@bcoe bcoe added the bug label Jan 16, 2017
@bcoe
Copy link
Member

bcoe commented Jan 16, 2017

@maxrimue @deployable I agree that this might be worth discussing more, would be nice to potentially only apply the coerce method once.

@bcoe bcoe reopened this Jan 16, 2017
@Delagen
Copy link

Delagen commented Aug 18, 2017

It even run for auto camelCased alias
for example --option-arg => {optionArg:value,"option-arg":value}
coerce function run for both

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

4 participants