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

Help not playing nicely with exitOverride and parseAsync #1770

Closed
slackorama opened this issue Jul 21, 2022 · 2 comments
Closed

Help not playing nicely with exitOverride and parseAsync #1770

slackorama opened this issue Jul 21, 2022 · 2 comments

Comments

@slackorama
Copy link

I'm trying to output help from the code and not have my script die, but I'm running into the following via node run.js help run:

Code:

const { Command } = require('commander');
const program = new Command('test');

async function run() { process.stdout.write('i am running') }

async function main() {
  program
        .command('run')
        .exitOverride()
        .action(run);
  await program.parseAsync(process.argv);
}
try {
    main()
} catch (e) {
    console.log(e);
}

I get the following error:

sage: test run [options]

Options:
  -h, --help  display help for command
/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:449
      this._exitCallback(new CommanderError(exitCode, code, message));
                         ^

CommanderError: (outputHelp)
    at Command._exit (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:449:26)
    at outputHelpIfRequested (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:2096:9)
    at Command._parseCommand (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:1247:5)
    at /Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:1063:27
    at Command._chainOrCall (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:1159:12)
    at Command._dispatchSubcommand (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:1059:23)
    at Command._parseCommand (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:1236:19)
    at Command.parseAsync (/Users/foobar/projects/crypto-slack-ops/node_modules/commander/lib/command.js:917:16)
    at main (/Users/foobar/projects/crypto-slack-ops/src/run.js:11:17)
    at Object.<anonymous> (/Users/foobar/projects/crypto-slack-ops/src/run.js:14:1) {
  code: 'commander.helpDisplayed',
  exitCode: 0,
  nestedError: undefined
}

Node.js v18.6.0```
@shadowspawn
Copy link
Collaborator

If you want to turn process.exit() into throws, I recommend you call program.exitOverride() before adding any commands, so they all get the setting.

However, the most direct issue is you need a different pattern to unify catching throws from async routines. Try this and ask more questions about Commander behaviour if needed:

main().then(() => {
  console.log(`async success`)
}).catch((e) => {
  console.log(`async reject: error code is ${e.code}`);
});

@slackorama
Copy link
Author

thanks! that fixed my issue.

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