【问题标题】:Is it possible to display some help message when showing autocomplete candidates?显示自动完成候选人时是否可以显示一些帮助消息?
【发布时间】:2017-01-05 08:22:57
【问题描述】:

有些命令有很多-xx 可以是任何英文字母)选项,有时很难记住它们的所有含义。我可以使用 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#

【问题讨论】:

    标签: bash tab-completion bash-completion


    【解决方案1】:

    我曾经做过类似的事情,将curl 的一些单字符选项(如-x)映射到GNU 样式--long-options。

    这就是它的工作原理:

    [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 是否可以处理完成结果中的空格,但至少您可以使用 _-。:-)

    【讨论】:

      【解决方案2】:

      IMO 那将是一个坏主意。

      但如果你真的想要它,你可以这样做: 请注意,此代码有点粗糙,除非我知道您的用例,否则我无法提供确切的答案。但是,给您一个大致的概念就足够了。

      原文:

      complete -o nospace -F _default_completion my_command
      

      新:

      _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
      

      这样,bash 应该在每行显示一个选项,并在其前面显示一个描述。当然,您需要自己编写 generate_description_from_option。

      【讨论】:

      • COMPREPLY=${_compreply[@]} 应该是COMPREPLY=( ${_compreply[@]} )
      • 我以为 zsh 做了这样的事情。
      • @whjm:我在本地脚本中更正了。忘记在答案中纠正它。感谢您的注意。 :-) 已修复。
      • 谢谢,anishsane。顺便问一下,_get_comp_words_by_ref_get_comp_words_by_ref 是什么?
      • 这是 bash 补全中的标准功能。它给出了当前正在输入的单词。
      【解决方案3】:

      ble.sh 除了提供许多其他功能外,还提供了这些功能。

      【讨论】:

        猜你喜欢
        • 2019-03-25
        • 2016-10-12
        • 2023-03-22
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多