Skip to content

Commit

Permalink
Add help auto-complete, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvisser committed May 25, 2014
1 parent 84e8bde commit 8be0b18
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Currently:

* Parsing of string command input into an argv array
* Multi-line commands
* Tab auto-complete for commands based on available commands
* Tab auto-complete to choose command to run
* Custom tab auto-complete implementation for command-specific arguments
* API and Model for creating and loading your own commands
* Flexible error handling
* Custom PS1 and PS2 prompts
Expand Down Expand Up @@ -236,6 +237,12 @@ unknown$ iam branden
branden$ quit
```

### Custom Auto-complete

It is possible to implement auto-complete for your custom commands. Provide an `autocomplete` function in your command implementation that takes a `session`, `args` array and `callback([err], [replacementsArray])`.

See the [readline auto-complete explanation](https://github.com/mrvisser/node-readcommand#auto-complete) for more details, and the [help command](https://github.com/mrvisser/node-corporal/blob/master/commands/help.js) contains a reference implementation that returns auto-completions for command names for which to show help.

### Command Contexts

It is possible to filter the commands available based on a custom context. For example, if your shell application requires authentication, you may want to provide some commands only to users when they are authenticated. Contexts allow you to accomplish this.
Expand Down
13 changes: 13 additions & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ module.exports = {
}

return callback();
},
'autocomplete': function(session, args, callback) {
if (args.length !== 1) {
return callback();
}

// Filter by command names
return callback(null, _.chain(session.commands().get())
.keys()
.filter(function(commandName) {
return (commandName.indexOf(args[0]) === 0);
})
.value());
}
};

Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ function _createPs(session, psVar) {
*/
function _createAutocomplete(session) {
return function(args, callback) {
args = args.slice();

// First handle the case where this is an auto-suggest based on finding the command name
var allCommandNames = _.keys(session.commands().get());
if (_.isEmpty(args)) {
return callback(null, allCommandNames);
Expand All @@ -204,6 +207,14 @@ function _createAutocomplete(session) {
}));
}

// We presumably have a command argument already, feed the remaining arguments into the
// commands autocomplete implementation
var commandName = args.shift();
var command = session.commands().get(commandName);
if (command && _.isFunction(command.autocomplete)) {
return command.autocomplete(session, args, callback);
}

return callback(null, []);
};
}

0 comments on commit 8be0b18

Please sign in to comment.