Skip to content

[Feature] Pass version_option() to Command(params=) #2324

Description

@sla-1

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:

ERROR:'function' object has no attribute 'opts'

My init block looks like this, and if I change click.version_option to just click.options(...) it works just fine.

class CustomCli(click.MultiCommand):
    """Custom click.MultiCommand"""

    def __init__(self, commands_dir, description=None):
        env_option = click.version_option(version=0.1)
        params = [env_option]
        click.MultiCommand.__init__(self, help=description, params=params)
        self.__commands_dir = commands_dir
    
    def list_commands(self, ctx):
       ...
       return x
    
    def get_command(self, ctx, name):
       ...
       return x

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:

  • Python version: 3.8
  • Click version: 8.1.3

Activity

  1. davidism commented on Jul 18, 2022

    @davidism
    Member

    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.

  2. sla-1 commented on Jul 18, 2022

    @sla-1
    Author

    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.

  3. kdeldycke commented on Jul 27, 2022

    @kdeldycke
    Collaborator

    Hey @sla-1, I just had the same issue in my click-extra project, in which I hacked click.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 in params= argument.

    I intend to keep maintaining this, so feel free to rely on click-extra instead of having your own copy in your code. My class-based version_option will be part of the upcoming Click Extra v3.0.0 (currently blocked by #2312).

  4. sla-1 commented on Jul 27, 2022

    @sla-1
    Author

    @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()
    
  5. brandoncazander commented on May 4, 2023

    @brandoncazander

    I ran into this issue as well but with a CommandCollection and I just wanted to point out that you can decorate it with version_option manually 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.
  6. 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
  7. changed the title [-]Can't pass `version_option()` to `Command(params=)`[/-] [+][Feature] Pass `version_option()` to `Command(params=)`[/+] on Jan 5, 2025
  8. Rowlando13 commented on May 29, 2026

    @Rowlando13
    Member

    Based on #3527, I am going to close this. MultiCommand and CommandCollection are also deprecated.

  9. kdeldycke commented on May 29, 2026

    @kdeldycke
    Collaborator

    As discussed in #3527 , we will not extend the @version_option decorator.

    If you need something different than the default, you have to produce your own message. Or use the full-featured @click_extra.version_option decorator from the Click Extra project.

  10. locked as resolved and limited conversation to collaborators on Jul 24, 2026
  11. added
    parsingParsing, parameters, commands, chaining, context
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsingParsing, parameters, commands, chaining, context

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions