[Python] Bash completion not working after upgrading click version from version 7.0 to 8.1.3

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    Bash completion is working normally with Click 7.0

    Environment:

    • Python version: 3.9.9
    • Click version: 7.0
    • Bash version: 5.1.8
    • Bash-completion:2.11

    This is the code:

    import click
    @click.group(cls=clicommon.AliasedGroup, context_settings=CONTEXT_SETTINGS)
    @click.pass_context
    def cli(ctx):
    """SONiC command line - 'show' command"""

    # Load database config files
    load_db_config()
    ctx.obj = Db()

    @cli.command()
    @click.argument('ipaddress', required=False)
    @click.option('-if', '--iface')
    @click.option('--verbose', is_flag=True, help="Enable verbose output")
    def arp(ipaddress, iface, verbose):
    """Show IP ARP table"""
    cmd = "nbrshow -4"

    if ipaddress is not None:
    cmd += " -ip {}".format(ipaddress)

    if iface is not None:
    if clicommon.get_interface_naming_mode() == "alias":
    if not ((iface.startswith("PortChannel")) or
    (iface.startswith("eth"))):
    iface = iface_alias_converter.alias_to_name(iface)

    cmd += " -if {}".format(iface)

    run_command(cmd, display_cmd=verbose)

    if __name__ == '__main__':
    cli()


    Bash complete config file is like this:

    root@localhost:~# cat /etc/bash_completion.d/show
    _show_completion() {
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
    COMP_CWORD=$COMP_CWORD \
    _SHOW_COMPLETE=complete $1 ) )
    return 0
    }

    complete -F _show_completion -o default show;


    After upgrading to version 8.1.3, the command is OK, but bash completion is not working:

    root@localhost:~# show
    Usage: show [OPTIONS] COMMAND [ARGS]...

    SONiC command line - 'show' command

    Options:
    -h, -?, --help Show this message and exit.

    Commands:
    aaa Show AAA configuration
    acl Show ACL related information
    alarm Show alarm info
    arp Show IP ARP table


    root@localhost:~# show .
    ./ .bash_history .bash_profile .cshrc .docker/ .python_history .viminfo
    ../ .bash_logout .bashrc .curlrc .lesshst .tcshrc


    I have changed the configuration of /etc/bash_completion.d/show, but it is not working for me. I want bash completion to work normally.

    Continue reading...

Compartilhe esta Página