Skip to content

Commit 52160e2

Browse files
committed
approvals
1 parent 0aa439b commit 52160e2

File tree

8 files changed

+156
-164
lines changed

8 files changed

+156
-164
lines changed

examples/completions/src/lib/send_completions.sh

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,37 @@ send_completions() {
77
echo $'# Modifying it manually is not recommended'
88
echo $''
99
echo $'_cli_completions_filter() {'
10-
echo $' local words="$1"'
10+
echo $' local words=("$@")'
1111
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1212
echo $' local result=()'
13+
echo $' local want_options=0'
1314
echo $''
1415
echo $' # words the user already typed (excluding the command itself)'
1516
echo $' local used=()'
1617
echo $' if ((COMP_CWORD > 1)); then'
1718
echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
1819
echo $' fi'
1920
echo $''
20-
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
21-
echo $' # Completing an option: offer everything (including options)'
22-
echo $' echo "$words"'
23-
echo $''
24-
echo $' else'
25-
echo $' # Completing a non-option: offer only non-options,'
26-
echo $' # and don\'t re-offer ones already used earlier in the line.'
27-
echo $' for word in $words; do'
21+
echo $' # Completing an option: offer everything.'
22+
echo $' # Completing a non-option: drop options and already-used words.'
23+
echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
24+
echo $' for word in "${words[@]}"; do'
25+
echo $' if ((!want_options)); then'
2826
echo $' [[ "${word:0:1}" == "-" ]] && continue'
2927
echo $''
30-
echo $' local seen=0'
3128
echo $' for u in "${used[@]}"; do'
3229
echo $' if [[ "$u" == "$word" ]]; then'
33-
echo $' seen=1'
34-
echo $' break'
30+
echo $' continue 2'
3531
echo $' fi'
3632
echo $' done'
37-
echo $' ((!seen)) && result+=("$word")'
38-
echo $' done'
33+
echo $' fi'
3934
echo $''
40-
echo $' echo "${result[*]}"'
41-
echo $' fi'
35+
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
36+
echo $' printf -v word \'%q\' "$word"'
37+
echo $' result+=("$word")'
38+
echo $' done'
39+
echo $''
40+
echo $' echo "${result[*]}"'
4241
echo $'}'
4342
echo $''
4443
echo $'_cli_completions() {'
@@ -53,51 +52,51 @@ send_completions() {
5352
echo $''
5453
echo $' case "$compline" in'
5554
echo $' \'download\'*\'--handler\')'
56-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "curl wget")" -- "$cur")'
55+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "curl" "wget")" -- "$cur")'
5756
echo $' ;;'
5857
echo $''
5958
echo $' \'upload\'*\'--user\')'
6059
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A user -- "$cur")'
6160
echo $' ;;'
6261
echo $''
6362
echo $' \'completions\'*)'
64-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help -h")" -- "$cur")'
63+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "-h")" -- "$cur")'
6564
echo $' ;;'
6665
echo $''
6766
echo $' \'d\'*\'--handler\')'
68-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "curl wget")" -- "$cur")'
67+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "curl" "wget")" -- "$cur")'
6968
echo $' ;;'
7069
echo $''
7170
echo $' \'upload\'*\'-u\')'
7271
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A user -- "$cur")'
7372
echo $' ;;'
7473
echo $''
7574
echo $' \'download\'*)'
76-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_cli_completions_filter "--force --handler --help -f -h")" -- "$cur")'
75+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_cli_completions_filter "--force" "--handler" "--help" "-f" "-h")" -- "$cur")'
7776
echo $' ;;'
7877
echo $''
7978
echo $' \'u\'*\'--user\')'
8079
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A user -- "$cur")'
8180
echo $' ;;'
8281
echo $''
8382
echo $' \'upload\'*)'
84-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -A user -W "$(_cli_completions_filter "--help --password --user -h -p -u CHANGELOG.md README.md")" -- "$cur")'
83+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -A user -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u" "CHANGELOG.md" "README.md")" -- "$cur")'
8584
echo $' ;;'
8685
echo $''
8786
echo $' \'u\'*\'-u\')'
8887
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A user -- "$cur")'
8988
echo $' ;;'
9089
echo $''
9190
echo $' \'d\'*)'
92-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_cli_completions_filter "--force --handler --help -f -h")" -- "$cur")'
91+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_cli_completions_filter "--force" "--handler" "--help" "-f" "-h")" -- "$cur")'
9392
echo $' ;;'
9493
echo $''
9594
echo $' \'u\'*)'
96-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -A user -W "$(_cli_completions_filter "--help --password --user -h -p -u CHANGELOG.md README.md")" -- "$cur")'
95+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -A user -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u" "CHANGELOG.md" "README.md")" -- "$cur")'
9796
echo $' ;;'
9897
echo $''
9998
echo $' *)'
100-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --version -h -v completions d download u upload")" -- "$cur")'
99+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--version" "-h" "-v" "completions" "d" "download" "u" "upload")" -- "$cur")'
101100
echo $' ;;'
102101
echo $''
103102
echo $' esac'

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,37 @@ send_completions() {
77
echo $'# Modifying it manually is not recommended'
88
echo $''
99
echo $'_cli_completions_filter() {'
10-
echo $' local words="$1"'
10+
echo $' local words=("$@")'
1111
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1212
echo $' local result=()'
13+
echo $' local want_options=0'
1314
echo $''
1415
echo $' # words the user already typed (excluding the command itself)'
1516
echo $' local used=()'
1617
echo $' if ((COMP_CWORD > 1)); then'
1718
echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
1819
echo $' fi'
1920
echo $''
20-
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
21-
echo $' # Completing an option: offer everything (including options)'
22-
echo $' echo "$words"'
23-
echo $''
24-
echo $' else'
25-
echo $' # Completing a non-option: offer only non-options,'
26-
echo $' # and don\'t re-offer ones already used earlier in the line.'
27-
echo $' for word in $words; do'
21+
echo $' # Completing an option: offer everything.'
22+
echo $' # Completing a non-option: drop options and already-used words.'
23+
echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
24+
echo $' for word in "${words[@]}"; do'
25+
echo $' if ((!want_options)); then'
2826
echo $' [[ "${word:0:1}" == "-" ]] && continue'
2927
echo $''
30-
echo $' local seen=0'
3128
echo $' for u in "${used[@]}"; do'
3229
echo $' if [[ "$u" == "$word" ]]; then'
33-
echo $' seen=1'
34-
echo $' break'
30+
echo $' continue 2'
3531
echo $' fi'
3632
echo $' done'
37-
echo $' ((!seen)) && result+=("$word")'
38-
echo $' done'
33+
echo $' fi'
3934
echo $''
40-
echo $' echo "${result[*]}"'
41-
echo $' fi'
35+
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
36+
echo $' printf -v word \'%q\' "$word"'
37+
echo $' result+=("$word")'
38+
echo $' done'
39+
echo $''
40+
echo $' echo "${result[*]}"'
4241
echo $'}'
4342
echo $''
4443
echo $'_cli_completions() {'
@@ -53,23 +52,23 @@ send_completions() {
5352
echo $''
5453
echo $' case "$compline" in'
5554
echo $' \'download\'*)'
56-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force --help -f -h")" -- "$cur")'
55+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force" "--help" "-f" "-h")" -- "$cur")'
5756
echo $' ;;'
5857
echo $''
5958
echo $' \'upload\'*)'
60-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --password --user -h -p -u")" -- "$cur")'
59+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u")" -- "$cur")'
6160
echo $' ;;'
6261
echo $''
6362
echo $' \'d\'*)'
64-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force --help -f -h")" -- "$cur")'
63+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force" "--help" "-f" "-h")" -- "$cur")'
6564
echo $' ;;'
6665
echo $''
6766
echo $' \'u\'*)'
68-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --password --user -h -p -u")" -- "$cur")'
67+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u")" -- "$cur")'
6968
echo $' ;;'
7069
echo $''
7170
echo $' *)'
72-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --version -h -v d download u upload")" -- "$cur")'
71+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--version" "-h" "-v" "d" "download" "u" "upload")" -- "$cur")'
7372
echo $' ;;'
7473
echo $''
7574
echo $' esac'

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@
55
# Modifying it manually is not recommended
66

