Setting case insensitivity for bash completion on command

Is there a way to indicate that a particular command is case insensitive without enabling global case sensitivity (at least for this shell)?

In my particular case, I have a small application that gives me command line access to a database of email addresses, so I print:

db get email john smith 

and he comes back with John Smith's email address. Therefore, I managed to enable termination mainly inside the application: setup

 COMPREPLY=($(compgen -W "$(db --complete $COMP_CWORD "$COMP_WORDS[@]"}")" -- ${COMP_WORDS[COMP_CWORD]})) 

works to let me follow the get and email tab. However, if I then type j<tab> , it refuses because it is correctly header in the email database. I would like to get bash to finish this anyway. (If I use capital J , it works.)

Otherwise, I can change the --complete option if it responds by matching the input, I suppose, but ideally the command line will match the database, if at all possible.

Please note that I work inside the application when using readline, but only interacts with bash, which seems to be a problem.

+6
source share
1 answer

Indeed, there seems to be no compgen matching for a word list ( -W ). I see the following workarounds:

A simple solution: first transfer both the list of words and the input token to all lowercase. Note. This is only an option, if it is acceptable that all the improvements turn into all lowercase letters.

 complete_lower() { local token=${COMP_WORDS[$COMP_CWORD]} local words=$( db --complete $COMP_CWORD "${COMP_WORDS[@]}" ) # Translate both the word list and the token to all-lowercase. local wordsLower=$( printf %s "$words" | tr [:upper:] [:lower:] ) local tokenLower=$( printf %s "$token" | tr [:upper:] [:lower:] ) COMPREPLY=($(compgen -W "$wordsLower" -- "$tokenLower")) } 

Better but more complicated solution: collapse your own case-matching logic:

 complete_custommatch() { local token=${COMP_WORDS[$COMP_CWORD]} local words=$( db --complete $COMP_CWORD "${COMP_WORDS[@]}" ) # Turn case-insensitive matching temporarily on, if necessary. local nocasematchWasOff=0 shopt nocasematch >/dev/null || nocasematchWasOff=1 (( nocasematchWasOff )) && shopt -s nocasematch # Loop over words in list and search for case-insensitive prefix match. local w matches=() for w in $words; do if [[ "$w" == "$token"* ]]; then matches+=("$w"); fi done # Restore state of 'nocasematch' option, if necessary. (( nocasematchWasOff )) && shopt -u nocasematch COMPREPLY=("${matches[@]}") } 
+6
source

Source: https://habr.com/ru/post/915340/


All Articles