-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalrus_completion.sh
More file actions
65 lines (55 loc) · 2.32 KB
/
walrus_completion.sh
File metadata and controls
65 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
################################################################################
# walrus_completion.sh #
# This script was coded by stakin.com. #
# If you encounter any bugs, please report them at: #
# https://github.com/StakinOfficial/walrus-completion/issues #
# #
# Licensed under the MIT License. #
# #
################################################################################
_generate_walrus_completions() {
local cur prev opts program
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
program="${COMP_WORDS[0]}" # Dynamic naming
# Identification
local command subcommand subsubcommand
for ((i = 1; i < COMP_CWORD; i++)); do
if [[ "${COMP_WORDS[i]}" != -* ]]; then
if [[ -z "$command" ]]; then
command="${COMP_WORDS[i]}"
elif [[ -z "$subcommand" ]]; then
subcommand="${COMP_WORDS[i]}"
else
subsubcommand="${COMP_WORDS[i]}"
break
fi
fi
done
# Parsing
local help_output
if [[ -n "$subsubcommand" ]]; then
help_output=$("${program}" "$command" "$subcommand" "$subsubcommand" --help 2>/dev/null)
elif [[ -n "$subcommand" ]]; then
help_output=$("${program}" "$command" "$subcommand" --help 2>/dev/null)
elif [[ -n "$command" ]]; then
help_output=$("${program}" "$command" --help 2>/dev/null)
else
help_output=$("${program}" --help 2>/dev/null)
fi
# Extraction
opts=$(echo "$help_output" | awk '/Commands:/,/^$/ {if (!/:/ && !/^$/ && $1) print $1}')
opts+=" $(echo "$help_output" | grep -oE '\-\-[a-zA-Z0-9\-]+' | sort -u)"
# Dups removal
opts=$(echo "$opts" | tr ' ' '\n' | awk '!seen[$0]++' | tr '\n' ' ')
# Finally complete based on the current word
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
}
# Whitelist commands
commands=(
walrus
)
for cmd in "${commands[@]}"; do
complete -F _generate_walrus_completions "$cmd"
done