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

Is it possible to create a command that only runs when no positional argument is specified? #1621

Open
cameronus opened this issue Apr 12, 2020 · 5 comments
Labels

Comments

@cameronus
Copy link

I am attempting to offer the following commands:

cli-tool
cli-tool import
cli-tool clean

I can achieve the functionality of the first command (with no positional argument specified) using the default command handler, but this means it is also accessible using any other random positional arguments, like:

cli-tool foobar

This would also trigger the default command handler, instead of an error warning the user that this command does not exist (which I would like). Would it be possible to achieve the behavior I am seeking?

@aorinevo
Copy link
Contributor

aorinevo commented Apr 12, 2020

I think you can leverage the middlewares api to achieve this. That is, you can bind a middleware to a specific command and have it execute whatever logic you want given the desired conditions (i.e. no positional arguments).

In a similar fashion, but with obvious performance trade-offs, you can bind a middleware function to every cli command.

There might be other approachS to achieve the desired outcome but this I think will satisfy the requirements.

@cameronus
Copy link
Author

cameronus commented Apr 12, 2020

@aorinevo I'm not sure how middleware could be used here. If you mean that I could manually handle all of the commands and options through the middleware functions, then yargs' argument parsing capabilities aren't being utilized and I might as well do everything manually. Would there be any way to handle only cases in which no positional arguments are specified?

@mleguen
Copy link
Member

mleguen commented Apr 14, 2020

@cameronus What about using .strict()? It would cause cli-tool foobar to fail because cli-tool would have too many positionals (0 expected, 1 provided).

@cameronus
Copy link
Author

@mleguen Unfortunately .strict() doesn't work per my testing. I could be implementing it incorrectly, so I will provide the code I am using.

require('yargs')
  .strict()
  .command('$0', 'first command', () => {}, (argv) => {
    console.log('first command')
  })
  .command('test', 'second command', () => {}, (argv) => {
    console.log('second command')
  })
  .argv

Running cli-tool foobar still yields first command and not a positional argument error.

@mleguen
Copy link
Member

mleguen commented Apr 15, 2020

@cameronus

Unfortunately .strict() doesn't work per my testing

Well, it should. I'm fixing this.

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

3 participants