apt 补全不适用于双破折号 (--) 选项

apt 补全不适用于双破折号 (--) 选项

在 Linux mint 上运行 gnome-terminal,一些自动完成功能自安装以来一直无法工作。有趣的是,像apt itabinsertsapt installapt utabshown这样的命令unhold update upgrade按预期进行。同样,apt install wgtab显示wgaelic wgalician-minimos wgerman-medical wget wget2 wget2-dev.

然而apt list --itab 才不是自动完成 to apt list --installed,这就是我想要的。

所以,我认为问题出在我的/usr/share/bash-completion/completions/apt文件上,如下所示:

# Debian apt(8) completion                             -*- shell-script -*-

_apt()
{
    local sourcesdir="/etc/apt/sources.list.d"
    local cur prev words cword
    _init_completion || return

    local GENERIC_APT_GET_OPTIONS='
        -d --download-only
        -y --assume-yes
        --assume-no
        -u --show-upgraded
        -m --ignore-missing
        -t --target-release
        --download
        --fix-missing
        --ignore-hold
        --upgrade
        --only-upgrade
        --allow-change-held-packages
        --allow-remove-essential
        --allow-downgrades
        --print-uris
        --trivial-only
        --remove
        --arch-only
        --allow-unauthenticated
        --allow-insecure-repositories
        --install-recommends
        --install-suggests
        --no-install-recommends
        --no-install-suggests
        --fix-policy
    '

    # see if the user selected a command already
    local COMMANDS=(
        "list"
        "search"
        "show" "showsrc"
        "install" "remove" "purge" "autoremove" "autopurge"
        "update"
        "upgrade" "full-upgrade" "dist-upgrade"
        "edit-sources"
        "help"
        "source" "build-dep"
        "clean" "autoclean"
        "download" "changelog"
        "moo"
        "depends" "rdepends"
        "policy")

    local command i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${COMMANDS[@]} =~ ${words[i]} ]]; then
            command=${words[i]}
            break
        fi
    done

    # Complete a -t<SPACE><TAB>
    case $prev in
        -t|--target-release)
            COMPREPLY=( $( compgen -W "$(apt-cache policy | egrep -o 'a=[^,]*|n=[^,]*' | cut -f2- -d= | sort -u)" -- "$cur" ) )
            return 0
            ;;
    esac

    # supported options per command
    if [[ "$cur" == -* ]]; then
        case $command in
            install|remove|purge|upgrade|dist-upgrade|full-upgrade|autoremove)
                COMPREPLY=( $( compgen -W '--show-progress
                  --fix-broken --purge --verbose-versions --auto-remove
                  -s --simulate --dry-run
                  --download
                  --fix-missing
                  --fix-policy
                  --ignore-hold
                  --force-yes
                  --trivial-only
                  --reinstall --solver
                  -t --target-release'"$GENERIC_APT_GET_OPTIONS" -- "$cur" ) )
                return 0
                ;;
            update)
                COMPREPLY=( $( compgen -W '--list-cleanup
                  --print-uris
                  --allow-insecure-repositories
                  ' -- "$cur" ) )
                return 0
                ;;
            list)
                COMPREPLY=( $( compgen -W '--installed --upgradable 
                  --manual-installed
                  -v --verbose
                  -a --all-versions
                  -t --target-release
                  ' -- "$cur" ) )
                return 0
                ;;
            show)
                COMPREPLY=( $( compgen -W '-a --all-versions
                  ' -- "$cur" ) )
                return 0
                ;;
            depends|rdepends)
                COMPREPLY=( $( compgen -W '-i
                    --important
                    --installed
                    --pre-depends
                    --depends
                    --recommends
                    --suggests
                    --replaces
                    --breaks
                    --conflicts
                    --enhances
                    --recurse
                    --implicit' -- "$cur" ) )
                return 0
                ;;
            search)
                COMPREPLY=( $( compgen -W '
                    -n --names-only
                    -f --full' -- "$cur" ) )
                return 0
                ;;
            showsrc)
                COMPREPLY=( $( compgen -W '
                    --only-source' -- "$cur" ) )
                return 0
                ;;
            source)
                COMPREPLY=( $( compgen -W '
                    -s --simulate --dry-run
                    -b --compile --build
                    -P --build-profiles
                    --diff-only --debian-only
                    --tar-only
                    --dsc-only
                    -t --target-release
                    '"$GENERIC_APT_GET_OPTIONS" -- "$cur" ) )
                return 0
                ;;
            build-dep)
                COMPREPLY=( $( compgen -W '
                    -a --host-architecture
                    -s --simulate --dry-run
                    -P --build-profiles
                    -t --target-release
                    --purge --solver
                    '"$GENERIC_APT_GET_OPTIONS" -- "$cur" ) )
                return 0
                ;;
            moo)
                COMPREPLY=( $( compgen -W '
                    --color
                    ' -- "$cur" ) )
                return 0
                ;;
            clean|autoclean)
                COMPREPLY=( $( compgen -W '
                    -s --simulate --dry-run
                    ' -- "$cur" ) )
                return 0
                ;;
        esac
    fi

    # specific command arguments
    if [[ -n $command ]]; then
        case $command in
            remove|purge|autoremove)
                if [[ -f /etc/debian_version ]]; then
                    # Debian system
                    COMPREPLY=( $( \
                        _xfunc dpkg _comp_dpkg_installed_packages $cur ) )
                else
                    # assume RPM based
                    _xfunc rpm _rpm_installed_packages
                fi
                return 0
                ;;
            show|list|download|changelog|depends|rdepends)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) )
                return 0
                ;;
            install)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) )
                if [[ "$cur" == ./* || "$cur" == /* ]]; then
                    _filedir "deb"
                fi
                return 0
                ;;
            source|build-dep|showsrc|policy)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) $( apt-cache dumpavail | \
                    command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) )
                return 0
                ;;
            edit-sources)
                COMPREPLY=( $( compgen -W '$( command ls $sourcesdir )' \
                    -- "$cur" ) )
                return 0
                ;;
            moo)
                COMPREPLY=( $( compgen -W 'moo' \
                    -- "$cur" ) )
                return 0
                ;;
        esac
    fi

    # no command yet, show what commands we have
    if [ "$command" = "" ]; then
        COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur" ) )
    fi

    return 0
} &&
complete -F _apt apt

# ex: ts=4 sw=4 et filetype=sh

其他所有内容似乎来源正确,我尝试取消注释

# enable bash completion in interactive shells
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

在两者中/etc/bash.bashrc,但这~/.bashrc也没有解决问题。

答案1

只需查看行为并推断功能即可。

apt somecommand --得到完成而不是apt --, whileapt-get --有效。原因是apt合并了不同命令的功能,其中apt-getapt-cache。完成取决于子命令,因此选项应该出现在子命令之后是有意义的。

例子:

$ apt install --
--allow-change-held-packages   --install-recommends
--allow-downgrades             --install-suggests
--allow-insecure-repositories  --no-install-recommends
--allow-remove-essential       --no-install-suggests
--allow-unauthenticated        --only-upgrade
--arch-only                    --print-uris
--assume-no                    --purge
--assume-yes                   --reinstall
--auto-remove                  --remove
--download                     --show-progress
--download-only                --show-upgraded
--dry-run                      --simulate
--fix-broken                   --solver
--fix-missing                  --target-release
--fix-policy                   --trivial-only
--force-yes                    --upgrade
--ignore-hold                  --verbose-versions
--ignore-missing               

但:

$ apt search --
--full        --names-only  

相关内容