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

Run middlewares even if no commands specified #1329

Closed
nikolay-borzov opened this issue Apr 10, 2019 · 5 comments
Closed

Run middlewares even if no commands specified #1329

nikolay-borzov opened this issue Apr 10, 2019 · 5 comments

Comments

@nikolay-borzov
Copy link

nikolay-borzov commented Apr 10, 2019

Use case: Parse variable number of file paths and remove extra '

app file1.js 'file2.js' file3.js

This doesn't work

yargs
 .middleware(argv => {
   console.log('1', argv._);
   argv._ = argv._.map(path => path.replace(/'/g, ''));
   console.log('2', argv._);
  })
  .parse();

No output

This works

yargs
  .middleware(argv => {
    console.log('3', argv._);
    argv._ = argv._.map(path => path.replace(/'/g, ''));
    console.log('4', argv._);
  })
  .command('$0', 'the default command')
  .parse()

3 [ 'file1.js', ''file2.js'', 'file3.js' ]
4 [ 'file1.js', 'file2.js', 'file3.js' ]

@aorinevo
Copy link
Contributor

aorinevo commented Apr 10, 2019

Hi @nicolay-borzov,

Can you log argv._ immediately before and after reassignment for both examples and share it here?

@nikolay-borzov
Copy link
Author

Updated issue. The problem here is that middleware isn't called in first case

@aorinevo
Copy link
Contributor

Strange, I’m assuming it’s because there are no commands added in the first example and hence no middleware would run.

@bcoe: Interested to know your thoughts on this. Should middleware run even if no commands are added? I’m leaning towards yes.

@bcoe bcoe added the question label Apr 10, 2019
@bcoe
Copy link
Member

bcoe commented Apr 10, 2019

@aorinevo I'd argue it should work if you specify a default command with:

.command('$0')

https://github.com/yargs/yargs/blob/master/docs/advanced.md#default-commands

But that it's beyond the scope of middle-ware to run if no commands are specified; I'm curious if it does work with a default command? would be good to add a test.

@bcoe
Copy link
Member

bcoe commented Nov 22, 2020

Closing in favor of the newer #1351

@bcoe bcoe closed this as completed Nov 22, 2020
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

3 participants