Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Shell自定义自动完成-不列出以前列出的参数_Bash_Shell_Autocomplete_Sh - Fatal编程技术网

Bash Shell自定义自动完成-不列出以前列出的参数

Bash Shell自定义自动完成-不列出以前列出的参数,bash,shell,autocomplete,sh,Bash,Shell,Autocomplete,Sh,我已经将自动完成编程到我的脚本中,但当我输入TAB时,它也会显示以前填充的参数。如何避免这种情况 这是我的密码: _load_opt () { COMPREPLY=() local cur=${COMP_WORDS[COMP_CWORD]} local prev=${COMP_WORDS[COMP_CWORD-1]} case "$prev" in "--create") COMPREPLY=( $( compge

我已经将自动完成编程到我的脚本中,但当我输入TAB时,它也会显示以前填充的参数。如何避免这种情况

这是我的密码:

_load_opt ()   
{                
  COMPREPLY=()   
  local cur=${COMP_WORDS[COMP_CWORD]}
  local prev=${COMP_WORDS[COMP_CWORD-1]}

  case "$prev" in
   "--create")
       COMPREPLY=( $( compgen -W "--name --type --size" -- $cur ) )  
       return 0 
       ;;
  "--delete")
        COMPREPLY=( $( compgen -W "--name" -- $cur ) )
        return 0 
       ;;
 esac

 if [[ ${cur} == -* ]]
 then
    COMPREPLY=($(compgen -W "--create --delete" -- $cur ) )
 return 0
fi
}

complete -F _load_opt ./run.sh `
我找到了这个脚本。 当我跑的时候

# ./run.sh --create --name file.txt --
--create  --delete  
由于最后一个默认的
if
语句,它会自动完成主参数。但是我想自动完成
--type--size
而不是
--name

我试图用
--create--name
再添加一个case,但我应该用所有组合添加。听起来不对


我怎样才能做到这一点?。谢谢你的帮助

要执行您想要的操作,您需要检查整个命令行,每次检查一个选项,类似这样的内容(测试不多):(Edit:使用关联数组,需要bash v4)

\u加载选项(){
#如果我们在开头,只允许动词选项。
如果((COMP_CWORD==1));则
COMPREPLY=($(compgen-W--create--delete--“$2”);
fi;

如果((COMP_CWORD谢谢你的想法。虽然有语法错误,但我可以纠正它。谢谢。@Dinesh:bash没有报告我的系统中的任何语法错误。有一个地方最好引用扩展名,我更改了扩展名。你看到了什么语法错误?
-该局部变量的
标记并不是全部在我的Linux机器上欠下的。所以我使用
-a
选项创建了数组。我想可能是因为
标志[--name]=ok;
失败了。而
${!标志[*]}
打印索引而不是值是因为
符号。@Dinesh:啊,你没有bash v4,所以你没有关联数组。所有这些语法都是关联数组支持的一部分。你可以在没有关联数组的情况下做类似的事情(也许你已经有了),但这会更复杂。
_load_opt () {
    # If we're at the beginning, only allow the verb options.
    if (( COMP_CWORD == 1 )); then
        COMPREPLY=($(compgen -W "--create --delete" -- "$2"));
    fi;
    if (( COMP_CWORD <= 1 )); then
        return;
    fi;

    # Otherwise, construct the list of allowed options based on the verb
    local -A flags;
    case ${COMP_WORDS[1]} in 
        --create)
            flags[--name]=ok;
            flags[--type]=ok;
            flags[--size]=ok
        ;;
        --delete)
            flags[--name]=ok
        ;;
        *)
            return 0
        ;;
    esac;

    # And scan the entire command line, even after the current point, for already
    # provided options, removing them from the list
    local word;
    for word in "${COMP_WORDS[@]:2}";
    do
        unset flags[$word];
    done;

    # Finally, complete either an option or an option value, depending on the
    # previous word (always the third argument to this function)
    # The first three lines in the case statement are just examples
    case $3 in
        --name) COMPREPLY=($(compgen -W "valid name list" -- "$2")) ;;
        --type) COMPREPLY=($(compgen -W "good bad ugly" -- "$2")) ;;
        --size) COMPREPLY=($(compgen -W "small medium large" -- "$2")) ;; 
        *) COMPREPLY=($(compgen -W "${!flags[*]}" -- "$2")) ;;
    esac
}