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

addCommonOption for options common to subcommands #1670

Closed

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Jan 3, 2022

Pull Request

Problem

Sometimes people would like to be able to add common (global) options to all the subcommands, rather than to the program. This was the most popular enhancement in poll #1551 (comment)

Related:

Solution

  • add .addCommonOption()
  • implemented using .copyCommonOptions()
  • following same pattern as .copyInheritedSettings(), and common settings are automatic for .command() but manual for .addCommand()

This is implemented for one level deep. The common options are added to direct subcommands and not sub sub commands, as don't have a good way of identifying leaf vs non-leaf commands at the time they are added.

Why not .commonOption() matching .option()? Because would need more variations to cover .requiredOption() and .addOption().

Example Usage

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

program
  .addCommonOption(new Option('-d, --debug'))
  .addCommonOption(new Option('--quiet'));

program
  .command('sub')
  .option('-l, --local')
  .action(() => {
    console.log('Thanks for calling sub');
  });

program.parse();
$ node index.js help sub
Usage: index sub [options]

Options:
  -d, --debug
  --quiet
  -l, --local
  -h, --help   display help for command

ChangeLog

@shadowspawn
Copy link
Collaborator Author

shadowspawn commented Jan 3, 2022

There is not a load of code in this PR which makes me realise this could be done manually fairly simply. Some of the linked issues use subclasses for implementing, here is a similar direct approach.

// Add common options after setting up program and subcommands
program.commands.forEach((cmd) => {
   cmd.option('-d, --debug');
   cmd.option('--quiet');
});

And with this approach can use .option() or .requiredOption() or .addOption(), or add event listeners, so more flexible than .addCommonOption().

Perhaps I should just add some example files showing how to do it yourself!

@shadowspawn
Copy link
Collaborator Author

This implementation was more limited and less useful than I expected. Lot of scenarios. Considerations are:

  • .option() vs .requiredOption() vs .addOption()
  • listeners for option events
  • separately created commands (added using .addCommand()) should get some control
  • besides .command(), do common options get added for .addCommand() or .createCommand() ?

Current plan is write some examples on how to do it yourself with a simple loop, and maybe a simple subclass.

@shadowspawn shadowspawn deleted the feature/common-options branch November 10, 2023 22:09
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

Successfully merging this pull request may close these issues.

None yet

1 participant