Skip to content

Context.params isn't setup in the MultiCommand.list_commands method #1574

Open

Description

@polikeiji

Expected Behavior

I can refer parsed options in MultiCommand.list_commands method.

Actual Behavior

I can't. Options aren't initialized in the list_commands.

I'm creating a CLI tool that dynamically loads subcommands from a file. And the location of the file is specified by an option of the CLI tool. I can refer the parsed option in MultiCommand.get_command but I can't refer it in MultiCommand.list_commands method.

I think the list_command method is called before finishing the parse_args method so we can't use parsed options in the list_commands.

echo(ctx.get_help(), color=ctx.color)

for subcommand in self.list_commands(ctx):

I created the following file and I got an empty dictionary object by the logger.debug when I execute the following command.

https://github.com/polikeiji/bkm/blob/master/bkm.py#L94

python -m bkm -f AAA --help

Is this behavior the expected one?

Environment

  • Python version: 3.8.0
  • Click version: 7.1.2

Activity

  1. parabolize commented on Aug 31, 2020

    @parabolize

    Could you provide a shorter example that shows where you are calling MultiCommand.list_commands? I have created a example where it is being called late enough for Context.params to be populated:

    import click
    
    
    class NosyGroup(click.Group):
        def list_commands(self, ctx):
            print(f'Group ctx params: {ctx.params}')
            return super().list_commands(ctx)
    
    
    @click.command(cls=NosyGroup)
    @click.option('-a')
    @click.option('-b')
    @click.pass_context
    def run(ctx, **kwargs):
        click.echo(f'run: {kwargs}')
        click.echo(f'params: {ctx.params}')
    
        click.echo('calling MultiCommand.list_commands:')
        commands = ctx.command.list_commands(ctx)
    
        click.echo(f'commands in Group: {commands}')
    
    
    @run.command()
    @click.option('-x')
    @click.pass_context
    def comm1(ctx, **kwargs):
        click.echo(f'comm1: {kwargs}')
        click.echo(f'params: {ctx.params}')
    
    
    @run.command()
    @click.option('-y')
    @click.pass_context
    def comm2(ctx, **kwargs):
        click.echo(f'comm2: {kwargs}')
        click.echo(f'params: {ctx.params}')

    Output:

    % multi -a foo -b bar comm1 -x bazz
    run: {'a': 'foo', 'b': 'bar'}
    params: {'a': 'foo', 'b': 'bar'}
    calling MultiCommand.list_commands:
    Group ctx params: {'a': 'foo', 'b': 'bar'}
    commands in Group: ['comm1', 'comm2']
    comm1: {'x': 'bazz'}
    params: {'x': 'bazz'}
    
  2. added
    parsingParsing, parameters, commands, chaining, context
    and removed 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