77
_cli_completions_filter() {
8-
local words="$1"
8+
local words=("$@")
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
11+
local want_options=0
1112

1213
# words the user already typed (excluding the command itself)
1314
local used=()
1415
if ((COMP_CWORD > 1)); then
1516
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
1617
fi
1718

18-
if [[ "${cur:0:1}" == "-" ]]; then
19-
# Completing an option: offer everything (including options)
20-
echo "$words"
21-
22-
else
23-
# Completing a non-option: offer only non-options,
24-
# and don't re-offer ones already used earlier in the line.
25-
for word in $words; do
19+
# Completing an option: offer everything.
20+
# Completing a non-option: drop options and already-used words.
21+
[[ "${cur:0:1}" == "-" ]] && want_options=1
22+
for word in "${words[@]}"; do
23+
if ((!want_options)); then
2624
[[ "${word:0:1}" == "-" ]] && continue
2725

28-
local seen=0
2926
for u in "${used[@]}"; do
3027
if [[ "$u" == "$word" ]]; then
31-
seen=1
32-
break
28+
continue 2
3329
fi
3430
done
35-
((!seen)) && result+=("$word")
36-
done
31+
fi
3732

38-
echo "${result[*]}"
39-
fi
33+
# compgen -W expects shell-escaped words in one space-delimited string.
34+
printf -v word '%q' "$word"
35+
result+=("$word")
36+
done
37+
38+
echo "${result[*]}"
4039
}
4140

