Skip to content

Commit be9b05c

Browse files
committed
- Improve bash completion generation
1 parent fdeb788 commit be9b05c

File tree

15 files changed

+196
-97
lines changed

15 files changed

+196
-97
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ gem 'runfile'
88
gem 'runfile-tasks'
99
gem 'simplecov'
1010

11+
gem 'completely', path: '/vagrant/gems/completely'
12+
1113
gemspec
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
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}"'
10+
echo $' local cur compline'
11+
echo $' _init_completion -s || return'
12+
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
13+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
1114
echo $''
12-
echo $' case "$comp_line" in'
15+
echo $' case "$compline" in'
1316
echo $' \'completions\'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;'
1417
echo $' \'download\'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;'
1518
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")) ;;'
19+
echo $' \'d\'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;'
20+
echo $' \'u\'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;'
21+
echo $' *) COMPREPLY=($(compgen -W "--help --version -h -v completions d download u upload" -- "$cur")) ;;'
1722
echo $' esac'
18-
echo $'}'
19-
echo $''
23+
echo $'} &&'
2024
echo $'complete -F _cli_completions cli'
25+
echo $''
26+
echo $'# ex: filetype=sh'
2127
}

lib/bashly/concerns/command_scopes.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
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+
413
# Returns only the names of the Commands
514
def command_names
615
commands.map &:name
716
end
817

18+
# Returns a full list of the Command names and aliases combined
19+
def command_aliases
20+
commands.map(&:aliases).flatten
21+
end
22+
923
# Returns a flat array containing all the commands in this tree.
1024
# This includes self + children + grandchildres + ...
1125
def deep_commands

lib/bashly/concerns/completions.rb

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,76 @@
11
require 'completely'
22

33
module Bashly
4-
# This is a `Command` concern responsible for providing bash completion data
4+
# This is a `Command` and `Flag` concern responsible for providing bash
5+
# completion data
56
module Completions
6-
def completion_data(with_version: true)
7-
result = { full_name => completion_words(with_version: with_version) }
8-
9-
commands.each do |command|
10-
result.merge! command.completion_data(with_version: false)
11-
end
7+
module Flag
8+
def completion_data(command_full_name)
9+
result = {}
10+
comps = allowed || completions
1211

13-
result
14-
end
12+
if comps
13+
aliases.each do |name|
14+
result["#{command_full_name}*#{name}"] = comps
15+
end
16+
end
1517

16-
def completion_script
17-
completion_generator.script
18+
result
19+
end
1820
end
1921

20-
def completion_function(name = nil)
21-
completion_generator.wrapper_function(name)
22-
end
22+
module Command
23+
def completion_data(with_version: true)
24+
result = {}
2325

24-
private
26+
all_full_names.each do |name|
27+
result[name] = completion_words(with_version: with_version)
28+
flags.each do |flag|
29+
result.merge! flag.completion_data(name)
30+
end
31+
end
32+
33+
commands.each do |command|
34+
result.merge! command.completion_data(with_version: false)
35+
end
2536

26-
def completion_generator
27-
Completely::Completions.new(completion_data)
28-
end
37+
result
38+
end
2939

30-
def completion_flag_names
31-
flags.map(&:name) + flags.map(&:short)
32-
end
40+
def completion_script
41+
completion_generator.script
42+
end
3343

34-
def completion_allowed_args
35-
flags.map(&:allowed).flatten + args.map(&:allowed).flatten
36-
end
44+
def completion_function(name = nil)
45+
completion_generator.wrapper_function(name)
46+
end
3747

38-
def completion_words(with_version: false)
39-
trivial_flags = %w[--help -h]
40-
trivial_flags += %w[--version -v] if with_version
41-
all = (
42-
command_names + trivial_flags +
43-
completion_flag_names + completion_allowed_args
44-
)
48+
private
4549

46-
all += completions if completions
47-
all.compact.uniq.sort
48-
end
50+
def completion_generator
51+
Completely::Completions.new(completion_data)
52+
end
53+
54+
def completion_flag_names
55+
flags.map(&:name) + flags.map(&:short)
56+
end
57+
58+
def completion_allowed_args
59+
args.map(&:allowed).flatten
60+
end
61+
62+
def completion_words(with_version: false)
63+
trivial_flags = %w[--help -h]
64+
trivial_flags += %w[--version -v] if with_version
65+
all = (
66+
command_aliases + trivial_flags +
67+
completion_flag_names + completion_allowed_args
68+
)
4969

