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

choices and coerce sequence #756

Open
Alexsey opened this Issue Jan 13, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@Alexsey

Alexsey commented Jan 13, 2017

This code

const argv = require('yargs')
  .choices('user', ['Goofy', 'Miky'])
  .coerce('user', user => {
    switch (user) {
      case 'Goofy': return {
        firstName: 'Mark',
        lastName: 'Pipe'
      }
      case 'Miky': return {
        firstName: 'Elon',
        lastName: 'Stone'
      }
    }
  })

will fail because choices will run after coerce. But should it be this way? Obvious purpose of choices is to validate user input but not generated value. And it would be wary good if we will have an opportunity to validate user input and then build some useful value base on it

@bcoe

This comment has been minimized.

Member

bcoe commented Jan 20, 2017

@Alexsey definitely an edge-case, but I could certainly see an argument for coerce always being applied last ... at the same time, I could see an argument for the other way.

@nexdrew what are your thought son this one, want to play King Solomon on this one?

@hisabimbola

This comment has been minimized.

hisabimbola commented Feb 5, 2017

I am also having this issue now. Can you recommend a workaround for this? @bcoe

@hisabimbola

This comment has been minimized.

hisabimbola commented Feb 5, 2017

and I think my issue is a bit different but related to this

.option('logLevel', {
    alias: 'l',
    default: 'info',
    describe: 'Enable and set log level',
    choices: ['info', 'silly', 'verbose', 'warn', 'error'],
     coerce: (level) => {
       log.level = level
    },
  })

Anytime I run this I get this error

Invalid values:
  Argument: logLevel, Given: undefined, Choices: "info", "silly", "verbose", "warn", "error"
@bcoe

This comment has been minimized.

Member

bcoe commented Feb 5, 2017

@hisabimbola hoping to do some issue triaging tomorrow; I'll put my thinking cap on and try to get you an answer 👍

@hisabimbola

This comment has been minimized.

hisabimbola commented Feb 5, 2017

thanks for your response @bcoe

I was able to get mine to work now, my issue was that I was not returning any value in the coerce method.

Updating the code to this works for me

.option('logLevel', {
    alias: 'l',
    default: 'info',
    describe: 'Enable and set log level',
    choices: ['info', 'silly', 'verbose', 'warn', 'error'],
    coerce: (level) => {
      log.level = level
      return level
    },
  })

thanks, and maybe it fixes @Alexsey issue too

@tkarls

This comment has been minimized.

tkarls commented Feb 22, 2017

Same problem as original poster.

Think it makes most sense to first restrict the options using choices and then after a valid choice run the coerce function.

That way the two features can coexist. Which is impossible now.

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