使用bash的可编程完成条件尾随空格

编程入门 行业动态 更新时间:2024-10-24 08:29:34
本文介绍了使用bash的可编程完成条件尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建一个函数来提供,我使用一个命令完成可编程(从的 www.debian-administration/articles/317 )。 shell脚本用法如下:

I'm creating a function to provide programmable completion for a command that I use (with much help from www.debian-administration/articles/317). The shell script usage is as follows:

script.sh command [command options]

在这里命令可以是'富'和'酒吧'和命令选项为'富'是'a_foo =价值和b_foo =价值和命令选项为巴是a_bar =价值和b_bar =价值。

where command can be either 'foo' or 'bar' and command options for 'foo' are 'a_foo=value' and 'b_foo=value' and command options for 'bar' are 'a_bar=value' and 'b_bar=value'.

下面是我使用的配置:

_script() { local cur command all_commands COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" command="${COMP_WORDS[1]}" all_commands="foo bar" case "${command}" in foo) COMPREPLY=( $(compgen -W "--a_foo --b_foo" -- ${cur}) ); return 0;; bar) COMPREPLY=( $(compgen -W "--a_bar --b_bar" -- ${cur}) ); return 0;; *) ;; esac COMPREPLY=( $(compgen -W "${all_commands}" -- ${cur}) ) return 0 } complete -F _script script.sh

这主要是工程,我想:

% script.sh f[TAB]

完成对:

% script.sh foo

(与根据需要一个空格)

(with a trailing space as desired)

然而,这样的:

% script.sh foo a[TAB]

完成对:

% script.sh foo a_foo

(也有一空格)

我想用一个'='来代替尾随空格。或者,我会愿意改变传递的值compgen是--a_foo = --b_foo =,在这种情况下,我可能只是删除尾随的空间。

I'd like to replace the trailing space with an '='. Alternatively, I'd be willing to change the values passed to compgen to be "--a_foo= --b_foo=", in which case I could just delete the trailing space.

不幸的是,该命令是不是我的控制之下,所以我不能改变的命令行选项是格式--a_foo价值而不是--a_foo =价值。

Unfortunately, the command is not under my control, so I can't change the command line options to be of format "--a_foo value" instead of "--a_foo=value".

推荐答案

首先,你需要=添加到COM prePLY:

First you need to add = to the COMPREPLY:

COMPREPLY=( $(compgen -W "--a_foo= --b_foo=" -- ${cur}) )

下次你需要告诉完成不=后添加空格

next you need to tell completion not to add space after = with

compopt -o nospace

所以,你的脚本行应该是:

So, you script lines should be:

foo) COMPREPLY=( $(compgen -W "--a_foo= --b_foo=" -- ${cur}) ); compopt -o nospace; return 0;; bar) COMPREPLY=( $(compgen -W "--a_bar= --b_bar=" -- ${cur}) ); compopt -o nospace; return 0;;

更多推荐

使用bash的可编程完成条件尾随空格

本文发布于:2023-06-04 08:33:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可编程   空格   条件   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!