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

Support to know if any known command was processed, or any output written #240

Closed
yanickrochon opened this issue Aug 21, 2014 · 3 comments
Closed

Comments

@yanickrochon
Copy link

When calling program.parse(process.argv);, I would like to know if any command was processed. Meaning, if the program was invoked with either -h, --help, version or any other command.

For example, if I have this setup

program.command('foo')
 ....
;
program.once('foo', function () { console.log('*** Foo invoked!'); });

program.once('version', function () { console.log('*** Version printed');

this will only output *** Foo invoked! if the command foo is specified in the command-line, but will not output *** Version printed! if version is specified.

Bottom line is that, I'd that, right after program.parse(process.argv);, to know whether anything happened (any output, any command processed, etc.) or not. How can I uniformingly do this?

@SomeKittens
Copy link
Collaborator

One possible workaround is to abuse the coercion system:

var fooWasCalled = function (foo) {
  console.log('*** Foo invoked');
  return foo;
};

program
  .option('-f, --foo', fooWasCalled)
  .parse(process.argv);

@yanickrochon
Copy link
Author

So... wrap the command action, then. However it doesn't work for version, and would work for -h, --help option only if overridden.

But, as I mentioned, program.once('foo', ...); works for user defined commands, but not for version.

@yanickrochon
Copy link
Author

I found a solution :

program.on('*', function (args) {
  console.error('Unknown argument :', args[0]);
  console.error();
  process.exit(1);
});

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