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

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

问题描述

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

  $ javaProgram -Dproperty = foo option1 option2 

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

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

我是Bash完成脚本的新手,但是我知道内置 complete shell的 nospace 选项.但是,对于内置的 compgen 来说,它似乎不起作用,这似乎是我想要的.我试过使用 nospace ,然后在适当的选项末尾显式包括空格,但是它们似乎并没有通过它.

有人知道如何在某些选项的末尾获得空格,但在其他选项的末尾却没有空格吗?

解决方案

C4H5A提到的scp bash完成中保留额外空间的功能不是&符,它只是sed语法,用于替换匹配"./p>

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

 本地IFS = $'\ t \ n'COMPREPLY =($(compgen -W"$ options"-$ cur)) 

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

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).

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.

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?

解决方案

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".

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

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

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

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