巴什与完井等号和枚举标志值 [英] Bash completions with equals sign and enumerable flag values

查看:130
本文介绍了巴什与完井等号和枚举标志值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个bash完成例程将提示命令行标志和合适的标志值。例如,在下面的 fstcompose 命令我想竞赛套路,首先建议在 compose_filter = 标记,然后从建议的可能值[alt_sequence,汽车,比赛,序列]

  fstcompose --compose_filter =

对于不有一套价值观有关我希望有竞争回落到提示的路径或文件的默认模式。任何标志

在一个问题,我面对的是作为一个单独的令牌对待=等号,并设置为$ P $光伏COMP_WORD。是否有检测前全旗及包括previous =字符的技术?还是有实现的标志与枚举值的这种类型的完成更好的办法?下面是完成例程我工作的一个样本。

  _fstcompose()
{
  当地CUR preV选择采用过滤器p preV
  COM prePLY =()
  CUR =$ {COMP_WORDS [COMP_CWORD]}
  preV =$ {COMP_WORDS [COMP_CWORD-1]}
  选择采用= - compose_filter = --connect
  过滤器=alt_sequence自动匹配序列  如果[[$ {} CUR == - *];然后
  COM prePLY =($(compgen -W$ {} OPTS - $ {} CUR))
  返回0
  科幻  如果[[$ {$ P $}光伏==--compose_filter =]];然后
  COM prePLY =($(compgen -W$ {}滤镜 - $ {} CUR))
  返回0
  科幻
  _filedir
}
完成-o -F无空间_fstcompose fstcompose


解决方案

您第一个返回0 prevents第二如果被评估。尝试使用情况特定最到最不特定的或至少订购您如果语句的方式有序声明。

由于=包含在 $ COMP_WORDBREAKS $ {preV} 是 - compose_filter没有=

如果您从 $ COMP_WORDBREAKS 删除=,那么 - compose_filter 还是 $ {CUR} ,而不是 $ {preV} 。这样做没有preserving并恢复它的价值会破坏其他落成。

我不知道还有什么其他问题,有可能是。

您可以辣椒的功能与被重定向到另一个终端来帮助调试呼应语句。例如:

 回声CUR:$ CUR,preV:$preV>为/ dev / PTS / 2

I'm trying to construct a bash completion routine that will suggest command lines flags and suitable flag values. For example in the below fstcompose command I would like the competition routine to first suggest the compose_filter= flag, and then suggest possible values from [alt_sequence, auto, match, sequence].

fstcompose --compose_filter=

For any flags that do not have a set of associated of values I want the competition to fall back to the default mode of suggesting paths or files.

The one issue I'm facing is the = equal sign treated as an individual token and is set as the prev COMP_WORD. Is there a technique for detecting the whole flag before and including the previous = character ? Or is there a better way of implementing this type completion of flag with enumerable values? Below is a sample of the completion routine I'm working with.

_fstcompose()
{
  local cur prev opts filters pprev
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  opts="--compose_filter= --connect"
  filters="alt_sequence auto match sequence"

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

  if [[ ${prev} == "--compose_filter=" ]] ; then
  COMPREPLY=($(compgen -W "${filters}" -- ${cur}))
  return 0
  fi
  _filedir
}
complete -o nospace -F _fstcompose fstcompose

解决方案

Your first return 0 prevents the second if from being evaluated. Try using a case statement ordered from most-specific to least-specific or at least order your if statements that way.

Since "=" is included in $COMP_WORDBREAKS, ${prev} is "--compose_filter" without the "=".

If you remove "=" from $COMP_WORDBREAKS then --compose_filter is still ${cur} rather than ${prev}. Doing so without preserving and restoring its value will break other completions.

I'm not sure what other problems there may be.

You can pepper your function with echo statements that are redirected to another terminal to aid in debugging. For example:

echo "cur: $cur, prev: $prev" > /dev/pts/2

这篇关于巴什与完井等号和枚举标志值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