Trying to write an ssh autocomplete. I want an selection such as the following to be converted to the one after ->. After much googling couldn't find any how to replace already generated words.
ex.
ssh stage 1 -> ssh usstageserver001
ssh prod 3 -> ssh usprodserver003

In short auto-completion of 'ssh env machine' works. I want to replace 'ssh env machine' with 'ssh env-machine-in-one-word'.

Code:
_ssh()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="stage prod"

    case "${prev}" in
        stage)
            COMPREPLY=( $(compgen -W "1 2" -- ${cur}) )
            return 0;
            ;;
        prod)
            COMPREPLY=( $(compgen -W "1 2 3 4 5" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac

    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
}

complete -F _ssh ssh