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

Options shared by a root command and its subCommands #1426

Closed
electrotype opened this issue Dec 29, 2020 · 5 comments
Closed

Options shared by a root command and its subCommands #1426

electrotype opened this issue Dec 29, 2020 · 5 comments

Comments

@electrotype
Copy link

I had an issue that took me a long time to figure out and fix. It is about an option that I add to all the commands of my application: -s for "silent". When this option is set, a command will disable all outputs when running.

Since this is a global option that I add to all commands, I created a addGlobalOptions(command) function and I call it for every command.

The issue is that I was also calling addGlobalOptions() on the root Command!

My program looks a bit like:

const commander = new Command();
addGlobalOptions(commander);
addSubCommands(commander);

In addSubCommands, each command has the global options added too, using addGlobalOptions.

By calling addGlobalOptions on the root "commander" Command, a subCommand would always receive undefined for the -s argument, even when specified by the user! By removing the global options on the root "commander" Command, true is received correctly.

I did not find anything in the documentation about this? What is the expected behavior when an option is shared between a root Command and one of its subCommand? Or maybe I'm doing something wrong?

@shadowspawn
Copy link
Collaborator

The root command parses its options from anywhere on the command line, so it consumes the option first before the subcommand. The relevant line in the README is:

Options on the command line are not positional, and can be specified before or after other arguments.

You can add and access a common option on the root command, or add it to all your subcommands. A visible difference to the user is where in the help the option appears. Do you think of it as a "program" option on the root command, or a common option which every subcommand has locally?

There is an open issue with collected reports and research: #1229
and the related issues to your problem are: #598 #1033 #1307

@electrotype
Copy link
Author

Thank you for the explanation.

@shadowspawn
Copy link
Collaborator

Pull Request opened to add .enablePositionalOptions() and .passThroughOptions(): #1427

@shadowspawn
Copy link
Collaborator

Opened poll on possible enhancements to global options: #1551

@shadowspawn
Copy link
Collaborator

Opened a draft PR for .addCommonOption() #1670

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