How to add tab completion to a Nodejs CLI app

13,117

Solution 1

Use omelette package that I built. If you have any questions, please contact me.

Solution 2

Edit - fast answer

After I answered, I kept reading tabtab source a bit and noticed that I can also run

 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.

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.

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

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.

important! install tabtab only from master

It seems tabtab has an annoying bug that was resolved in master but never got into a release..

The relevant commit to fix it is this: 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.

So if you want to get this fix, and you should(!), install tabtab only from master.

don't waste 2 hours figuring out what you did wrong like me :)

How did i reach this answer? TL;DR

While @CameronLittle's answer gives the explanation behind the scene, I would like to explain how to I reached the answer.

I tried using the package tabtab which has an explicit section about installing it. see https://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

###-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. following the other answer here, I looked at /etc/bash_completion.d/npm - which showed the same exact content.. and so the comment.

I decided to run

pkgname completion > /etc/bash_completion.d/pkgname

however that requires sudo permissions and so becomes

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

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

Solution 3

I would just like to add that there is a npm package yargs that enables bash-completion shortcuts for commands and options.

It has the option to output a .bashrc completion script. Bash completions are then enabled by sourcing the generated script.

It is currently an actively maintained package on npm with over a million downloads a month.

Solution 4

As @Joe said, this is something your user's shell provides. For bash, you essentially create and register a function that's run when a user tabs after typing the name of your program. 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

I also like this example, because it's simple and shows what needs to go on. It's for the python tool fabric.

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

Share:
13,117
Jonovono
Author by

Jonovono

Updated on June 14, 2022

Comments

  • Jonovono
    Jonovono almost 2 years

    I want to add tab completion to a Nodejs CLI app (And preferably generate the tab completion dynamically).

    I found a few npm modules but not sure how to really implement them:

    https://github.com/hij1nx/complete

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

    So what I am looking for is so I can have a nodejs file that is something like:

    my-cmd create arg1 arg2
    

    But then I might want to autocomplete like:

    my-cmd cr<tab> -> create
    

    Thanks!