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 autocomplete based on omelette library #907

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
37 changes: 36 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,44 @@ You can enable `--harmony` option in two ways:
* Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. Note some os version don’t support this pattern.
* Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.

## Autocomplete

commander has autocomplete capability builtin, and you can enable it by the following steps:

1. Declare the candidate values for each option and arg. The candidate value can be an array or a function that receives typedArgs (an array of already typed command line string split by empty space) and returns an array of candidates.

```js
program
.arguments('<a> <b>')
.option('--verbose', 'verbose')
.option('-n, --name <name>', 'specify name')
.option('--description <desc>', 'specify description')
.complete({
options: {
'--name': function(typedArgs) { return ['kate', 'jim']; },
'--description': ['desc1', 'desc2']
},
arguments: {
a: function(typedArgs) { return ['a-1', 'a-2']; },
b: ['b-1', 'b-2']
}
});
```

2. Ask your command line user to enable autocompletion for current session by executing the following command.
For persistent support, we can recommend adding the command to their shell initialization file such as .bashrc or .zshrc etc.

```
# for zsh or bash
eval "$(<cli-program-name> --completion)"

# for fish shell
<cli-program-name> --completion-fish | source
```

## Automated --help

The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:

```
$ ./examples/pizza --help
Expand Down
34 changes: 34 additions & 0 deletions Readme_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ Commander 将会尝试在入口脚本(例如 `./examples/pm`)的目录中搜
* 在子命令脚本中加上 `#!/usr/bin/env node --harmony`。注意一些系统版本不支持此模式。
* 在指令调用时加上 `--harmony` 参数,例如 `node --harmony examples/pm publish`。`--harmony` 选项在开启子进程时会被保留。

## 自动补全

commander 已自带命令行自动补全功能。您可以按照以下步骤启用它:

1. 声明对应每个选项和参数的候选值。候选值可以是一个数组或者是一个 接受当前已经输入值的数组然后返回一个候选数组的function。

```js
program
.arguments('<a> <b>')
.option('--verbose', 'verbose')
.option('-n, --name <name>', 'specify name')
.option('--description <desc>', 'specify description')
.complete({
options: {
'--name': function(typedArgs) { return ['kate', 'jim']; },
'--description': ['desc1', 'desc2']
},
arguments: {
a: function(typedArgs) { return ['a-1', 'a-2']; },
b: ['b-1', 'b-2']
}
});
```

2. 邀请你的命令行用户开启自动补全功能。他们可以直接在shell执行以下指令,或是把其加入到个人shell对应的.bashrc或.zshrc等文件中,就可以获得长期的自动补全支持。

```
# for zsh or bash
eval "$(<cli-program-name> --completion)"

# for fish shell
<cli-program-name> --completion-fish | source
```

## 自动化帮助信息 --help

帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:
Expand Down
30 changes: 30 additions & 0 deletions examples/autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

/**
* To simulate real use cases please set the example path into PATH variable
* export PATH=".":$PATH
*
* Then please execute the following line to enable completion for your shell
* eval "$(autocomplete --completion)"
*
* And then you can try use tab completion after auto-complete command
*/

var program = require('..');

program
.arguments('<a> <b>')
.option('--verbose', 'verbose')
.option('-n, --name <name>', 'specify name')
.option('--description <desc>', 'specify description')
.complete({
options: {
'--name': function() { return ['kate', 'jim']; },
'--description': ['desc1', 'desc2']
},
arguments: {
a: function() { return ['a-1', 'a-2']; },
b: ['b-1', 'b-2']
}
})
.parse(process.argv);
46 changes: 46 additions & 0 deletions examples/autocomplete-sub
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node

/**
* To simulate real use cases please set the example path into PATH variable
* export PATH=".":$PATH
*
* Then please execute the following line to enable completion for your shell
* eval "$(autocomplete-sub --completion)"
*/

var program = require('..');

program
.command('sub1 <arg1> <arg2>')
.description('sub command 1')
.option('--verbose', 'verbose')
.option('-n, --name <name>', 'specify name')
.option('--description <desc>', 'specify description')
.complete({
options: {
'--name': function() { return ['kate', 'jim']; },
'--description': ['d1', 'd2']
},
arguments: {
arg1: function() { return ['a-1', 'a-2', 'a-3']; },
arg2: ['b-1', 'b-2']
}
});

program
.command('sub2 <arg1>')
.description('sub command 2')
.option('--verbose', 'verbose')
.option('-n, --name <name>', 'specify name')
.option('--description <desc>', 'specify description')
.complete({
options: {
'--name': function() { return ['lucy', 'linda']; },
'--description': ['db1', 'db2']
},
arguments: {
arg1: function() { return ['a-11', 'a-12']; },
}
});

program.parse(process.argv);
Loading