Skip to content

Commit 6c52c52

Browse files
committed
- Fix bash completions for commands with global flags
1 parent 815735e commit 6c52c52

File tree

10 files changed

+143
-48
lines changed

10 files changed

+143
-48
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def completion_data(command_full_name)
1111

1212
if comps
1313
aliases.each do |name|
14-
result["#{command_full_name}*#{name}"] = comps
14+
prefix = command_full_name
15+
prefix = "#{prefix}*" unless prefix.end_with? '*'
16+
result["#{prefix}#{name}"] = comps
1517
end
1618
end
1719

@@ -23,7 +25,8 @@ module Command
2325
def completion_data(with_version: true)
2426
result = {}
2527

26-
all_full_names.each do |name|
28+
completion_full_names.each do |name|
29+
name = "#{name}*" if name.include? '*'
2730
result[name] = completion_words with_version: with_version
2831
flags.each do |flag|
2932
result.merge! flag.completion_data(name)
@@ -45,6 +48,17 @@ def completion_function(name = nil)
4548
completion_generator.wrapper_function name
4649
end
4750

51+
protected
52+
53+
def completion_full_names
54+
if parent_command
55+
glue = parent_command.global_flags? ? '*' : ' '
56+
parent_command.completion_full_names.product(aliases).map { |a| a.join glue }
57+
else
58+
aliases
59+
end
60+
end
61+
4862
private
4963

5064
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,44 @@
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+
117+
:completions_global_flags_nested:
118+
name: cli
119+
commands:
120+
- name: images
121+
flags:
122+
- long: --verbose
123+
commands:
124+
- name: ls
125+
126+
:completions_global_flags_root:
127+
name: cli
128+
flags:
129+
- long: --debug
130+
commands:
131+
- name: images
132+
commands:
133+
- name: ls
134+
flags:
135+
- long: --env
136+
arg: env
137+
allowed: [prod, dev]
138+
101139
:custom_filename:
102140
name: run
103141
filename: ops/run_command.sh

0 commit comments

Comments
 (0)