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

Sub Commands with the same option as the parent comand do not receive the options #1824

Closed
jmcdo29 opened this issue Nov 23, 2022 · 2 comments

Comments

@jmcdo29
Copy link

jmcdo29 commented Nov 23, 2022

Command and Sub Command with Same Option

Current Behavior

If you have a command and a sub command with the same option, then when you
invoke program command subcommand -o option the subcommand will not receive
the option.

Expected Behavior

When you invoke program command subcommand -o option the subcommand recieves
the options passed, just like the command does.

Reproduction Repository

https://github.com/jmcdo29/commander-sub-command-same-option-name

Reproduction Steps

  1. Clone https://github.com/jmcdo29/commander-sub-command-same-option-name
  2. Run your package manager install of choice e.g. pnpm i
  3. Run node index.js say -m hello to see the main command output
  4. Run node index.js say yellow -m hello to see the sub command output
  5. Observe that the sub command does not have any options passed to it

To see that the sub command does work, you can run the command
node main.js say yellow -y hello.

Commander Info

version: 9.4.1

Related Issue

@shadowspawn
Copy link
Collaborator

Thanks for the detailed description and reproduction.

The default behaviour is that options can come before or after command-arguments, and before or after subcommands. So the first command is eating the option before the subcommand parsing starts.

You can change this behaviour with .enablePositionalOptions(). See the README for some additional description and link to an example file.

(Sometimes this comes up when the top-level command and options are not really hierarchical but a shortcut to the most common use. In this case the command can be made a default command, and the options no longer get used at two levels. See #1616. )

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

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