4241
_cli_completions() {
@@ -51,23 +50,23 @@ _cli_completions() {
5150

5251
case "$compline" in
5352
'download'*)
54-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force --help -f -h")" -- "$cur")
53+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force" "--help" "-f" "-h")" -- "$cur")
5554
;;
5655

5756
'upload'*)
58-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --password --user -h -p -u")" -- "$cur")
57+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u")" -- "$cur")
5958
;;
6059

6160
'd'*)
62-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force --help -f -h")" -- "$cur")
61+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--force" "--help" "-f" "-h")" -- "$cur")
6362
;;
6463

6564
'u'*)
66-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --password --user -h -p -u")" -- "$cur")
65+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--password" "--user" "-h" "-p" "-u")" -- "$cur")
6766
;;
6867

6968
*)
70-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help --version -h -v d download u upload")" -- "$cur")
69+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_cli_completions_filter "--help" "--version" "-h" "-v" "d" "download" "u" "upload")" -- "$cur")
7170
;;
7271

7372
esac

spec/approvals/completions/function

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@ custom_name() {
66
echo $'# Modifying it manually is not recommended'
77
echo $''
88
echo $'_get_completions_filter() {'
9-
echo $' local words="$1"'
9+
echo $' local words=("$@")'
1010
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1111
echo $' local result=()'
12+
echo $' local want_options=0'
1213
echo $''
1314
echo $' # words the user already typed (excluding the command itself)'
1415
echo $' local used=()'
1516
echo $' if ((COMP_CWORD > 1)); then'
1617
echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
1718
echo $' fi'
1819
echo $''
19-
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
20-
echo $' # Completing an option: offer everything (including options)'
21-
echo $' echo "$words"'
22-
echo $''
23-
echo $' else'
24-
echo $' # Completing a non-option: offer only non-options,'
25-
echo $' # and don\'t re-offer ones already used earlier in the line.'
26-
echo $' for word in $words; do'
20+
echo $' # Completing an option: offer everything.'
21+
echo $' # Completing a non-option: drop options and already-used words.'
22+
echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
23+
echo $' for word in "${words[@]}"; do'
24+
echo $' if ((!want_options)); then'
2725
echo $' [[ "${word:0:1}" == "-" ]] && continue'
2826
echo $''
29-
echo $' local seen=0'
3027
echo $' for u in "${used[@]}"; do'
3128
echo $' if [[ "$u" == "$word" ]]; then'
32-
echo $' seen=1'
33-
echo $' break'
29+
echo $' continue 2'
3430
echo $' fi'
3531
echo $' done'
36-
echo $' ((!seen)) && result+=("$word")'
37-
echo $' done'
32+
echo $' fi'
3833
echo $''
39-
echo $' echo "${result[*]}"'
40-
echo $' fi'
34+
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
35+
echo $' printf -v word \'%q\' "$word"'
36+
echo $' result+=("$word")'
37+
echo $' done'
38+
echo $''
39+
echo $' echo "${result[*]}"'
4140
echo $'}'
4241
echo $''
4342
echo $'_get_completions() {'
@@ -52,7 +51,7 @@ custom_name() {
5251
echo $''
5352
echo $' case "$compline" in'
5453
echo $' *)'
55-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_get_completions_filter "--force --help --verbose --version -h -v")" -- "$cur")'
54+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_get_completions_filter "--force" "--help" "--verbose" "--version" "-h" "-v")" -- "$cur")'
5655
echo $' ;;'
5756
echo $''
5857
echo $' esac'

0 commit comments

Comments
 (0)