简体   繁体   English

如何将选项卡完成添加到 Nodejs CLI 应用程序

[英]How to add tab completion to a Nodejs CLI app

I want to add tab completion to a Nodejs CLI app (And preferably generate the tab completion dynamically).我想向 Nodejs CLI 应用程序添加选项卡完成(并且最好动态生成选项卡完成)。

I found a few npm modules but not sure how to really implement them:我找到了一些 npm 模块,但不确定如何真正实现它们:

https://github.com/hij1nx/complete https://github.com/hij1nx/complete

https://github.com/mklabs/node-tabtab https://github.com/mklabs/node-tabtab

So what I am looking for is so I can have a nodejs file that is something like:所以我正在寻找的是这样我可以拥有一个类似于以下内容的 nodejs 文件:

my-cmd create arg1 arg2

But then I might want to autocomplete like:但后来我可能想要自动完成,如:

my-cmd cr<tab> -> create

Thanks!谢谢!

Use omelette package that I built.使用我构建的煎蛋包。 If you have any questions, please contact me.如果你有任何疑问,请联系我。

Edit - fast answer编辑 - 快速回答

After I answered, I kept reading tabtab source a bit and noticed that I can also run我回答后,我继续阅读tabtab源代码,发现我也可以运行

 pkgname completion install 

to install the completion.安装完成。 since my environment was already dirty, I don't know if it actually did anything, but seems to me like it did..因为我的环境已经很脏了,我不知道它是否真的做了什么,但在我看来它确实做了..

Longer answer更长的答案

@CameronLittle has given great documentation. @CameronLittle 提供了很好的文档。

For the impatient, you can start by running对于不耐烦的,你可以从跑步开始

sudo bash -c 'pkgname completion > /etc/bash_completion.d/pkgname'
source /etc/bash_completion.d/pkgname

This will add completion to your current bash session.这将为您当前的 bash 会话添加完成。

As far as I know, new sessions will get the completion automatically.据我所知,新会话将自动完成。

To make the process seamless for user, you can use the install and postinstall hooks in package.json为了使用户的过程无缝,您可以使用 package.json 中的installpostinstall钩子

https://docs.npmjs.com/misc/scripts https://docs.npmjs.com/misc/scripts

Make sure to not print anything by default.确保默认情况下不打印任何内容。 means running pkgname should result in no output, or otherwise it will not work.意味着运行pkgname应该不会产生任何输出,否则它将无法工作。

important!重要的! install tabtab only from master仅从 master 安装 tabtab

It seems tabtab has an annoying bug that was resolved in master but never got into a release..似乎 tabtab 有一个烦人的错误,该错误已在 master 中解决,但从未发布过..

The relevant commit to fix it is this: https://github.com/mklabs/node-tabtab/commit/f8473555bf7278a300eae31cbe3377421e2eeb26修复它的相关承诺是这样的: https : //github.com/mklabs/node-tabtab/commit/f8473555bf7278a300eae31cbe3377421e2eeb26

which handles completion for strings starting with -- .它处理以--开头的字符串的完成。

The commit if from february 2014, however the latest release as of (Jan. 2015) is 0.0.2 from Jan. 2014.. I assume there will not be more releases.从 2014 年 2 月开始的提交,但是(2015 年 1 月)的最新版本是 2014 年 1 月的 0.0.2 .. 我认为不会有更多版本。

So if you want to get this fix, and you should(!), install tabtab only from master.所以如果你想得到这个修复,你应该(!),只从 master 安装 tabtab。

don't waste 2 hours figuring out what you did wrong like me :)不要像我一样浪费 2 个小时弄清楚你做错了什么:)

How did i reach this answer?我是如何得出这个答案的? TL;DR TL; 博士

While @CameronLittle's answer gives the explanation behind the scene, I would like to explain how to I reached the answer.虽然@CameronLittle 的回答给出了幕后的解释,但我想解释一下我是如何得出答案的。

I tried using the package tabtab which has an explicit section about installing it.我尝试使用包tabtab ,它有一个关于安装它的明确部分。 see https://www.npmjs.com/package/tabtab#completion-installhttps://www.npmjs.com/package/tabtab#completion-install

However, that didn't seem to work for me.然而,这似乎对我不起作用。

Looking at the code they instruct to add, I see the following process.argv.slice(2)[0] === 'completion' which made me run the command pkgname completion , which outputs something that starts with查看他们指示添加的代码,我看到以下process.argv.slice(2)[0] === 'completion'这让我运行命令pkgname completion ,它输出以开头的内容

###-begin-pkgname-completion-###
### credits to npm, this file is coming directly from isaacs/npm repo
#
# Just testing for now. (trying to learn this cool stuff)
#
# npm command completion script
#
# Installation: pkgname completion >> ~/.bashrc  (or ~/.zshrc)
#

the words this file is coming directly from isaacs/npm repo made me wonder more. this file is coming directly from isaacs/npm repo让我想知道更多。 following the other answer here, I looked at /etc/bash_completion.d/npm - which showed the same exact content.. and so the comment.按照此处的其他答案,我查看了 /etc/bash_completion.d/npm - 它显示了相同的确切内容......所以评论。

I decided to run我决定跑

pkgname completion > /etc/bash_completion.d/pkgname

however that requires sudo permissions and so becomes但是,这需要 sudo 权限,因此变为

sudo bash -c "pkgname completion > /etc/bash_completion.d/pkgname

and then, in order to apply it to current bash session I had to run然后,为了将其应用于当前的 bash 会话,我必须运行

 source /etc/bash_completion.d/pkgname

and voila!瞧! it works!有用!

when I tried to open another terminal, it still worked, so I assume it will apply to all users.当我尝试打开另一个终端时,它仍然有效,所以我认为它适用于所有用户。 if not - you should add it to .bashrc or something..如果没有 - 你应该将它添加到 .bashrc 或其他东西中。

I would just like to add that there is a npm package yargs that enables bash-completion shortcuts for commands and options.我只想补充一点,有一个npm 包 yargs可以为命令和选项启用 bash 完成快捷方式。

It has the option to output a .bashrc completion script.它可以选择输出 .bashrc 完成脚本。 Bash completions are then enabled by sourcing the generated script.然后通过获取生成的脚本来启用 Bash 完成。

It is currently an actively maintained package on npm with over a million downloads a month.它目前是 npm 上一个积极维护的软件包,每月下载量超过一百万次。

As @Joe said, this is something your user's shell provides.正如@Joe 所说,这是您用户的 shell 提供的。 For bash, you essentially create and register a function that's run when a user tabs after typing the name of your program.对于 bash,您本质上创建并注册了一个函数,该函数在用户键入程序名称后按 Tab 键时运行。 The function returns the strings available for autocomplete.该函数返回可用于自动完成的字符串。

See this tutorial for an intro有关介绍,请参阅本教程

http://www.debian-administration.org/article/316/An_introduction_to_bash_completion_part_1 http://www.debian-administration.org/article/316/An_introduction_to_bash_completion_part_1

I also like this example, because it's simple and shows what needs to go on.我也喜欢这个例子,因为它很简单并且显示了需要做什么。 It's for the python tool fabric.它用于 python 工具结构。

https://raw.githubusercontent.com/marcelor/fabric-bash-autocompletion/master/fab https://raw.githubusercontent.com/marcelor/fabric-bash-autocompletion/master/fab

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

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