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

Commander always executes the first registered command #825

Closed
naoey opened this issue Jul 5, 2018 · 8 comments
Closed

Commander always executes the first registered command #825

naoey opened this issue Jul 5, 2018 · 8 comments

Comments

@naoey
Copy link

naoey commented Jul 5, 2018

I have a small script in which I'm trying to use commander.js for parsing options but I'm having some trouble understanding how its supposed to be used. Looking at the docs I assume I have set it up correctly, but no matter what input I try running it with it always executes the action for the first registered command.

Script:

#!/usr/bin/env node

const program = require('commander');

const build = require('./lib/build');
const init = require('./lib/init');

program
  .version(require('./package.json').version);

program
  .command('build', 'Build packages for iOS and Android')
  .option('-p, --path [path]', 'Path to the plugin project to build', null)
  .option('-o, --output [output]', 'Path to output the build artifacts', './output')
  .option('-f, --force [force]', 'Overwrite any files in the output destination', false)
  .action(build);

program
  .command('init <name>', 'Creates boilerplate for a new plugin')
  .action(init);

program.parse(process.argv);

If I try running the script with ./index.js init, it attempts to invoke the build command. Similarly if I reverse the order of registering the commands and try to invoke build, it executes the init command.

Using the latest commander.js 2.16.0 from npm, running on Ubuntu 16.04 with node v8.11.2 using fish shell. I also tried using bash with the same result.

This is the result of simply logging process.argv:

argv [ '/usr/bin/node',
  '/home/naoey/repositories/plugin-tools/index.js',
  'init' ]
@m-slashe
Copy link

m-slashe commented Jul 6, 2018

The problem seems to happen when you put a description to a command, try to remove them

@naoey
Copy link
Author

naoey commented Jul 6, 2018

Hmm that is indeed the case. Still seems like an issue to be fixed because the second parameter of the function signature does expect an optional description. Will leave the issue open for the maintainers to decide what to do with it.
image

@mpathy
Copy link

mpathy commented Jul 12, 2018

Here, all my options are started.

program.version(package.version);
program.command("init").action( something1() );
program.command("install").action( something2() );
program.command("uninstall").action( something3() );
program.parse(process.argv);

something1() is started, then something2(), ...

I cant figure out why and I already removed my descriptions..

@naoey
Copy link
Author

naoey commented Jul 15, 2018

You are invoking the actions yourself instead of passing them as reference. Try program.command("init").action( something1 ); (notice missing parentheses with your function name)

@dirkkok
Copy link

dirkkok commented Sep 5, 2018

I ran into the same issue today, I now use the description function instead:

program
  .command('init <name>')
  .description('Creates boilerplate for a new plugin')
  .action(init);

@shadowspawn
Copy link
Collaborator

shadowspawn commented Dec 29, 2018

To expand on the answer to the original question...

Adding a description parameter to .command triggers a different style of processing where another script is run rather than .action . This is a common source of confusion! Documented here:

https://github.com/tj/commander.js#git-style-sub-commands

To add a description without switching modes, use .description as described by @dirkkok

@shadowspawn
Copy link
Collaborator

To expand on answer to @mpathy issue:

.action takes a function parameter. See examples:

https://github.com/tj/commander.js#examples

So your code could look like:

program.command("init").action(() => {
    something1();
 });
 program.command("install").action(() => {
    something2();
 });
program.command("uninstall").action(() => {
    something3();
 });

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

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

5 participants