Skip to content

Commit 8f85641

Browse files
authored
Merge pull request #225 from DannyBen/improve/flag-arg-completion
Improve bash completion generation
2 parents fdeb788 + 71dd64d commit 8f85641

File tree

29 files changed

+651
-217
lines changed

29 files changed

+651
-217
lines changed

bashly.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ Gem::Specification.new do |s|
1717
s.license = 'MIT'
1818
s.required_ruby_version = ">= 2.7.0"
1919

20-
s.add_runtime_dependency 'colsole', '~> 0.6'
21-
s.add_runtime_dependency 'completely', '~> 0.3'
20+
s.add_runtime_dependency 'colsole', '~> 0.7'
21+
s.add_runtime_dependency 'completely', '~> 0.4.0'
2222
s.add_runtime_dependency 'mister_bin', '~> 0.7'
23-
s.add_runtime_dependency 'requires', '~> 0.1'
23+
s.add_runtime_dependency 'requires', '~> 0.2'
2424

2525
s.metadata = {
2626
"bug_tracker_uri" => "https://github.com/DannyBen/bashly/issues",

examples/completions/README.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ name: cli
2424
help: Sample application with bash completions
2525
version: 0.1.0
2626

27+
# All commands and flags will be automatically used in the completions script
2728
commands:
2829
- name: completions
2930
help: |-
@@ -33,6 +34,9 @@ commands:
3334
- name: download
3435
alias: d
3536
help: Download a file
37+
38+
# Adding custom completions for a command. In this case, typing
39+
# `cli download <TAB>` will suggest files.
3640
completions:
3741
- <file>
3842

@@ -47,6 +51,14 @@ commands:
4751
- long: --force
4852
short: -f
4953
help: Overwrite existing files
54+
- long: --handler
55+
arg: command
56+
57+
# The allowed flag arg whitelist will be added automatically. In this case,
58+
# typing `cli download --handler <tab>` will suggest curl or wget
59+
allowed:
60+
- curl
61+
- wget
5062

5163
examples:
5264
- cli download example.com
@@ -59,20 +71,36 @@ commands:
5971
- name: upload
6072
alias: u
6173
help: Upload a file
74+
75+
# Add directories and users to the suggested completions.
6276
completions:
6377
- <directory>
6478
- <user>
79+
6580
args:
6681
- name: source
6782
required: true
6883
help: File to upload
6984

85+
# The allowed argument whitelist will be added automatically. In this case
86+
# typing `cli upload <TAB>` will suggest these files.
87+
allowed:
88+
- README.md
89+
- CHANGELOG.md
90+
7091
flags:
7192
- long: --user
7293
short: -u
7394
arg: user
7495
help: Username to use for logging in
7596
required: true
97+
98+
# Adding completions to a flag with an argument will add it to the suggested
99+
# words list. In this case typing `cli upload --user <TAB>` will suggest
100+
# users.
101+
completions:
102+
- <user>
103+
76104
- long: --password
77105
short: -p
78106
arg: password
@@ -165,25 +193,37 @@ Options:
165193
### `$ ./cli completions`
166194

167195
```shell
168-
#!/usr/bin/env bash
196+
# cli completion -*- shell-script -*-
169197

170198
# This bash completions script was generated by
171199
# completely (https://github.com/dannyben/completely)
172200
# Modifying it manually is not recommended
173-
_cli_completions() {
174-
local cur=${COMP_WORDS[COMP_CWORD]}
175-
local comp_line="${COMP_WORDS[@]:1}"
176201

177-
case "$comp_line" in
202+
_cli_completions() {
203+
local cur compline
204+
_init_completion -s || return
205+
cur=${COMP_WORDS[COMP_CWORD]}
206+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
207+
208+
case "$compline" in
209+
'download'*'--handler') COMPREPLY=($(compgen -W "curl wget" -- "$cur")) ;;
210+
'upload'*'--user') COMPREPLY=($(compgen -A user -- "$cur")) ;;
178211
'completions'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;
179-
'download'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;
180-
'upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;
181-
''*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;
212+
'd'*'--handler') COMPREPLY=($(compgen -W "curl wget" -- "$cur")) ;;
213+
'upload'*'-u') COMPREPLY=($(compgen -A user -- "$cur")) ;;
214+
'download'*) COMPREPLY=($(compgen -A file -W "--force --handler --help -f -h" -- "$cur")) ;;
215+
'u'*'--user') COMPREPLY=($(compgen -A user -- "$cur")) ;;
216+
'upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u CHANGELOG.md README.md" -- "$cur")) ;;
217+
'u'*'-u') COMPREPLY=($(compgen -A user -- "$cur")) ;;
218+
'd'*) COMPREPLY=($(compgen -A file -W "--force --handler --help -f -h" -- "$cur")) ;;
219+
'u'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u CHANGELOG.md README.md" -- "$cur")) ;;
220+
*) COMPREPLY=($(compgen -W "--help --version -h -v completions d download u upload" -- "$cur")) ;;
182221
esac
183-
}
184-
222+
} &&
185223
complete -F _cli_completions cli
186224

225+
# ex: filetype=sh
226+
187227

188228
```
189229

examples/completions/src/bashly.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: cli
22
help: Sample application with bash completions
33
version: 0.1.0
44

5+
# All commands and flags will be automatically used in the completions script
56
commands:
67
- name: completions
78
help: |-
@@ -11,6 +12,9 @@ commands:
1112
- name: download
1213
alias: d
1314
help: Download a file
15+
16+
# Adding custom completions for a command. In this case, typing
17+
# `cli download <TAB>` will suggest files.
1418
completions:
1519
- <file>
1620

@@ -25,6 +29,14 @@ commands:
2529
- long: --force
2630
short: -f
2731
help: Overwrite existing files
32+
- long: --handler
33+
arg: command
34+
35+
# The allowed flag arg whitelist will be added automatically. In this case,
36+
# typing `cli download --handler <tab>` will suggest curl or wget
37+
allowed:
38+
- curl
39+
- wget
2840

2941
examples:
3042
- cli download example.com
@@ -37,20 +49,36 @@ commands:
3749
- name: upload
3850
alias: u
3951
help: Upload a file
52+
53+
# Add directories and users to the suggested completions.
4054
completions:
4155
- <directory>
4256
- <user>
57+
4358
args:
4459
- name: source
4560
required: true
4661
help: File to upload
4762

63+
# The allowed argument whitelist will be added automatically. In this case
64+
# typing `cli upload <TAB>` will suggest these files.
65+
allowed:
66+
- README.md
67+
- CHANGELOG.md
68+
4869
flags:
4970
- long: --user
5071
short: -u
5172
arg: user
5273
help: Username to use for logging in
5374
required: true
75+
76+
# Adding completions to a flag with an argument will add it to the suggested
77+
# words list. In this case typing `cli upload --user <TAB>` will suggest
78+
# users.
79+
completions:
80+
- <user>
81+
5482
- long: --password
5583
short: -p
5684
arg: password
Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
11
## [@bashly-upgrade completions send_completions]
22
send_completions() {
3-
echo $'#!/usr/bin/env bash'
3+
echo $'# cli completion -*- shell-script -*-'
44
echo $''
55
echo $'# This bash completions script was generated by'
66
echo $'# completely (https://github.com/dannyben/completely)'
77
echo $'# Modifying it manually is not recommended'
8+
echo $''
89
echo $'_cli_completions() {'
9-
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
10-
echo $' local comp_line="${COMP_WORDS[@]:1}"'
11-
echo $''
12-
echo $' case "$comp_line" in'
13-
echo $' \'completions\'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;'
14-
echo $' \'download\'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;'
15-
echo $' \'upload\'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;'
16-
echo $' \'\'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;'
17-
echo $' esac'
18-
echo $'}'
10+
echo $' local cur compline'
11+
echo $' _init_completion -s || return'
12+
echo $''
13+
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
14+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
15+
echo $''
16+
echo $' case "$compline" in'
17+
echo $' \'download\'*\'--handler\')'
18+
echo $' COMPREPLY=($(compgen -W "curl wget" -- "$cur"))'
19+
echo $' ;;'
20+
echo $''
21+
echo $' \'upload\'*\'--user\')'
22+
echo $' COMPREPLY=($(compgen -A user -- "$cur"))'
23+
echo $' ;;'
24+
echo $''
25+
echo $' \'completions\'*)'
26+
echo $' COMPREPLY=($(compgen -W "--help -h" -- "$cur"))'
27+
echo $' ;;'
28+
echo $''
29+
echo $' \'d\'*\'--handler\')'
30+
echo $' COMPREPLY=($(compgen -W "curl wget" -- "$cur"))'
31+
echo $' ;;'
32+
echo $''
33+
echo $' \'upload\'*\'-u\')'
34+
echo $' COMPREPLY=($(compgen -A user -- "$cur"))'
35+
echo $' ;;'
36+
echo $''
37+
echo $' \'download\'*)'
38+
echo $' COMPREPLY=($(compgen -A file -W "--force --handler --help -f -h" -- "$cur"))'
39+
echo $' ;;'
1940
echo $''
41+
echo $' \'u\'*\'--user\')'
42+
echo $' COMPREPLY=($(compgen -A user -- "$cur"))'
43+
echo $' ;;'
44+
echo $''
45+
echo $' \'upload\'*)'
46+
echo $' COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u CHANGELOG.md README.md" -- "$cur"))'
47+
echo $' ;;'
48+
echo $''
49+
echo $' \'u\'*\'-u\')'
50+
echo $' COMPREPLY=($(compgen -A user -- "$cur"))'
51+
echo $' ;;'
52+
echo $''
53+
echo $' \'d\'*)'
54+
echo $' COMPREPLY=($(compgen -A file -W "--force --handler --help -f -h" -- "$cur"))'
55+
echo $' ;;'
56+
echo $''
57+
echo $' \'u\'*)'
58+
echo $' COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u CHANGELOG.md README.md" -- "$cur"))'
59+
echo $' ;;'
60+
echo $''
61+
echo $' *)'
62+
echo $' COMPREPLY=($(compgen -W "--help --version -h -v completions d download u upload" -- "$cur"))'
63+
echo $' ;;'
64+
echo $''
65+
echo $' esac'
66+
echo $'} &&'
2067
echo $'complete -F _cli_completions cli'
68+
echo $''
69+
echo $'# ex: filetype=sh'
2170
}

lib/bashly/concerns/command_scopes.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
module Bashly
22
# This is a `Command` concern responsible for providing additional scopes.
33
module CommandScopes
4+
# Returns an array of all full names (including aliases)
5+
def all_full_names
6+
[full_name] + if parents.any?
7+
alt.map { |a| (parents + [a]).join(' ') }
8+
else
9+
alt
10+
end
11+
end
12+
13+
# Returns a full list of the Command names and aliases combined
14+
def command_aliases
15+
commands.map(&:aliases).flatten
16+
end
17+
418
# Returns only the names of the Commands
519
def command_names
620
commands.map &:name

0 commit comments

Comments
 (0)