_mole_completions()
{
    local cur_word prev_word subcommand
    cur_word="${COMP_WORDS[$COMP_CWORD]}"
    prev_word="${COMP_WORDS[$COMP_CWORD-1]}"
    subcommand="${COMP_WORDS[1]}"

    if [ "$COMP_CWORD" -eq 1 ]; then
        COMPREPLY=( $(compgen -W "clean uninstall optimize analyze status purge installer touchid completion update remove help version" -- "$cur_word") )
    else
        case "$subcommand" in
            clean)
                case "$prev_word" in
                    --external)
                        COMPREPLY=( $(compgen -d -- "$cur_word") )
                        ;;
                    *)
                        COMPREPLY=( $(compgen -W "--dry-run -n --external --whitelist --debug --help -h" -- "$cur_word") )
                        ;;
                esac
                ;;
            analyze|analyse)
                if [[ "$cur_word" == -* ]]; then
                    COMPREPLY=( $(compgen -W "--json --help -h" -- "$cur_word") )
                else
                    COMPREPLY=( $(compgen -f -- "$cur_word") )
                fi
                ;;
            purge)
                COMPREPLY=( $(compgen -W "--paths --dry-run -n --include-empty --debug --help -h" -- "$cur_word") )
                ;;
            completion)
                COMPREPLY=( $(compgen -W "bash zsh fish" -- "$cur_word") )
                ;;
            *)
                COMPREPLY=()
                ;;
        esac
    fi
}

complete -F _mole_completions mole mo
