completarr

0.2.2 • Public • Published

Completarr

JavaScript Style Guide npm

Completarr is a zero config way to add shell auto completion for your yargs-based CLI applications.

You may want to use this over yargs' built-in solution if you need to support a wider range of shells (including zsh and fish).

Installation

npm install --save completarr

Usage

To use Completarr, perform the following rather simple steps:

  1. Integrate it into your CLI app:

    require('completarr')()
     
    // Your yargs-related code
  2. Add install/uninstall hooks to your package.json to automatically attach the completion script the user's shell init file:

    {
      "scripts": {
        "postinstall": "install-yargs-completion",
        "uninstall": "uninstall-yargs-completion"
      }
    }

Caveats

There are some things to consider:

  • Completarr is based on omelette and therefore does not support Windows. 😕
  • Completarr only works with globally installed commands.
  • Your yargs app needs to expose its help via .help() to make Completarr work.

Configuration

Completarr should work without any config 99% of the time, but there might be some edge cases in your app you want to handle:

Command Name (Shell Script)

By default, Completarr adds completion for all commands it finds in your package.json's bin field. If you got more commands there than you want completion for, you need to pass them to the install/uninstall hook explicitely:

// Your CLI app's package.json
{
  // We got three binaries:
  "bin": {
    "a": "./src/a",
    "b": "./src/b"
    "c": "./src/c"
  },
  "scripts": {
    // But we only want to install completions for "b" and "c":
    "postinstall": "install-yargs-completion b c",
    "uninstall": "uninstall-yargs-completion b c"
  }
}

Command Name (Node)

Completarr needs to know the name of your yargs root command to provide completions. By default it derives that from the binary where Completarr is included:

// file: src/hello
require('completarr')()
 
// Completarr assumes the command name is "hello"

However if for some reason your command name does not equal your file's name, you may pass Completarr the command name manually:

// file: src/foo.js
require('completarr')({
  name: 'hello'
})

Help Option

By default, yargs' option for showing the help output is --help. However, should you decide to publish the help under a different option (which yargs allows you to do) you'll have to tell Completarr about it:

// We use --info instead of --help
require('completarr')({
  helpOption: 'info'
})
 
// yargs-related code
require('yargs')
  .help('info')
  // ...

Package Sidebar

Install

npm i completarr

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

9.78 kB

Total Files

7

Last publish

Collaborators

  • loilo