_mysql()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local options=''

    # Get dash options
    options=$(mysql --help | sed -n -e '/^[ ][ ]*-[^-]/ { s/,.*$//; p; }' | xargs)

    # Get double dash options
    options="$options $(mysql --help | sed -n 's/^[ ]*//; /^-/ {; s/^-[a-zA-Z#?],*[ ]*//; s/[ ].*$//; s/=.*$/=/; y/()./   /; s/\[//; p; }' | sort | uniq | sed -n '2,$ p')"

    # Get database names
    options="$options $(mysql -uroot test -e 'show databases' | sed -n '2,$ p' | xargs)"

    COMPREPLY=( $(compgen -W "$options" -- $cur) )
}

complete -F _mysql mysql

_mysql()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local options=''

    # Get dash options
    options=$(mysql --help | sed -n -e '/^[ ][ ]*-[^-]/ { s/,.*$//; p; }' | xargs)

    # Get double dash options
    options="$options $(mysql --help | sed -n 's/^[ ]*//; /^-/ {; s/^-[a-zA-Z#?],*[ ]*//; s/[ ].*$//; s/=.*$/=/; y/()./   /; s/\[//; p; }' | sort | uniq | sed -n '2,$ p')"

    # Get database names
    options="$options $(mysql -uroot test -e 'show databases' | sed -n '2,$ p' | xargs)"

    COMPREPLY=( $(compgen -W "$options" -- $cur) )
}

complete -o bashdefault -o default -o nospace -F _mysql mysql

Use bash default if no match found
Use bash default if no match found
