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

Global options that are defined after commands are not global #776

Open
krlwlfrt opened this issue Dec 22, 2024 · 1 comment
Open

Global options that are defined after commands are not global #776

krlwlfrt opened this issue Dec 22, 2024 · 1 comment

Comments

@krlwlfrt
Copy link

If I define options as global (either via .globalOption or .option(..., {global: true}) they just apply/are global to the last command that was defined.

If I use .reset() after every command, I can define truly global options again.

Please see this gist and the comment on it for an example and it's output: https://gist.github.com/krlwlfrt/e6e60117ef0e1e41d1dbbf7042928789

Is this a bug or by design? If it is by design, you could maybe add disclaimer to the docs what "global" means, or that one should use .reset() every so often.

@c4spar
Copy link
Owner

c4spar commented Dec 25, 2024

This is working as expected. The command class is a chainable command factory. Adding an option after adding a new command with the command method, will register the option on the new sub command instead of the main command.

new Command()
  // Add option to main command:
  .option('-f, --foo', '...')
  // Add sub command
  .command('foo')
  // Add option to foo command:
  .option('-b, --bar', '...')

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