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

Custom command listening does not work of the new version #1517

Closed
Hack-Jay opened this issue May 10, 2021 · 3 comments
Closed

Custom command listening does not work of the new version #1517

Hack-Jay opened this issue May 10, 2021 · 3 comments

Comments

@Hack-Jay
Copy link

const commander = require('commander');
const program = new commander.Command();

  program
        .name(Object.keys(pkg.bin)[0])
        .usage('<command> [options]')
        .version(pkg.version)
        .option('-d, --debug', 'debug mode', false)

   program.on('option:debug', function () {
      // this.debug is undefined
        if (this.debug) {
            process.env.LOG_LEVEL = 'verbose';
        } else {
            process.env.LOG_LEVEL = 'info';
        }
        log.level = process.env.LOG_LEVEL;
        log.verbose('verbose', 'debug mode')
    });
cli -d

Version 7.2.1 does not work, but version 6.2.1 does

@shadowspawn
Copy link
Collaborator

What do you mean by "does not work"? Is it that your listener does not correctly detect the state of the debug option?

There was a major change to the way option values are stored between Commander 6 and 7. By default, the option values are now stored separately and not as properties on the command object.

See Migration Tips: https://github.com/tj/commander.js/releases/tag/v7.0.0

Instead of program.debug, you can access the value as program.opts().debug.

@shadowspawn
Copy link
Collaborator

The PR making the change: #1409

@Hack-Jay
Copy link
Author

Thank you. That solves my problem

program.opts().debug.

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