显示自动完成候选者时是否可以显示一些帮助消息? [英] Is it possible to display some help message when showing autocomplete candidates?

查看:114
本文介绍了显示自动完成候选者时是否可以显示一些帮助消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些命令具有许多-x(x可以是任何英文字母)选项,有时很难记住它们的所有含义.我可以使用bash的compgen -W '-a -b -c'来显示可能的选项,并且我想知道是否还可以显示一些帮助消息.像这样:

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

解决方案

我曾经做过一些类似于将curl的单个char选项(例如-x)映射到GNU样式--long-option s的事情.

这是它的工作方式:

 [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 -
 

并非完全符合您的要求,但您可以根据自己的目的进行更新.

(我不确定bash是否可以在完成结果中处理空格,但至少可以使用_-.:-)

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#

解决方案

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 -. :-)

这篇关于显示自动完成候选者时是否可以显示一些帮助消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