Skip to content

Commit a413c4d

Browse files
committed
complete flag completion implementation
1 parent 9a55f6f commit a413c4d

9 files changed

Lines changed: 113 additions & 5 deletions

File tree

‎lib/bashly/views/command/completion_flag_candidates.gtx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
if fixed_flags? || public_flags.any?
22
> if [[ $completion_current == -* ]]; then
33
if fixed_flags?
4-
> completion_flag_candidates --help "$completion_current" --help -h
4+
if short_flag_exist? '-h'
5+
> completion_flag_candidates --help "$completion_current" --help
6+
else
7+
> completion_flag_candidates --help "$completion_current" --help -h
8+
end
59
if root_command?
6-
> completion_flag_candidates --version "$completion_current" --version -v
10+
if short_flag_exist? '-v'
11+
> completion_flag_candidates --version "$completion_current" --version
12+
else
13+
> completion_flag_candidates --version "$completion_current" --version -v
14+
end
715
end
816
end
917
public_flags.each do |flag|

‎lib/bashly/views/command/completion_function.gtx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ end
88
if args.any?(&:unique)
99
> local -A completion_used_arguments=()
1010
end
11+
if flags.any?(&:unique)
12+
> local -A completion_used_flag_values=()
13+
end
1114
> shift
1215
>
1316
= render(:completion_word_filter).indent 2

‎lib/bashly/views/command/completion_word_filter.gtx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ public_commands.each do |command|
88
> ;;
99
end
1010
if fixed_flags?
11-
> --help | -h)
11+
if short_flag_exist? '-h'
12+
> --help)
13+
else
14+
> --help | -h)
15+
end
1216
> completion_blocked_flags["--help"]=1
1317
> shift
1418
> ;;
1519
if root_command?
16-
> --version | -v)
20+
if short_flag_exist? '-v'
21+
> --version)
22+
else
23+
> --version | -v)
24+
end
1725
> completion_blocked_flags["--version"]=1
1826
> shift
1927
> ;;

‎lib/bashly/views/flag/completion_filter_arg.gtx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
= render(:completion_value_candidates).indent 2
44
> return
55
> fi
6+
if unique
7+
> completion_used_flag_values["{{ name }}:$1"]=1
8+
end
69
> shift
710
= render :completion_filter_block

‎lib/bashly/views/flag/completion_value_candidates.gtx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@ if allowed
44
> "{{ value }}"
55
end
66
> )
7-
> completion_candidates "$completion_current" "${completion_values[@]}"
7+
if unique
8+
> local completion_value
9+
> for completion_value in "${completion_values[@]}"; do
10+
> if [[ -z "${completion_used_flag_values["{{ name }}:$completion_value"]:-}" ]]; then
11+
> completion_candidates "$completion_current" "$completion_value"
12+
> fi
13+
> done
14+
else
15+
> completion_candidates "$completion_current" "${completion_values[@]}"
16+
end
817
end

‎spec/fixtures/completions/core/examples.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ unique positional argument prefix:
128128
words: [release, stable, e]
129129
expected: [edge]
130130

131+
unique flag value:
132+
words: [release, --label, blue, --label, '']
133+
expected: [green, canary]
134+
135+
unique flag value prefix:
136+
words: [release, --label, blue, --label, c]
137+
expected: [canary]
138+
139+
unique repeatable flag remains:
140+
words: [release, --label, blue, '--']
141+
expected: [--help, --label]
142+
131143
private command is not reachable:
132144
words: [internal, '']
133145
expected: []

‎spec/fixtures/completions/core/src/bashly.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ commands:
3333
short: -c
3434
negatable: true
3535
help: Enable color output
36+
- long: --secret
37+
help: Internal flag that must not be suggested
38+
private: true
3639
- name: stop
3740
alias: down
3841
help: Stop a server
@@ -79,6 +82,14 @@ commands:
7982

8083
- name: release
8184
help: Release to channels
85+
flags:
86+
- long: --label
87+
short: -l
88+
arg: label
89+
allowed: [blue, green, canary]
90+
help: Labels to apply
91+
repeatable: true
92+
unique: true
8293
args:
8394
- name: channel
8495
allowed: [stable, beta, edge]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
global flag names:
2+
words: ['--']
3+
expected: [--help, --version, --profile, --verbose, --host]
4+
5+
global short flag names:
6+
words: ['-']
7+
expected: [--help, --version, --profile, -p, --verbose, -v, --host, -h]
8+
9+
global flag allowed values:
10+
words: [--profile, pro]
11+
expected: [production]
12+
13+
command after global flag:
14+
words: [--profile, production, '']
15+
expected: [deploy]
16+
17+
local flags after global flag and command:
18+
words: [--profile, production, deploy, '--']
19+
expected: [--help, --force]
20+
21+
custom version short flag remains repeatable:
22+
words: [-v, '--']
23+
expected: [--help, --version, --profile, --verbose, --host]
24+
25+
custom help short flag accepts its value:
26+
words: [-h, us]
27+
expected: [user]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: cli
2+
help: Global flag completion fixture
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --profile
7+
short: -p
8+
arg: profile
9+
allowed: [development, production]
10+
help: Select a profile
11+
- long: --verbose
12+
short: -v
13+
repeatable: true
14+
help: Increase verbosity
15+
- long: --host
16+
short: -h
17+
arg: host
18+
allowed: [user, admin]
19+
help: Select a host
20+
21+
commands:
22+
- name: deploy
23+
help: Deploy the application
24+
flags:
25+
- long: --force
26+
short: -f
27+
help: Force the deployment

0 commit comments

Comments
 (0)