Skip to content

Commit 96d7c00

Browse files
authored
Merge pull request #134 from DannyBen/add/whitelist-to-completions
Automatically add allowed arguments to completions
2 parents 8aa0275 + 2ce5d2b commit 96d7c00

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

lib/bashly/concerns/completions.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ def completion_flag_names
3131
flags.map(&:name) + flags.map(&:short)
3232
end
3333

34+
def completion_allowed_args
35+
flags.map(&:allowed).flatten + args.map(&:allowed).flatten
36+
end
37+
3438
def completion_words(with_version: false)
3539
trivial_flags = %w[--help -h]
3640
trivial_flags += %w[--version -v] if with_version
3741
all = (
3842
command_names + trivial_flags +
39-
completion_flag_names
43+
completion_flag_names + completion_allowed_args
4044
)
4145

4246
all += completions if completions

spec/approvals/completions/whitelist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
download:
3+
- "--help"
4+
- "--method"
5+
- "--role"
6+
- "--version"
7+
- "-h"
8+
- "-v"
9+
- '22'
10+
- '3000'
11+
- '80'
12+
- admin
13+
- get
14+
- https
15+
- post
16+
- ssh
17+
- user

spec/bashly/concerns/completions_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,15 @@
3838
end
3939
end
4040
end
41+
42+
context "with a command that uses whitelist args" do
43+
let(:fixture) { :completions_whitelist }
44+
45+
describe '#completion_data' do
46+
it "returns a data structure that includes thw whitelist" do
47+
expect(subject.completion_data.to_yaml)
48+
.to match_approval("completions/whitelist")
49+
end
50+
end
51+
end
4152
end

spec/fixtures/models/commands.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,20 @@
161161
- long: --verbose
162162
short: -v
163163

164+
:completions_whitelist:
165+
name: download
166+
args:
167+
- name: protocol
168+
allowed: [https, ssh]
169+
- name: port
170+
allowed: ["80", "22", "3000"]
171+
flags:
172+
- long: --role
173+
arg: name
174+
allowed: [user, admin]
175+
- long: --method
176+
arg: name
177+
allowed: [get, post]
178+
164179
:custom_header:
165180
name: check

0 commit comments

Comments
 (0)