1 var fs = require('fs'),
4 // add bash completions to your
5 // yargs-powered applications.
6 module.exports = function (yargs, usage) {
8 completionKey: 'get-yargs-completions'
11 // get a list of completion commands.
12 self.getCompletion = function (done) {
14 current = process.argv[process.argv.length - 1],
15 previous = process.argv.slice(process.argv.indexOf('--' + self.completionKey) + 1),
16 argv = yargs.parse(previous)
18 // a custom completion function can be provided
20 if (completionFunction) {
21 if (completionFunction.length < 3) {
22 // synchronous completion function.
23 return done(completionFunction(current, argv))
25 // asynchronous completion function
26 return completionFunction(current, argv, function (completions) {
32 if (!current.match(/^-/)) {
33 usage.getCommands().forEach(function (command) {
34 completions.push(command[0])
38 if (current.match(/^-/)) {
39 Object.keys(yargs.getOptions().key).forEach(function (key) {
40 completions.push('--' + key)
47 // generate the completion script to add to your .bashrc.
48 self.generateCompletionScript = function ($0) {
49 var script = fs.readFileSync(
50 path.resolve(__dirname, '../completion.sh.hbs'),
53 name = path.basename($0)
55 // add ./to applications not yet installed as bin.
56 if ($0.match(/\.js$/)) $0 = './' + $0
58 script = script.replace(/{{app_name}}/g, name)
59 return script.replace(/{{app_path}}/g, $0)
62 // register a function to perform your own custom
63 // completions., this function can be either
64 // synchrnous or asynchronous.
65 var completionFunction = null
66 self.registerFunction = function (fn) {
67 completionFunction = fn