Is it possible to display some help message when showing autocomplete candidates?

725 views Asked by At

Some commands have many -x (x can be any English letter) options and it's some times difficult to remember all of their meanings. I can use bash's compgen -W '-a -b -c' to show possible options and I'm wondering if it's possible to also show some help message. Like this:

bash# foo -<TAB><TAB>
-a: This is option a    -b: This is option b
-C: This is option c
bash#
3

There are 3 answers

0
pynexj On BEST ANSWER

I ever did something similar to map some of curl's single char options (like -x) to GNU style --long-options.

This is how it works:

[STEP 101] # cat curl
function _compgen_curl()
{
    local cmd=$1 cur=$2 pre=$3
    local -a options=( \
                       '' --connect-timeout \
                       -k --insecure \
                       -m --max-time \
                       -o --output \
                       -O --remote-name \
                       -u --user \
                       -U --proxy-user
                       -x --proxy \
                       -y --speed-time \
                       -Y --speed-limit \
                     )
    local -a options2=()
    local i short long

    for ((i = 0; i < ${#options[@]}; i += 2)); do
        short=${options[i]}
        long=${options[i+1]}
        if [[ -z $short || -z $long ]]; then
            options2+=( $short$long )
        else
            options2+=( $short,$long )
        fi
    done

    if [[ $cur == - ]]; then
        COMPREPLY=( $( compgen -W "${options2[*]}" -- "$cur" ) )
    elif [[ $cur == --* ]]; then
        COMPREPLY=( $( compgen -W "${options[*]}" -- "$cur" ) )
    fi
}

complete -F _compgen_curl -o bashdefault -o default curl

 

[STEP 102] # . ./curl
[STEP 103] # curl -<TAB><TAB>
--connect-timeout  -o,--output        -u,--user          -y,--speed-time
-k,--insecure      -O,--remote-name   -x,--proxy
-m,--max-time      -U,--proxy-user    -Y,--speed-limit
[STEP 103] # curl -

Not exactly what you asked but you can update it for your own purpose.

(I'm not sure if bash can handle whitespaces in the completion result but at least you can use _ or -. :-)

5
anishsane On

IMO that would be a bad idea.

But if you really want it, you can do it something like this: Note that this code is a bit crude and unless I know your use case, I cannot provide exact answer. However, it should be sufficient to give you a general idea.

Original:

complete -o nospace -F _default_completion my_command

New:

_custom_completion(){
    local cur;
    _get_comp_words_by_ref cur;
    _default_completion
    if [ ${#COMPREPLY[@]} == 1 ]; then return; fi
    local _compreply=()
    local reply_entry
    local description
    for reply_entry in ${COMPREPLY[@]}; do
        description=$(generate_description_from_option "$reply_entry")
        description=$(printf "%${COLUMNS}s" "$reply_entry : $description" )
        _compreply+=$description
    done
    COMPREPLY=(${_compreply[@]})
} && complete -o nospace -F _custom_completion my_command

With this, bash should show one option per line with description in front of it. Of course, you will need to write generate_description_from_option yourself.

0
eadmaster On

ble.sh provides that, besides many other features.