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

access argv/command from coerce #1266

Open
brewster1134 opened this issue Jan 1, 2019 · 4 comments
Open

access argv/command from coerce #1266

brewster1134 opened this issue Jan 1, 2019 · 4 comments
Labels

Comments

@brewster1134
Copy link

brewster1134 commented Jan 1, 2019

i have a global option defined that all commands share, and need to use coerce for some additional manipulation. the issue is i need to access the argv object, or at least the command called, however only the option is passed in. this is likely a feature request, but maybe there is another way to accomplish what i am trying to do. here is a contrived example...

yargs
  .option 'foo',
    coerce: (foo) ->
      # need to access `build` here

  .command 'build', 'build', ->
    # i want to avoid having to declare a `foo` option here, as well as for every other command
@brewster1134
Copy link
Author

i am also noticing that coerce is called twice for options defined globally... possible bug?

@aorinevo
Copy link
Contributor

aorinevo commented Jan 9, 2019

Hi @brewster1134, middleware gets passed a reference to argv. Perhaps that’s an option?

@BigBlueHat
Copy link
Contributor

@brewster1134 your contrived example is exactly what I'm attempting to do. 😄 It mostly works as the option is set and available later in the command's handlers.

i am also noticing that coerce is called twice for options defined globally... possible bug?

It looks like yargs runs coerce on each alias also--which means if you have configDir as an option, it will run once for that and for config-dir.

However, I'm also noticing lots of excess coercion attempts where its value gets set a second time (with multiple additional runs) when it's used alongside a positional arg... Still trying to sort that out, though...

@BigBlueHat
Copy link
Contributor

However, I'm also noticing lots of excess coercion attempts where its value gets set a second time (with multiple additional runs) when it's used alongside a positional arg... Still trying to sort that out, though...

This problem seemed to have been because I was using [] instead of <> for a positional, and also had added the --configDir [configDir] text to the usage line. Seems there's more magic hiding in the "command" text than I'd realized...

@bcoe bcoe added the question label Sep 10, 2020
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