Improve name completion handling.

Use regex to handle short option `-n`, since short options can be
combined (e.g. `-nd`) as long as at max one requires an argument.

Also consider the case when the arg for the long option is not given
together with `--name=`.

Signed-off-by: Edênis Freindorfer Azevedo <edenisfa@gmail.com>
This commit is contained in:
Edênis Freindorfer Azevedo 2021-08-27 17:24:24 -03:00
parent db8b9df555
commit 8aa4490505
No known key found for this signature in database
GPG Key ID: F7F1907D677FB8C9

View File

@ -27,11 +27,12 @@ _lxc_names() {
_lxc_append_name() {
local vms=$(command lxc-ls)
local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$"
# If `--name` or `-n` are present, do not complete with container names.
for param in ${words[@]}; do
if [[ ${param} =~ ^--name=(.*)$ ]]; then
if [[ ${param} =~ ^--name(=(.*))?$ ]]; then
return
elif [[ ${param} =~ ^-n$ ]]; then
elif [[ ${param} =~ ${shortoptnamexp} ]]; then
return
fi
for vm in ${vms[@]}; do