0

I wrote the function to do a dynamic autocompletion for my tool, for the first argument (for example: mytool <run | delete> --other_opt /media/../../).

How can I use the classic path autocompletion for the other args (>2)? I tried COMPREPLY=($(compgen -d -- ${cur})), but it does not work properly.

_my_completions()
{
    local cache_dir='/Cache'
    local cur prev
    local array
    local versions
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case ${COMP_CWORD} in
        1)
            COMPREPLY=($(compgen -W "run  delete" -- ${cur}))
            ;;
        2)
            case ${prev} in
                versions)
                    for file in $cache_dir/*; do
                        if [[ -d $file ]]; then
                            tool=$(basename "$file")
                            array+="${tool}  "
                        fi
                    done
                    compopt -o nospace
                    COMPREPLY=($(compgen -W "$(echo "${array[*]}")" -- "${cur}"))
                    ;;
            esac
            ;;
        *)
            COMPREPLY=($(compgen -d -- ${cur}))
            ;;
    esac
}
complete  -F _my_completions mytool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.