Skip to content
Merged
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
45 changes: 12 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,50 +349,29 @@ functionality to your script in one of three ways:

The bash completions generation is completely automatic, and you will have to
rerun the `bashly add comp *` command whenever you change your `bashly.yml`
script.
file.

In addition to suggesting subcommands and flags, you can instruct bashly to
also suggest files, directories, users and more. To do this, add another option
in your `bashly.yml` on the command you wish to alter:
also suggest files, directories, users, git branches and more. To do this,
add another option in your `bashly.yml` on the command you wish to alter:

```yaml
# bashly.yml
commands:
- name: upload
help: Upload a file
completions: [directory, user]
completions:
- <directory>
- <user>
- $(git branch 2> /dev/null)

```

Valid completion additions are:

| Keyword | Meaning
|-------------|---------------------
| `alias` | Alias names
| `arrayvar` | Array variable names
| `binding` | Readline key binding names
| `builtin` | Names of shell builtin commands
| `command` | Command names
| `directory` | Directory names
| `disabled` | Names of disabled shell builtins
| `enabled` | Names of enabled shell builtins
| `export` | Names of exported shell variables
| `file` | File names
| `function` | Names of shell functions
| `group` | Group names
| `helptopic` | Help topics as accepted by the help builtin
| `hostname` | Hostnames, as taken from the file specified by the HOSTFILE shell variable
| `job` | Job names
| `keyword` | Shell reserved words
| `running` | Names of running jobs
| `service` | Service names
| `signal` | Signal names
| `stopped` | Names of stopped jobs
| `user` | User names
| `variable` | Names of all shell variables

Note that these are taken from the [Programmable Completion Builtin][compgen],
and will simply be added using the `compgen -A action` command.
- Anything between `<...>` will be added using the `compgen -A action` flag.
- Anything else, will be appended to the `compgen -W` flag.

For more information about these custom completions, see the documentation for
the [completely][completely] gem.


## Real World Examples
Expand Down
7 changes: 5 additions & 2 deletions examples/completions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ commands:
- name: download
short: d
help: Download a file
completions: [file]
completions:
- <file>

args:
- name: source
Expand All @@ -51,7 +52,9 @@ commands:
- name: upload
short: u
help: Upload a file
completions: [directory, user]
completions:
- <directory>
- <user>
args:
- name: source
required: true
Expand Down
7 changes: 5 additions & 2 deletions examples/completions/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ commands:
- name: download
short: d
help: Download a file
completions: [file]
completions:
- <file>

args:
- name: source
Expand All @@ -36,7 +37,9 @@ commands:
- name: upload
short: u
help: Upload a file
completions: [directory, user]
completions:
- <directory>
- <user>
args:
- name: source
required: true
Expand Down
7 changes: 2 additions & 5 deletions lib/bashly/concerns/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ def completion_flag_names
flags.map(&:name) + flags.map(&:short)
end

def completion_actions
completions ? completions.map { |c| "<#{c}>" } : []
end

def completion_words(with_version: false)
trivial_flags = %w[--help -h]
trivial_flags += %w[--version -v] if with_version
all = (
command_names + trivial_flags +
completion_flag_names + completion_actions
completion_flag_names
)

all += completions if completions
all.compact.uniq.sort
end

Expand Down
1 change: 1 addition & 0 deletions spec/approvals/completions/advanced
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ say goodbye:
- "-h"
- universe
say goodbye universe:
- "$(git branch)"
- "--color"
- "--help"
- "--verbose"
Expand Down
2 changes: 1 addition & 1 deletion spec/approvals/completions/script
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _say_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}

case "$COMP_LINE" in
'say goodbye universe'*) COMPREPLY=($(compgen -W "--color --help --verbose -c -h -v" -- "$cur")) ;;
'say goodbye universe'*) COMPREPLY=($(compgen -W "$(git branch) --color --help --verbose -c -h -v" -- "$cur")) ;;
'say hello world'*) COMPREPLY=($(compgen -A directory -A user -W "--force --help --verbose -h" -- "$cur")) ;;
'say goodbye'*) COMPREPLY=($(compgen -W "--help -h universe" -- "$cur")) ;;
'say hello'*) COMPREPLY=($(compgen -W "--help -h world" -- "$cur")) ;;
Expand Down
2 changes: 1 addition & 1 deletion spec/bashly/models/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
end
end

describe '#catch_all_help', :focus do
describe '#catch_all_help' do
context "when catch_all is disabled" do
it "returns nil" do
expect(subject.catch_all_help).to be_nil
Expand Down
9 changes: 7 additions & 2 deletions spec/fixtures/models/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@

:completions_simple:
name: get
completions: [file]
completions:
- <file>

flags:
- long: --force
Expand All @@ -142,13 +143,17 @@
- name: hello
commands:
- name: world
completions: [directory, user]
completions:
- <directory>
- <user>
flags:
- long: --force
- long: --verbose
- name: goodbye
commands:
- name: universe
completions:
- $(git branch)
flags:
- long: --color
short: -c
Expand Down