Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for switching command auto-completion #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ Adds `-t,--timeout N` to exit the process after N seconds with an error

Adds `-c,--catch` to catch and output uncaughtExceptions and resume execution

**autocomplete** - *enabled by default*

Enables command [auto-completion](https://github.com/chriso/cli/blob/master/examples/command.js)

*Note: Plugins are automatically disabled if an option or switch of the same name is already defined*

## LICENSE
Expand Down
35 changes: 19 additions & 16 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ if (process.env.NODE_DISABLE_COLORS || process.env.TERM === 'dumb') {
* The 'help' plugin is enabled by default.
*/
var enable = {
help: true, //Adds -h, --help
version: false, //Adds -v,--version => gets version by parsing a nearby package.json
status: false, //Adds -k,--no-color & --debug => display plain status messages /display debug messages
timeout: false, //Adds -t,--timeout N => timeout the process after N seconds
catchall: false, //Adds -c,--catch => catch and output uncaughtExceptions
glob: false //Adds glob matching => use cli.glob(arg)
help: true, //Adds -h, --help
version: false, //Adds -v,--version => gets version by parsing a nearby package.json
status: false, //Adds -k,--no-color & --debug => display plain status messages /display debug messages
timeout: false, //Adds -t,--timeout N => timeout the process after N seconds
catchall: false, //Adds -c,--catch => catch and output uncaughtExceptions
glob: false, //Adds glob matching => use cli.glob(arg)
autocomplete: true //Adds command auto-completion
}
cli.enable = function (/*plugins*/) {
Array.prototype.slice.call(arguments).forEach(function (plugin) {
Expand Down Expand Up @@ -238,7 +239,7 @@ cli.next = function () {
* `commands` is an optional array or object for apps that are of the form
* my_app [OPTIONS] <command> [ARGS]
* The command list is output with usage information + there is bundled
* support for auto-completion, etc.
* support for auto-completion (if 'autocomplete' plugin is enabled), etc.
*
* See README.md for more information.
*
Expand Down Expand Up @@ -397,20 +398,22 @@ cli.autocompleteCommand = function (command) {
} else {
list = command_list;
}
var i, j = 0, c = command.length, tmp_list;
var i, j, l = 0, c = command.length, tmp_list;
if (list.length === 0 || list.indexOf(command) !== -1) {
return command;
}
for (i = 0; i < c; i++) {
tmp_list = [];
if (enable['autocomplete']) {
for (i = 0; i < c; i++) {
tmp_list = [];
l = list.length;
if (l <= 1) break;
for (j = 0; j < l; j++)
if (list[j].length >= i && list[j][i] === command[i])
tmp_list.push(list[j]);
list = tmp_list;
}
l = list.length;
if (l <= 1) break;
for (j = 0; j < l; j++)
if (list[j].length >= i && list[j][i] === command[i])
tmp_list.push(list[j]);
list = tmp_list;
}
l = list.length;
if (l === 1) {
return list[0];
} else if (l === 0) {
Expand Down