trivial: Allow mixing modifiers/parameters in bash-completion scripts

This commit is contained in:
Gaël PORTAY 2022-04-05 14:24:28 +02:00 committed by Gaël PORTAY
parent 5bdbf0dd0a
commit c8987ab100
3 changed files with 45 additions and 84 deletions

View File

@ -16,24 +16,25 @@ _show_modifiers()
_fwupdagent() _fwupdagent()
{ {
local cur prev command local cur prev command args
COMPREPLY=() COMPREPLY=()
cur=`_get_cword` cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
command=${COMP_WORDS[1]} command=${COMP_WORDS[1]}
_count_args
case $command in case $command in
*) *)
#find first command #find first command
if [[ ${COMP_CWORD} = 1 ]]; then if [[ "$args" = "1" ]]; then
COMPREPLY=( $(compgen -W '${_fwupdagent_cmd_list[@]}' -- "$cur") ) COMPREPLY=( $(compgen -W '${_fwupdagent_cmd_list[@]}' -- "$cur") )
#modifiers for all commands
else
_show_modifiers
fi fi
;; ;;
esac esac
#modifiers
_show_modifiers
return 0 return 0
} }

View File

@ -102,11 +102,12 @@ _show_remotes()
_fwupdmgr() _fwupdmgr()
{ {
local cur prev command local cur prev command args
COMPREPLY=() COMPREPLY=()
cur=`_get_cword` cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
command=${COMP_WORDS[1]} command=${COMP_WORDS[1]}
_count_args
case $prev in case $prev in
--filter) --filter)
@ -118,114 +119,87 @@ _fwupdmgr()
case $command in case $command in
activate|clear-results|downgrade|get-releases|get-results|unlock|verify|verify-update|get-updates|switch-branch|update|upgrade) activate|clear-results|downgrade|get-releases|get-results|unlock|verify|verify-update|get-updates|switch-branch|update|upgrade)
#device ID #device ID
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_show_device_ids _show_device_ids
#modifiers
else
_show_modifiers
fi fi
;; ;;
get-details) get-details)
#find files #find files
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#modifiers
else
_show_modifiers
fi fi
;; ;;
device-test) device-test)
#find files #find files
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#modifiers
else
_show_modifiers
fi fi
;; ;;
install) install)
#device ID #device ID
if [[ "$prev" = "${COMP_WORDS[2]}" ]]; then if [[ "$args" = "2" ]]; then
_show_device_ids _show_device_ids
#version #version
elif [[ "$prev" = "${COMP_WORDS[3]}" ]]; then elif [[ "$args" = "3" ]]; then
_show_release_versions "$prev" _show_release_versions "$prev"
#modifiers
else
_show_modifiers
fi fi
;; ;;
local-install) local-install)
#find files #find files
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#device ID or modifiers #device ID or modifiers
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_show_device_ids _show_device_ids
_show_modifiers _show_modifiers
#modifiers
else
_show_modifiers
fi fi
;; ;;
modify-remote) modify-remote)
#find remotes #find remotes
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_show_remotes _show_remotes
#add key #add key
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
local keys local keys
keys="$(command fwupdmgr get-remotes | command awk -v pattern="Remote ID:.*${prev}$" '$0~pattern{show=1; next}/Remote/{show=0}{gsub(/:.*/,"")}show')" keys="$(command fwupdmgr get-remotes | command awk -v pattern="Remote ID:.*${prev}$" '$0~pattern{show=1; next}/Remote/{show=0}{gsub(/:.*/,"")}show')"
COMPREPLY+=( $(compgen -W "${keys}" -- "$cur") ) COMPREPLY+=( $(compgen -W "${keys}" -- "$cur") )
#modifiers
else
_show_modifiers
fi fi
;; ;;
enable-remote) enable-remote)
#find remotes #find remotes
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_show_remotes _show_remotes
#modifiers
else
_show_modifiers
fi fi
;; ;;
disable-remote) disable-remote)
#find remotes #find remotes
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_show_remotes _show_remotes
#modifiers
else
_show_modifiers
fi fi
;; ;;
refresh) refresh)
#find first file #find first file
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#find second file #find second file
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_filedir _filedir
#find remote ID #find remote ID
elif [[ "$prev" = "${COMP_WORDS[3]}" ]]; then elif [[ "$args" = "4" ]]; then
_show_remotes _show_remotes
#modifiers
else
_show_modifiers
fi fi
;; ;;
*) *)
#find first command #find first command
if [[ ${COMP_CWORD} = 1 ]]; then if [[ "$args" = "1" ]]; then
COMPREPLY=( $(compgen -W '${_fwupdmgr_cmd_list[@]}' -- "$cur") ) COMPREPLY=( $(compgen -W '${_fwupdmgr_cmd_list[@]}' -- "$cur") )
#modifiers for all commands
else
_show_modifiers
fi fi
;; ;;
esac esac
#modifiers
_show_modifiers
return 0 return 0
} }

