Bug 14727: koha-indexer tab completion in bash
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 25 Aug 2015 15:17:17 +0000 (12:17 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 13 Oct 2015 14:37:55 +0000 (11:37 -0300)
This patch introduces tab-completion in bash to the koha-indexer
command. It completes using valid option switches and instance names.
It stops completing then -h or --help are issued, and once a command
switch is chosen (either --start, --stop or --restart) it goes ahead
with valid instances completion.

To test:
- Have a packages install (kohadevbox works)
- Run:
  $ . debian/koha-common.bash-completion
- Type
  koha-indexer <tab>
- Play with all the options
=> SUCCESS: They work as they should.
- Sign off :-D

Note: it fixes a small glitch on the plack completion.

Signed-off-by: Jesse Weaver <pianohacker@gmail.com>
Verified working, including filtering out already used options and
instances.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
debian/koha-common.bash-completion

index d65aba2..637a56b 100644 (file)
@@ -217,9 +217,9 @@ _koha-plack()
                  --help) COMPREPLY=() ; return 0 ;; # no more completions
                      -h) COMPREPLY=() ; return 0 ;; # no more completions
                 --quiet) # filter the other quiet switches and go on
-                    substract="$substract -e '--quiet' -e '-q'"; ;;
+                    substract="$substract -e -q"; ;;
                 -q)      # filter the other quiet switches and go on
-                    substract="$substract -e '--quiet' -e '-q'"; ;;
+                    substract="$substract -e --quiet"; ;;
             esac
             substract="$substract -e ${COMP_WORDS[i]}"
         fi
@@ -235,6 +235,42 @@ _koha-plack()
 }
 complete -F _koha-plack koha-plack
 
+_koha-indexer()
+{
+    local cur opts substract
+
+    COMPREPLY=()
+    _get_comp_words_by_ref cur
+    opts="--start --stop --restart --quiet -q --help -h"
+
+    # Build a list of the already used option switches
+    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
+        if [[ ${COMP_WORDS[i]} == -* ]]; then
+            case ${COMP_WORDS[i]} in
+                --start) _koha_list_cmd ; return 0 ;;
+                 --stop) _koha_list_cmd ; return 0 ;;
+              --restart) _koha_list_cmd ; return 0 ;;
+                 --help) COMPREPLY=() ; return 0 ;; # no more completions
+                     -h) COMPREPLY=() ; return 0 ;; # no more completions
+                --quiet) # filter the other quiet switches and go on
+                    substract="$substract -e -q"; ;;
+                -q)      # filter the other quiet switches and go on
+                    substract="$substract -e --quiet"; ;;
+            esac
+            substract="$substract -e ${COMP_WORDS[i]}"
+        fi
+    done
+
+    if [[ "$substract" != "" ]]; then
+        opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
+    fi
+
+    COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
+
+    return 0
+}
+complete -F _koha-indexer koha-indexer
+
 # Local variables:
 # mode: shell-script
 # sh-basic-offset: 4