gpt4 book ai didi

bash - 显示自动完成候选人时是否可以显示一些帮助信息?

转载 作者:行者123 更新时间:2023-11-29 09:51:21 25 4
gpt4 key购买 nike

有些命令有很多-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 的单个字符选项(如 -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 是否可以处理完成结果中的空格,但至少你可以使用 _-。:-)

关于bash - 显示自动完成候选人时是否可以显示一些帮助信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com