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

Should 'command:*' and command('*') be removed from README? #1090

Closed
shadowspawn opened this issue Nov 8, 2019 · 3 comments
Closed

Should 'command:*' and command('*') be removed from README? #1090

shadowspawn opened this issue Nov 8, 2019 · 3 comments

Comments

@shadowspawn
Copy link
Collaborator

shadowspawn commented Nov 8, 2019

We have two examples of using command+* in the README but perhaps not a good pattern to encourage. I wonder whether we should remove them and suggest or provide better alternatives.

// error on unknown commands
program.on('command:*', function () {
  console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
  process.exit(1);
});

This works nicely in some cases, but is not a general solution:

  • an action handler on the program also listens for command:*
  • it is called for every command for which there is not an action handler, which includes all git-style executable commands. Not just for unknown commands. (Using sub-command always invokes *  #809)

It might be an ok pattern for a subset of cases and could expand comments so clearer not a general pattern.

See also: #1088

program
  .command('*')
  .action(function(env){
    console.log('deploying "%s"', env);
  });

This example predates the ability to add arguments to the program using .arguments(), which might make for a tidier example, and more consistent with (sub)command action handlers.

Somewhat related is a request for allowing an action based command as default command #742

@shadowspawn
Copy link
Collaborator Author

I would like to do further investigation and work around 'command:*' actions and handling, but this issue isn't likely to prompt that work. Closing to reduce issue noise.

@Yalhu
Copy link

Yalhu commented Sep 23, 2020

I want to add a listener(do some checking, like version-check) on every command created.
I don't know how to work out, i had to added function on each command 'action'.
Even on certain command listener does't work.

// not work
program.on('command:init', function (operands) {
  console.log('on command: init')
});
program.on('init', function(){
  console.log('on init')
})

Thanks for help!

@shadowspawn
Copy link
Collaborator Author

There is not a hook called before actions. The related open issue is: #1197

  1. command:* is sent when the command name does not match a subcommand, and is a bit like the newer default command.

  2. 'command:init' will be sent when the subcommand init is used. Depending on whether you define the listener before or after the subcommand, your listener will be called before or after the subcommand.

If that does not help, then open a new issue so some visibility on question and answer instead of hidden away here. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants