tj / commander.js

node.js command-line interfaces made easy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option event listener not firing

shadowspawn opened this issue · comments

Nice to meet you, shadowspawn , and Merry Christmas. I am a Web developer, my name is jasmin

I'd like to ask you a question and hope you can help me answer it.

Please see the bellow codes:

I have installed version 11.1.0 of Commander

npm i commander@11.1.0

And the code is defined as follows:

const commander  = require("commander");
const program = new commander.Command();
program
  .name("test-cli") 
  .usage("<command> [options]")
  .version("1.0.0")
  .option("-tp, --target-path <target-path>", "Whether to specify a local debug file path?", ""); 
program
  .command("init [projectName]")
  .option("-f, --force", "Whether to force  to init the program? ")
  .action(function (projectName, cmdObj, program)  {
  console.log("init", projectName, cmdObj, program.parent.opts());
});
program.on("option: target-path", function(){
  console.log("target path test");
});
program.parse(process.argv);

When I execute the code bellow,

test-cli init --target-path /xxx

Why it can't print the test text "target path test" ?
Can you help with this question?

Originally posted by @jasmin2014 in #1969 (comment)

Close, just need to remove the space from the event name:

program.on("option:target-path", function(){
   ...
}
``

Thank you very much. The problem has been solved.