gpt4 book ai didi

Bash 完成 : Allow flags once

转载 作者:行者123 更新时间:2023-11-29 09:31:50 26 4
gpt4 key购买 nike

我有一个基本的完整功能:

_my_complete () 
{
local cur prev opts opts2;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
prev="${COMP_WORDS[COMP_CWORD-1]}";
opts="foo bar";
opts2="-f -s";
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
;;
2 | 4)
COMPREPLY=($(compgen -W "${opts2}" -- ${cur}))
;;
esac
}

如何限制补全在命令行中只接受一次-f或-s?

谢谢

最佳答案

已解决。灵感来自@whjm 的评论和这个post

_my_complete() {
local cur prev opts opts2 subopts ;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
prev="${COMP_WORDS[COMP_CWORD-1]}";
opts="foo bar";
opts2="-f -s";
subopts=();

for i in ${opts2}
do
for j in "${COMP_WORDS[@]}"
do
if [[ "$i" == "$j" ]]
then
continue 2
fi
done
subopts+=("$i")
done

case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
;;
2|4)
COMPREPLY=($(compgen -W "${subopts[*]}" -- ${cur}))
;;
esac
}

关于Bash 完成 : Allow flags once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962909/

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