在某些 Bash 自动完成选项的末尾添加空格,但在其他选项中不添加空格? [英] Add spaces to the end of some Bash autocomplete options, but not to others?

查看:15
本文介绍了在某些 Bash 自动完成选项的末尾添加空格,但在其他选项中不添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Java 程序创建 Bash 完成脚本.该程序的典型调用可能如下所示:

I'm trying to create a Bash completion script for a Java program. A typical invocation of that program might look like this:

$ javaProgram -Dproperty=foo option1 option2

我的脚本的一部分将是建议此程序可用的各种 Java 属性(即,当用户键入 -D 时,脚本会建议,例如,property=,允许用户输入值).

Part of my script will be to suggest the various Java properties that are available for this program (i.e., when the user types -D, the script would suggest, say, property=, allowing the user to then type the value).

我希望完成后不要在等号后插入空格.但是,当用户为程序键入各种选项时(上面示例中的 option1option2),我希望脚本完成并插入完成后一个空格.

I'd like the completion to not insert a space after the equals sign. However, when the user is typing the various options for the program (option1 and option2 in the above example), I'd like the script to do the completion, and insert a space when it is completed.

我是 Bash 补全脚本的新手,但我知道 complete shell 内置的 nospace 选项.不过,它似乎不适用于 compgen 内置函数,这似乎是我想要的.我已经尝试了使用 nospace ,然后在适当的选项结束时显式包括空格,但它们似乎没有通过.

I'm new to Bash completion scripting, but I'm aware of the nospace option for the complete shell builtin. It doesn't seem to work for the compgen builtin, though, which seems like what I want. I've tried using nospace, and then explicitly including spaces at the end of the appropriate options, but they don't seem to be making it through.

有谁知道如何在某些选项的末尾获取空格,而在其他选项的末尾没有空格?

Does anyone know how to get spaces at the end of some options, but no spaces at the end of others?

推荐答案

C4H5As 提到的 scp 的 bash 补全中保留添加空格的功能不是&符号,这只是用匹配替换"的 sed 语法.

The feature in in scp's bash completion mentioned by C4H5As that retains the added space is not the ampersand, that is just sed syntax for "replace with match".

您必须更改 IFS,以便 compgen 不会在空格上而是在换行符或制表符上拆分输入:

You have to change the IFS so compgen does not split the input on spaces but on newlines or tabs:

local IFS=$'	
'
COMPREPLY=( $(compgen -W "$options" -- $cur) )

这篇关于在某些 Bash 自动完成选项的末尾添加空格,但在其他选项中不添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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