Skip to content

Commit

Permalink
Merge pull request #27 from xgecko/master
Browse files Browse the repository at this point in the history
Adding command specific options
  • Loading branch information
heapwolf committed Jul 14, 2015
2 parents bb1880e + a24cc79 commit b945b78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions example/bin/examplecomp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ complete({
lemon: {},
mango: {}
},
commandOptions: {
apple: [ '--cinnamon'],
orange: [ '--extra-pulp', '--no-pulp' ]
},
options: {
'--version': {},
'--help': {}
Expand Down
17 changes: 15 additions & 2 deletions lib/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var bashrc = getRc();
var argv = require('optimist').argv;

var commands;
var commandOptions;
var program;
var opt;
var words = [];
Expand All @@ -22,6 +23,7 @@ function complete(options) {

options = options || {};
commands = options.commands || exports.commands || {};
commandOptions = options.commandOptions || exports.commandOptions || {};
opt = options.options || exports.options;
program = options.program || exports.program;

Expand All @@ -46,7 +48,7 @@ function complete(options) {
// further. This gets caught by the uncaughtException listener
// and stops anymore synchronous code from being executed.
// Yes, it is hacky.
throw 'stop';
throw '';
}
}

Expand All @@ -59,6 +61,7 @@ function completeWords() {
var i = 1;
var word;
var out;
var cmdOpt = opt;

// Reduce
for (; i < l; i++) {
Expand All @@ -71,6 +74,10 @@ function completeWords() {
} else {
if (command[word] && hop.call(command, word)) {
command = command[word];

if ( commandOptions[ word ] && hop.call( commandOptions, word ) ) {
cmdOpt = commandOptions[ word ];
}
} else {
break;
}
Expand Down Expand Up @@ -103,6 +110,12 @@ function completeWords() {

// Resolve position-independent options.
// Possibly add dynamic behavior here.
out = match(cur, cmdOpt);
if (out) {
echo(out);
return;
}

out = match(cur, opt);
if (out) {
echo(out);
Expand Down Expand Up @@ -334,7 +347,7 @@ complete.init = function() {

// If there are still any callbacks on the event loop,
// we need to kill the current stack immediately.
throw 'stop';
throw '';
}

return this;
Expand Down

0 comments on commit b945b78

Please sign in to comment.