gpt4 book ai didi

command-line-arguments - 如何使用给定的参数集完成 GNU 长选项?

转载 作者:行者123 更新时间:2023-12-02 02:42:45 31 4
gpt4 key购买 nike

GNU建议使用 --name=value 语法为 long 选项传递参数。它使长选项能够接受本身是可选的参数。

假设您有一组完整的可能参数。您如何为这样的选项编写 bash 完成代码?我希望完成在完成一个明确的参数时添加空间,但不是之前。

最佳答案

这是我为完成虚构命令的代码中给出的 GNU 选项而编写的模板代码 gnu-options .

不带参数的选项在数组 opts 中定义.可能带参数的选项在关联数组中定义 args .请注意,如 with-args0出现在两者中它是一个带有可选参数的选项。

该脚本甚至支持 $COMP_WORDBREAKS 的情况不包括“=”,但显示的完成时间更长。

# Hack for given strings $2,$3,... possibly being in $1 and $COMP_WORDBREAKS
# Only the part after each match is listed as a completion.
# Run 'shopt -s extdebug; declare -F __ltrim_colon_completions; shopt -u extdebug'
# to see location for the respective function for colon only.
__ltrim_completions ()
{
local cur=$1; shift
while [[ ${1+x} ]]; do
if [[ "$cur" == *$1* && "$COMP_WORDBREAKS" == *$1* ]]; then
local x_word=${cur%$1*}$1
local i
for i in ${!COMPREPLY[*]}; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$x_word"}
done
fi
shift
done
}


_gnu_options()
{
local IFS=$'\n' # needed for handling trailing space of some options and all arguments
local cur prev words cword split # needed by _init_completion()
local opts i prefix= wordlist
local -A args=()
# Do not treat = as word breaks even if they are in $COMP_WORDBREAKS:
# Split option=value into option in $prev and value in $cur
_init_completion -s || return

# DEFINE OPTIONS THAT DO NOT TAKE AN ARGUMENT HERE:
opts=(with-args0 option0 option1 par param)
# DEFINE THE OPTIONS WITH ARGUMENTS HERE:
args=([with-args0]= [with-args1]=$'arg10\narg11')
args[with-args2]=\
'arg=20
arg=21
var=22
argx'
args[with-args3]=

for i in ${!args[*]}; do
if [[ $prev = --$i ]]; then
local j dobreak=
[[ $split == false ]] && {
# equal sign not used; check, if argument is optional.
for j in ${opts[*]}; do [[ $i == $j ]] && { dobreak=t; break; } done
}
[[ $dobreak ]] && break
[[ "$COMP_WORDBREAKS" != *=* && $split == true ]] && prefix="--$i="
if [[ ${args[$i]} ]]; then
COMPREPLY=( $( compgen -P "$prefix" -W "${args[$i]}" -- "$cur" ) )
__ltrim_completions "$cur" =
else
case $i in
with-args0)
# expand file/directory name.
COMPREPLY=( $( compgen -P "$prefix" -A file -- "$cur" ) )
compopt -o filenames
;;
*)
COMPREPLY=()
;;
esac
fi
return 0
fi
done

wordlist=()
for i in ${opts[*]}; do wordlist+=("--$i "); done
for i in ${!args[*]}; do wordlist+=("--$i="); done
COMPREPLY=( $( compgen -W "${wordlist[*]}" -- "$cur" ) )
compopt -o nospace
} && complete -F _gnu_options gnu-options

关于command-line-arguments - 如何使用给定的参数集完成 GNU 长选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58156681/

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