kaidez
kaidez

Reputation: 679

Cannot pass an option to a command in commander.js

I'm trying to pass an option to a command that I created with commander.js...

program
 .command('init [options]')
 .description('scaffold the project')
 .option('-b, --build', 'add "build" folder with subfolders')
 .action(function(){
   if(program.build) {
    mkdirp("build/")
  }
 });

program.parse(process.argv);

...where if the -b flag is passed to init, the npm mkdirp module creates a "build" directory. Sadly, I can't get it working...any idea?

Upvotes: 0

Views: 517

Answers (1)

Mike Atkins
Mike Atkins

Reputation: 1591

change program.build to this.build inside the function passed to .action()

program
 .command('init [options]')
 .description('scaffold the project')
 .option('-b, --build', 'add "build" folder with subfolders')
 .action(function(){
   if(this.build) {
    mkdirp("build/")
  }
 });

program.parse(process.argv);

Upvotes: 2

Related Questions