-
Notifications
You must be signed in to change notification settings - Fork 59
/
bash-completion
190 lines (178 loc) · 8.16 KB
/
bash-completion
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022 Kaspar Giger <[email protected]>
# returns the available elbe commands. If elbe is installed together with grep and
# sed the commands are extracted by asking the program. Otherwise a hardcoded (and
# potentially outdated!) list is returned.
_elbe_commands()
{
if [ -x "$(command -v ${COMP_WORDS[0]})" ] && [ -x "$(command -v grep)" ] && [ -x "$(command -v sed)" ]; then
# elbe tool is installed, get a fresh list of available commands by running the
# command and grepping the output
echo $(${COMP_WORDS[0]} | grep " \* " | sed 's/^\s*\*\s*//;')
else
# Fallback solution (elbe, grep, or sed not available): currently available
# list (as of Nov 2022)
echo 'add check_updates chg_archive control diff get_archive
init initvm parselicence pbuilder pin_versions pkgdiff preprocess
prjrepo remove_sign repodir setsel show sign validate'
fi
}
# returns the command line options for a given program, by running the program (including
# the provided command line arguments!) and parsing the output. E.g. typically this would
# be called like "foo --help".
_get_cmd_options()
{
echo $($1 2>&1 | grep -E '^[[:space:]]+\-' \
| sed 's/, --/\n--/;s/^\s*//;' \
| sed -E 's/^(\-+[^ =]*)(=?).*/\1\2/;')
}
# returns the list of subcommands for an elbe command together with its command line options.
# This is used e.g. for the command "control" which has some subcommands. Additionally there
# exist command line options for "control". Both are returned.
# In case elbe, grep, or sed is not installed then the input parameter is simply returned
# (fallback solution).
_elbe_subcmd_with_opt()
{
if [ -x "$(command -v ${COMP_WORDS[0]})" ] && [ -x "$(command -v grep)" ] && [ -x "$(command -v sed)" ]; then
# elbe tool is installed, get a fresh list of available commands by running the
# command and grepping the output
commands=$(${COMP_WORDS[0]} ${COMP_WORDS[1]} 2>&1 | grep -E '^[[:space:]]' | sed 's/^\s*//;')
options=$(_get_cmd_options "${COMP_WORDS[0]} ${COMP_WORDS[1]} --help")
echo "${commands} ${options}"
else
# Fallback solution (elbe, grep, or sed not available): list the defaults passed
# as function argument
echo $1
fi
}
# returns the command line options for a given elbe command. In case the list of commands cannot
# be obtained by running the elbe program, the input parameter is simply returned (fallback
# solution).
_elbe_cmd_with_options()
{
if [ -x "$(command -v ${COMP_WORDS[0]})" ] && [ -x "$(command -v grep)" ] && [ -x "$(command -v sed)" ]; then
# elbe tool is installed, get a fresh list of available commands by running the
# command and grepping the output
# explanation of the the grep and sed commands:
# - grep... extracts lines starting with spaces followed by a dash (-)
# - 1st sed... splits short and long options on separate lines (e.g. -h, --help)
# - 2nd sed... extracts the name of the option (incl. optional =)
echo $(_get_cmd_options "${COMP_WORDS[0]} ${COMP_WORDS[1]}")
else
# Fallback solution (elbe, grep, or sed not available): list the defaults passed
# as function argument
echo $1
fi
}
# bash tab completion for program "elbe"
_cmd_args_completion_elbe()
{
local cur
_get_comp_words_by_ref -n = cur
if [[ "${cur}" == *= ]]; then
# if the last word typed ends with "=" then it's possible that the user
# might want to add a path after the "=", therefore offer him some file
# options. However, the current solution is very basic but rather suboptimal
# as the file doesn't nicely complete. No idea how to fix this though.
# Nevertheless, it's better than not offering any option at all
COMPREPLY=($(compgen -A file))
return 0
fi
# below, the basic approach is to run the elbe program and obtain the list of
# command line options and commands by. In some setups this might not be possible.
# In this case a fallback solution is implemented by hard-coding the avilable
# options/commands as currently present (as of Dec 2022). In order to keep the
# fallback solution updated, keep an eye on updating the list every now and then.
local cmd_opt=""
local file_arg=false
local disable_space=false
if [ "${#COMP_WORDS[@]}" -eq 2 ]; then
cmd_opt=$(_elbe_commands)
elif [ "${#COMP_WORDS[@]}" -ge 3 ]; then
local subcommand=${COMP_WORDS[1]}
case ${subcommand} in
pkgdiff|remove_sign|setsel|sign|validate)
file_arg=true
cmd_opt=" "
;;
control)
file_arg=true
disable_space=true
cmd_opt=$(_elbe_subcmd_with_opt 'rm_log list_projects create_project reset_project \
del_project set_xml build build_sysroot build_sdk build_cdroms get_file \
build_chroot_tarball dump_file get_files wait_busy set_cdrom set_orig \
set_pdebuild build_pbuilder update_pbuilder')
;;
check_updates)
file_arg=true
disable_space=true
disable_space=true
cmd_opt=$(_elbe_cmd_with_options '-h --help -s --script= --skip-validation -c --changelog=')
;;
chg_archive)
file_arg=true
cmd_opt=$(_elbe_cmd_with_options '-h --help --keep-attributes')
;;
init)
file_arg=true
disable_space=true
cmd_opt=$(_elbe_cmd_with_options '-h --help --skip-validation --directory= --cdrom= --buildtype= \
--debug --skip-build-bin --skip-build-sources')
;;
initvm)
cmd_opt=$(_elbe_subcmd_with_opt 'attach create ensure start stop submit sync')
;;
parselicence)
file_arg=true
disable_space=true
cmd_opt=$(_elbe_cmd_with_options '-h --help -s --script= --skip-validation -c --changelog=')
;;
pbuilder)
file_arg=true
disable_space=true
cmd_opt=$(_elbe_subcmd_with_opt 'build create --cross --help --origfile= -p \
--project= --skip-download --variants= --xmlfile= --ccache-size= \
-h --no-ccache --output= --profile= --proxy= -v --writeproject=')
;;
pin_versions)
file_arg=true
cmd_opt=$(_elbe_cmd_with_options '-h --help --skip-validation')
;;
preprocess)
file_arg=true
cmd_opt=$(_elbe_cmd_with_options '-h --help -o --output= -v --variants= -p --proxy=')
;;
prjrepo)
disable_space=true
cmd_opt=$(_elbe_subcmd_with_opt 'list_packages download upload_pkg -h --help --host= \
--port= --pass= --user= --retries= --debug --ignore-version-diff')
;;
repodir)
file_arg=true
disable_space=true
cmd_opt=$(_elbe_cmd_with_options '-h --help -o --output=')
;;
show)
file_arg=true
cmd_opt=$(_elbe_cmd_with_options '-h --help --verbose --skip-validation')
;;
*)
;;
esac
fi
if [ "${disable_space}" = true ]; then
# we need the nospace option below to avoid the space after the "--abc=" options
compopt -o nospace
fi
local compgen_arg=""
if [ "${file_arg}" = true ]; then
compgen_arg="${compgen_arg} -A file"
fi
if [ "${cmd_opt}" ]; then
COMPREPLY=($(compgen ${compgen_arg} -W "${cmd_opt}" -- ${cur}))
else
compopt -o default
COMPREPLY=()
fi
}
complete -F _cmd_args_completion_elbe elbe