From b5b80e5a6d85a53c89e8ef1ddb76ae985a1c7f65 Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Tue, 2 Jun 2015 23:59:01 +0600 Subject: bash-completion: handle comma-separated options This solution can become messy when you have too many options listed, because it repeats all of them. For example, after invoking completion with this input: $ partx --output END,SECTORS,SCHEME,START, You got these completions: END,SECTORS,SCHEME,START,FLAGS, END,SECTORS,SCHEME,START,NR, END,SECTORS,SCHEME,START,TYPE, END,SECTORS,SCHEME,START,NAME, END,SECTORS,SCHEME,START,SIZE, END,SECTORS,SCHEME,START,UUID, Nevertheless, it works even with numbers (listed options properly excluded from completion). Try to invoke completion after 'chcpu --disable ' or 'lsblk --exclude ' to see it in action. Few issues remained: * completion interrupts after encountering ':' in listed option, like in 'MAJ:MIN' in lsblk, losetup. * lscpu completion is broken: it inserts space after '--extended', but lscpu assumes there is no space after this option. It also doesn't complete '--parse' option. * some completion options are outdated (for example, lscpu MMHZ). We need to sync them with code. Fix for lscpu follows. Signed-off-by: Boris Egorov --- bash-completion/lslocks | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'bash-completion/lslocks') diff --git a/bash-completion/lslocks b/bash-completion/lslocks index c9cff2c6b..6ad6c057a 100644 --- a/bash-completion/lslocks +++ b/bash-completion/lslocks @@ -14,11 +14,17 @@ _lslocks_module() return 0 ;; '-o'|'--output') - # FIXME: how to append to a string with compgen? - local OUTPUT - OUTPUT="COMMAND PID TYPE SIZE MODE M START END PATH BLOCKER" + local prefix realcur OUTPUT_ALL OUTPUT + realcur="${cur##*,}" + prefix="${cur%$realcur}" + OUTPUT_ALL="COMMAND PID TYPE SIZE MODE M START END PATH BLOCKER" + for WORD in $OUTPUT_ALL; do + if ! [[ $prefix == *"$WORD"* ]]; then + OUTPUT="$WORD $OUTPUT" + fi + done compopt -o nospace - COMPREPLY=( $(compgen -W "$OUTPUT" -S ',' -- $cur) ) + COMPREPLY=( $(compgen -P "$prefix" -W "$OUTPUT" -S ',' -- $realcur) ) return 0 ;; '-h'|'--help'|'-V'|'--version') -- cgit v1.2.3-55-g7522