简体   繁体   English

在shell中自定义完成

[英]Custom completion in shell

I need to develop a utility that will take command line arguments as follows: 我需要开发一个实用程序,它将获取命令行参数,如下所示:

$ lsm  -g <group> -t <type> -d <device>

My project manager wants that when we type any argument like lsm -g and press Tab , then a function will be called to run a database query and fetch help for the user about the value of the option. 我的项目经理希望当我们键入任何参数如lsm -g并按Tab键时 ,将调用一个函数来运行数据库查询并为用户提取有关该选项值的帮助。

Similar to how terminal behaves in this case: 与终端在这种情况下的行为类似:

$ cd <tab>
.bash_history                   .local/
.bash_logout                    .log-report.log.swp
.bash_profile                   .macromedia/
.bashrc                         .mozilla/
.cache/                         Music/
.config/                        .mysql_history
.dbus/                          .nautilus/
Desktop/                        .opera/
Documents/                      .orc/
Downloads/                      .p2/
.eclipse/                       Pictures/

I have tried these approaches: 我试过这些方法:

  1. Use fork() to create a child to call the help function. 使用fork()创建一个子句来调用help函数。
  2. Use execv() to run a help function using a seperate binary. 使用execv()使用单独的二进制文件运行帮助函数。

But both require that lsm be running. 但两者都要求运行lsm

I'm not sure how to proceed further. 我不确定如何继续下去。

As various commentators have noted, tab-completion is implemented in the shell, not in the program which is about to be executed. 正如各种评论家所指出的那样,tab-completion是在shell中实现的,而不是在即将执行的程序中。

Most shells have frameworks for implementing custom tab-completion. 大多数shell都有用于实现自定义制表符完成的框架。 In the case of bash , it is implemented with the help of the readline library. bash的情况下,它是在readline库的帮助下实现的。 There is extensive documentation in the bash manual and there are a variety of tutorials kicking around the internet, such as this one from Debian (not an endorsement, just the result of a quick Google search). bash手册中有大量的文档,互联网上有各种各样的教程,比如来自Debian的这个教程(不是认可,只是谷歌快速搜索的结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM