commander-completion
Shell completion for Commander.js
This was built as part of foundry, a CLI utility for making releases painless.
$ npm pub|$ npm publish |
Getting Started
Install the module with: npm install commander-completion
var program = ;programname = 'git';program action { // Checkout a git branch };program action { program; }; // Parse in arguments (e.g. `COMP_LINE="git che" COMP_POINT=7 git completion`)// Logs: ['checkout']program;
Documentation
commander-completion
exposes a mixin function, mixinCommanderCompletion
, as its module.exports
. After we mixin to Commander.js
, we add more methods onto command()
objects.
Currently, you are required to specify the name
property of your program
.
mixinCommanderCompletion(commander)
Add new completion methods to Commander.js' Command
objects
- commander
Object
- Instance ofCommander.js
Command.completion(completionFn)
New method available on Commander.js Commands
(e.g. program.command('remote').completion(completionFn)
)
Save completion function to call when completing the current command
- completionFn
Function
- Error-first callback that will callback with matches -completion
should have a signature offunction (info, cb)
- info
Object
- Collection of distilled information about original input- The format will be the returned value from twolfson/line-info
- cb
Function
- Error-first callback function to run with matchescb
has a signature offunction (err, results)
- info
Command.complete(params, cb)
New method available on Commander.js Commands
(e.g. program.command('remote').complete(params, cb)
)
Get completion results for current command
- params
Object
- Information similar to that passed in bybash's
tab completion- line
String
- Input to complete against (similar toCOMP_LINE
) - cursor
Number
- Index withinline
of the cursor (similar toCOMP_POINT
)
- line
- cb
Function
- Optional error-first callback function that receives matchescb
should have a signature offunction (err, results)
- If
cb
is not provided,err
will be thrown andresults
will be printed tostdout
viaconsole.log
Examples
An full example of git
would be
var program = ;programname = 'git';program // `git checkout master` // `git checkout -b dev/hai` action { // Checkout a `git` branch };var remote = program;remote // `git remote add origin git@github.com:...` // No possible tab completion here action { // Add a `git` remote };remote // `git remote rm origin` action { // Remove a `git` remote }; programcomplete // `git remo|add` line: 'git remoadd' cursor: 8 { results; // ['remote']}; programcomplete // `git remote |` line: 'git remote ' cursor: 11 { results; // ['add', 'rm']};
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint
and test via npm test
.
Donating
Support this project and others by twolfson via donations.
http://twolfson.com/support-me
Unlicense
As of Dec 16 2013, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.