70+
all += completions if completions
71+
all.compact.uniq.sort
72+
end
73+
74+
end
5075
end
51-
end
76+
end

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Bashly
22
module Script
33
class Command < Base
4-
include Completions
4+
include Completions::Command
55
include CommandScopes
66

77
class << self

lib/bashly/script/flag.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Bashly
22
module Script
33
class Flag < Base
4+
include Completions::Flag
5+
46
class << self
57
def option_keys
68
@option_keys ||= %i[
7-
allowed arg conflicts default help long repeatable required
8-
short validate
9+
allowed arg completions conflicts default help long repeatable
10+
required short validate
911
]
1012
end
1113
end
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
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}"'
10+
echo $' local cur compline'
11+
echo $' _init_completion -s || return'
12+
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
13+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
1114
echo $''
12-
echo $' case "$comp_line" in'
15+
echo $' case "$compline" in'
1316
echo $' \'download\'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;'
1417
echo $' \'upload\'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;'
15-
echo $' \'\'*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;'
18+
echo $' \'d\'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;'
19+
echo $' \'u\'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;'
20+
echo $' *) COMPREPLY=($(compgen -W "--help --version -h -v d download u upload" -- "$cur")) ;;'
1621
echo $' esac'
17-
echo $'}'
18-
echo $''
22+
echo $'} &&'
1923
echo $'complete -F _cli_completions cli'
24+
echo $''
25+
echo $'# ex: filetype=sh'
2026
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
#!/usr/bin/env bash
1+
# cli completion -*- shell-script -*-
22

33
# This bash completions script was generated by
44
# completely (https://github.com/dannyben/completely)
55
# Modifying it manually is not recommended
6+
67
_cli_completions() {
7-
local cur=${COMP_WORDS[COMP_CWORD]}
8-
local comp_line="${COMP_WORDS[@]:1}"
8+
local cur compline
9+
_init_completion -s || return
10+
cur=${COMP_WORDS[COMP_CWORD]}
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
912

10-
case "$comp_line" in
13+
case "$compline" in
1114
'download'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
1215
'upload'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
13-
''*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;
16+
'd'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
17+
'u'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
18+
*) COMPREPLY=($(compgen -W "--help --version -h -v d download u upload" -- "$cur")) ;;
1419
esac
15-
}
16-
20+
} &&
1721
complete -F _cli_completions cli
22+
23+
# ex: filetype=sh

spec/approvals/cli/add/comp-yaml-file

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@ cli:
44
- "--version"
55
- "-h"
66
- "-v"
7+
- d
78
- download
9+
- u
810
- upload
911
cli download:
1012
- "--force"
1113
- "--help"
1214
- "-f"
1315
- "-h"
16+
cli d:
17+
- "--force"
18+
- "--help"
19+
- "-f"
20+
- "-h"
1421
cli upload:
1522
- "--help"
1623
- "--password"
1724
- "--user"
1825
- "-h"
1926
- "-p"
2027
- "-u"
28+
cli u:
29+
- "--help"
30+
- "--password"
31+
- "--user"
32+
- "-h"
33+
- "-p"
34+
- "-u"
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
custom_name() {
2-
echo $'#!/usr/bin/env bash'
2+
echo $'# get completion -*- shell-script -*-'
33
echo $''
44
echo $'# This bash completions script was generated by'
55
echo $'# completely (https://github.com/dannyben/completely)'
66
echo $'# Modifying it manually is not recommended'
7+
echo $''
78
echo $'_get_completions() {'
8-
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
9-
echo $' local comp_line="${COMP_WORDS[@]:1}"'
9+
echo $' local cur compline'
10+
echo $' _init_completion -s || return'
11+
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
12+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
1013
echo $''
11-
echo $' case "$comp_line" in'
12-
echo $' \'\'*) COMPREPLY=($(compgen -A file -W "--force --help --verbose --version -h -v" -- "$cur")) ;;'
14+
echo $' case "$compline" in'
15+
echo $' *) COMPREPLY=($(compgen -A file -W "--force --help --verbose --version -h -v" -- "$cur")) ;;'
1316
echo $' esac'
14-
echo $'}'
15-
echo $''
17+
echo $'} &&'
1618
echo $'complete -F _get_completions get'
19+
echo $''
20+
echo $'# ex: filetype=sh'
1721
}

0 commit comments

Comments
 (0)