Repository: spf13/cobra
File: bash_completionsV2.go (Go)

Code before:
        done < <(printf "%%s\n" "${out[@]}")

        local completions=()
        while IFS='' read -r comp; do
            if [ -z "$comp" ]; then
                continue
            fi

            __%[1]s_debug "Original comp: $comp"

            # First properly format the description string which follows a tab character if there is one
            if [[ "$comp" == *$tab* ]]; then
                desc=${comp#*$tab}
                comp=${comp%%%%$tab*}

                # Add the proper number of spaces to align the descriptions
                for ((i = ${#comp} ; i < $longest ; i++)); do
                    comp+=" "
                done
                comp+="  ($desc)"

                # Now escape all spaces
                comp=${comp// /\\ }

                # Also escape single quotes
                comp=${comp//\'/\\\'}
            fi

            __%[1]s_debug "Final comp: $comp"
            completions+=("$comp")
        done < <(printf "%%s\n" "${out[@]}")

        while IFS='' read -r comp; do
            # Although this script should only be used for bash
            # there may be programs that still convert the bash
            # script into a zsh one.  To continue supporting those
            # programs, we do this single adaptation for zsh
            if [ -n "${ZSH_VERSION}" ]; then
                # zsh completion needs --flag= prefix
                COMPREPLY+=("$flagPrefix$comp")
            else
                COMPREPLY+=("$comp")
            fi
        done < <(compgen -W "${completions[*]}" -- "$cur")

        # If there is a single completion left, remove the description text
        if [ ${#COMPREPLY[*]} -eq 1 ]; then
            __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
            comp="${COMPREPLY[0]%%%% *}"
            __%[1]s_debug "Removed description from single completion, which is now: ${comp}"
            COMPREPLY=()

Review comment (security):
You also need to escape the backticks as they can lead to arbitrary code execution and will break the script.

```
comp=${comp//\`/\\\`}
```

Also it would make sense to make sure it doesn't double escape if the command description already contains a escaped version like "\\`" or "\\'"

Code after:
__%[1]s_handle_standard_completion_case() {
    local tab comp
    tab=$(printf '\t')

    local longest=0
    # Look for the longest completion so that we can format things nicely
    while IFS='' read -r comp; do
        # Strip any description before checking the length
        comp=${comp%%%%$tab*}
        # Only consider the completions that match
        comp=$(compgen -W "$comp" -- "$cur")
        if ((${#comp}>longest)); then
            longest=${#comp}
        fi
    done < <(printf "%%s\n" "${out[@]}")

    local completions=()
    while IFS='' read -r comp; do
        if [ -z "$comp" ]; then
            continue
        fi

        __%[1]s_debug "Original comp: $comp"
        comp="$(__%[1]s_format_comp_descriptions "$comp" "$longest")"
        __%[1]s_debug "Final comp: $comp"
        completions+=("$comp")
    done < <(printf "%%s\n" "${out[@]}")

    while IFS='' read -r comp; do
        COMPREPLY+=("$comp")
    done < <(compgen -W "${completions[*]}" -- "$cur")

    # If there is a single completion left, remove the description text
    if [ ${#COMPREPLY[*]} -eq 1 ]; then
        __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
        comp="${COMPREPLY[0]%%%% *}"
        __%[1]s_debug "Removed description from single completion, which is now: ${comp}"
        COMPREPLY=()
        COMPREPLY+=("$comp")
    fi
}

__%[1]s_handle_special_char()
{
    local comp="$1"
    local char=$2
    if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
        local word=${comp%%"${comp##*${char}}"}
        local idx=${#COMPREPLY[*]}
        while [[ $((--idx)) -ge 0 ]]; do
            COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}