Skip to content

Commit 5e04243

Browse files
authored
Merge pull request #297 from DannyBen/fix/completions
Fix bash completions for commands with global flags
2 parents 815735e + da6a0f0 commit 5e04243

File tree

10 files changed

+144
-54
lines changed

10 files changed

+144
-54
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ assignees: ''
1111

1212
### Reproduction steps
1313

14-
### Expected behavior
15-
1614
### Actual behavior
15+
16+
### Expected behavior

lib/bashly/concerns/completions.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@ module Bashly
66
module Completions
77
module Flag
88
def completion_data(command_full_name)
9-
result = {}
109
comps = allowed || completions
10+
return {} unless comps
1111

12-
if comps
13-
aliases.each do |name|
14-
result["#{command_full_name}*#{name}"] = comps
15-
end
12+
aliases.to_h do |name|
13+
prefix = command_full_name
14+
prefix = "#{prefix}*" unless prefix.end_with? '*'
15+
["#{prefix}#{name}", comps]
1616
end
17-
18-
result
1917
end
2018
end
2119

2220
module Command
2321
def completion_data(with_version: true)
2422
result = {}
2523

26-
all_full_names.each do |name|
24+
completion_full_names.each do |name|
25+
name = "#{name}*" if name.include? '*'
2726
result[name] = completion_words with_version: with_version
2827
flags.each do |flag|
2928
result.merge! flag.completion_data(name)
@@ -45,6 +44,17 @@ def completion_function(name = nil)
4544
completion_generator.wrapper_function name
4645
end
4746

47+
protected
48+
49+
def completion_full_names
50+
if parent_command
51+
glue = parent_command.global_flags? ? '*' : ' '
52+
parent_command.completion_full_names.product(aliases).map { |a| a.join glue }
53+
else
54+
aliases
55+
end
56+
end
57+
4858
private
4959

5060
def completion_generator

lib/bashly/script/command.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ def aliases
3434
[name] + alt
3535
end
3636

37-
# Returns an array of all full names (including aliases and aliases of
38-
# parents)
39-
def all_full_names
40-
if parent_command
41-
parent_command.all_full_names.product(aliases).map { |a| a.join ' ' }
42-
else
43-
aliases
44-
end
45-
end
46-
4737
# Returns an array of alternative aliases if any
4838
def alt
4939
# DEPRECATION 0.8.0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
cli:
3+
- "--debug"
4+
- "--help"
5+
- "--version"
6+
- "-h"
7+
- "-v"
8+
- images
9+
cli*images*:
10+
- "--help"
11+
- "--verbose"
12+
- "-h"
13+
- ls
14+
cli*images*ls*:
15+
- "--env"
16+
- "--help"
17+
- "-h"
18+
cli*images*ls*--env:
19+
- prod
20+
- dev
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
cli:
3+
- "--help"
4+
- "--version"
5+
- "-h"
6+
- "-v"
7+
- images
8+
cli images:
9+
- "--help"
10+
- "--verbose"
11+
- "-h"
12+
- ls
13+
cli images*ls*:
14+
- "--help"
15+
- "-h"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
cli:
3+
- "--debug"
4+
- "--help"
5+
- "--version"
6+
- "-h"
7+
- "-v"
8+
- images
9+
cli*images*:
10+
- "--help"
11+
- "-h"
12+
- ls
13+
cli*images ls*:
14+
- "--env"
15+
- "--help"
16+
- "-h"
17+
cli*images ls*--env:
18+
- prod
19+
- dev

spec/approvals/script/command/nested_aliases

Lines changed: 0 additions & 25 deletions
This file was deleted.

spec/bashly/concerns/completions_command_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,37 @@
5959
end
6060
end
6161
end
62+
63+
context 'with a command that has global flags on the root command' do
64+
let(:fixture) { :completions_global_flags_root }
65+
66+
describe '#completion_data' do
67+
it 'returns a data structure that includes all command full names' do
68+
expect(subject.completion_data.to_yaml)
69+
.to match_approval('completions/completion_global_flags_root')
70+
end
71+
end
72+
end
73+
74+
context 'with a command that has global flags on a nested command' do
75+
let(:fixture) { :completions_global_flags_nested }
76+
77+
describe '#completion_data' do
78+
it 'returns a data structure that includes all command full names' do
79+
expect(subject.completion_data.to_yaml)
80+
.to match_approval('completions/completion_global_flags_nested')
81+
end
82+
end
83+
end
84+
85+
context 'with a command that has global flags on the root and a nested command' do
86+
let(:fixture) { :completions_global_flags }
87+
88+
describe '#completion_data' do
89+
it 'returns a data structure that includes all command full names' do
90+
expect(subject.completion_data.to_yaml)
91+
.to match_approval('completions/completion_global_flags')
92+
end
93+
end
94+
end
6295
end

spec/bashly/script/command_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@
5050
end
5151
end
5252

53-
describe '#all_full_names' do
54-
let(:fixture) { :nested_aliases }
55-
56-
it 'returns an array of all full names including aliases' do
57-
expect(subject.deep_commands.last.all_full_names.to_yaml)
58-
.to match_approval('script/command/nested_aliases')
59-
end
60-
end
61-
6253
describe '#args' do
6354
it 'returns an array of Argument objects' do
6455
expect(subject.args).to be_an Array

spec/fixtures/script/commands.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@
9898
arg: name
9999
allowed: [get, post]
100100

101+
:completions_global_flags:
102+
name: cli
103+
flags:
104+
- long: --debug
105+
commands:
106+
- name: images
107+
flags:
108+
- long: --verbose
109+
commands:
110+
- name: ls
111+
flags:
112+
- long: --env
113+
arg: env
114+
allowed: [prod, dev]
115+
116+
:completions_global_flags_nested:
117+
name: cli
118+
commands:
119+
- name: images
120+
flags:
121+
- long: --verbose
122+
commands:
123+
- name: ls
124+
125+
:completions_global_flags_root:
126+
name: cli
127+
flags:
128+
- long: --debug
129+
commands:
130+
- name: images
131+
commands:
132+
- name: ls
133+
flags:
134+
- long: --env
135+
arg: env
136+
allowed: [prod, dev]
137+
101138
:custom_filename:
102139
name: run
103140
filename: ops/run_command.sh

0 commit comments

Comments
 (0)