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

Adding options after commands have been created #1631

Closed
mikemilano opened this issue Oct 24, 2021 · 2 comments
Closed

Adding options after commands have been created #1631

mikemilano opened this issue Oct 24, 2021 · 2 comments

Comments

@mikemilano
Copy link

Is there a way to add options to commands after they have been created?

In this app, I'm separating commands into their own files and dynamically requiring them. I'd like to go back through and add global options to each of the commands that exists. Is this possible?

I've tried simply appending the options array of each command with a new Option but the option doesn't show when I run help on the command.

@shadowspawn
Copy link
Collaborator

No problem adding options, but use a Command method rather than manually adding to the options property. e.g.

const { program } = require('commander');

program.command('a');
program.command('b');

program.commands.forEach((cmd) => cmd.option('-e, --example'));

program.parse();
% node index.js help b
Usage: index b [options]

Options:
  -e, --example
  -h, --help     display help for command

@mikemilano
Copy link
Author

Thanks a lot John, that did it!

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