View File

@ -104,11 +104,12 @@ _show_modifiers()
_fwupdtool() _fwupdtool()
{ {
local cur prev command local cur prev command args
COMPREPLY=() COMPREPLY=()
cur=`_get_cword` cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
command=${COMP_WORDS[1]} command=${COMP_WORDS[1]}
_count_args
case $prev in case $prev in
--plugins) --plugins)
@ -124,84 +125,69 @@ _fwupdtool()
case $command in case $command in
get-details|install|install-blob|firmware-dump) get-details|install|install-blob|firmware-dump)
#find files #find files
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#device ID #device ID
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_show_device_ids _show_device_ids
#modifiers
else
_show_modifiers
fi fi
;; ;;
attach|detach|activate|verify-update|reinstall|get-updates) attach|detach|activate|verify-update|reinstall|get-updates)
#device ID #device ID
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_show_device_ids _show_device_ids
#modifiers
else
_show_modifiers
fi fi
;; ;;
build-firmware) build-firmware)
#file in #file in
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#file out #file out
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_filedir _filedir
#script #script
elif [[ "$prev" = "${COMP_WORDS[3]}" ]]; then elif [[ "$args" = "4" ]]; then
_filedir _filedir
#output #output
elif [[ "$prev" = "${COMP_WORDS[4]}" ]]; then elif [[ "$args" = "5" ]]; then
_filedir _filedir
#modifiers
else
_show_modifiers
fi fi
;; ;;
firmware-parse|firmware-patch) firmware-parse|firmware-patch)
#find files #find files
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#firmware_type #firmware_type
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_show_firmware_types _show_firmware_types
#modifiers
else
_show_modifiers
fi fi
;; ;;
firmware-convert) firmware-convert)
#file in #file in
if [[ "$prev" = "$command" ]]; then if [[ "$args" = "2" ]]; then
_filedir _filedir
#file out #file out
elif [[ "$prev" = "${COMP_WORDS[2]}" ]]; then elif [[ "$args" = "3" ]]; then
_filedir _filedir
#firmware_type in #firmware_type in
elif [[ "$prev" = "${COMP_WORDS[3]}" ]]; then elif [[ "$args" = "4" ]]; then
_show_firmware_types _show_firmware_types
#firmware_type out #firmware_type out
elif [[ "$prev" = "${COMP_WORDS[4]}" ]]; then elif [[ "$args" = "5" ]]; then
_show_firmware_types _show_firmware_types
#modifiers
else
_show_modifiers
fi fi
;; ;;
*) *)
#find first command #find first command
if [[ ${COMP_CWORD} = 1 ]]; then if [[ "$args" = "1" ]]; then
COMPREPLY=( $(compgen -W '${_fwupdtool_cmd_list[@]}' -- "$cur") ) COMPREPLY=( $(compgen -W '${_fwupdtool_cmd_list[@]}' -- "$cur") )
#modifiers for all commands
else
_show_modifiers
fi fi
;; ;;
esac esac
#modifiers
_show_modifiers
return 0 return 0
} }