Repository navigation
[Feature] Pass version_option() to Command(params=) #2324
Description
Activity
You'll get the same issue using it that way with any command, not just a multicommand, because it's a decorator meant to be used on a command callback.
Got it, I guess the best way would just use the
click.Option()and building out that way. Which was the workaround I currently did.Hey @sla-1, I just had the same issue in my
click-extraproject, in which I hackedclick.version_option()to have it print a coloured output of the version.I don't like it, but I ended up duplicating the original
click.version_option()code like you, so I can use it inparams=argument.I intend to keep maintaining this, so feel free to rely on
click-extrainstead of having your own copy in your code. My class-basedversion_optionwill be part of the upcoming Click Extra v3.0.0 (currently blocked by #2312).@kdeldycke Thanks for that! But I ended up passing an option to the MultiCommand() and just defining a simple version callback in the class.
env_option = click.Option( ["--version", "-v"], is_flag=True, callback=self.print_version, expose_value=False, is_eager=True, help="cli version", ) click.MultiCommand.__init__(self, help=description, params=[env_option])def print_version(self, ctx, param, value): _current = current_version() if not value or ctx.resilient_parsing: return click.echo(f"cli {_current}") ctx.exit()I ran into this issue as well but with a
CommandCollectionand I just wanted to point out that you can decorate it withversion_optionmanually to work around this:import click @click.group() def cli1(): pass @click.group() def cli2(): pass if __name__ == "__main__": command_collection = click.CommandCollection(sources=[cli1, cli2]) # Manually decorate collection with version_option version_option = click.version_option() cli = version_option(command_collection) cli()
This yields the desired behavior:
python3 cli.py Usage: cli.py [OPTIONS] COMMAND [ARGS]... Options: --version Show the version and exit. --help Show this message and exit.
- changed the title
[-]Can't pass click.version_option() to click.MultiCommand(params=)[/-][+]Can't pass `version_option()` to `Command(params=)`[/+]on Aug 18, 2023 - changed the title
[-]Can't pass `version_option()` to `Command(params=)`[/-][+][Feature] Pass `version_option()` to `Command(params=)`[/+]on Jan 5, 2025 Based on #3527, I am going to close this. MultiCommand and CommandCollection are also deprecated.
As discussed in #3527 , we will not extend the
@version_optiondecorator.If you need something different than the default, you have to produce your own message. Or use the full-featured
@click_extra.version_optiondecorator from the Click Extra project.- locked as resolved and limited conversation to collaborators
on Jul 24, 2026 - addedparsingParsing, parameters, commands, chaining, contextParsing, parameters, commands, chaining, context
on Aug 8, 2026
Hello,
We're using the click.MultiCommand in a custom class and I was trying to use the built-in
click.version_option()in click, but I would get this error anytime I tried to pass the function option to the MultiCommand class:My init block looks like this, and if I change click.version_option to just click.options(...) it works just fine.
The reusable version_options should be passable to multiclick. If there was a different way use
click.version_option()with MultiCommand please let me know. I couldn't find any documentation on it.Environment: