[ovs-dev] [RFC bashcomp] ovs-vsctl-bashcomp: Avoid setting the COMP_WORDBREAKS.

Alex Wang alexw at nicira.com
Fri Mar 13 23:57:52 UTC 2015


Modifying $COMP_WORDBREAKS in completion script is not the recommended
as it is a global variable and the modification could affect the behavior
of other completion scripts.  As a workaround, this commit uses the
_get_comp_words_by_ref which allows user to exclude characters out of
$COMP_WORDBREAKS and reassemble input command line.  However, as a side
effect, the bash completion module cannot handle ':' and '=' correctly
in the resulting completions.  Thusly, we need to remove those
completions.  This will reduce the functionality of some fancy completions.
For example, COLUMN?:KEY=VALUE will not complete on KEY and VALUE.

Signed-off-by: Alex Wang <alexw at nicira.com>
---
 utilities/ovs-vsctl-bashcomp.bash |   19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/utilities/ovs-vsctl-bashcomp.bash b/utilities/ovs-vsctl-bashcomp.bash
index e79c6a2..3db7165 100755
--- a/utilities/ovs-vsctl-bashcomp.bash
+++ b/utilities/ovs-vsctl-bashcomp.bash
@@ -36,7 +36,7 @@ _OVS_VSCTL_COMMANDS="$(_ovs_vsctl --commands)"
 
 # This doesn't complete on short arguments, so it filters them out.
 _OVS_VSCTL_OPTIONS="$(_ovs_vsctl --options | awk '/^--/ { print $0 }' \
-                      | sed -e 's/\(.*\)=ARG/\1=/')"
+                      | sed -e 's/\(.*\)=ARG/\1/')"
 IFS=$SAVE_IFS
 
 declare -A _OVS_VSCTL_PARSED_ARGS
@@ -358,7 +358,7 @@ _ovs_vsctl_complete_column_optkey_value () {
             $_OVSDB_SERVER_LOCATION Open_vSwitch $table)
 
         result=$(printf "%s\n" "${columns}" \
-                 | awk '/key.*value/ { print $1":"; next }
+                 | awk '/key.*value/ { print $1; next }
                                      { print $1; next }' \
                  | _ovs_vsctl_check_startswith_string "$1" | sort | uniq)
     fi
@@ -627,7 +627,7 @@ _ovs_vsctl_process_messages () {
 # status of the function _ovs_vsctl_complete_argument represents where
 # it has determined that the next argument will be.
 _ovs_vsctl_bashcomp () {
-    local cur valid_globals cmd_args raw_cmd cmd_pos valid_globals valid_opts
+    local words cword valid_globals cmd_args raw_cmd cmd_pos valid_globals valid_opts
     local test="false"
 
     # Does not support BASH_VERSION < 4.0
@@ -645,6 +645,12 @@ _ovs_vsctl_bashcomp () {
                   <<< "$tmp"
         export COMP_WORDS
         export COMP_CWORD="$((${#COMP_WORDS[@]}-1))"
+    else
+        # If not in test mode, reassembles the COMP_WORDS and COMP_CWORD
+        # using just space as word break.
+        _get_comp_words_by_ref -n "\"'><=;|&(:" -w words -i cword
+        COMP_WORDS=( "${words[@]}")
+        COMP_CWORD=${cword}
     fi
 
     # Extract the conf.db path.
@@ -661,13 +667,11 @@ _ovs_vsctl_bashcomp () {
     _OVS_VSCTL_PARSED_ARGS=()
     _OVS_VSCTL_NEW_RECORDS=()
     cmd_pos=-1
-    cur=${COMP_WORDS[COMP_CWORD]}
     valid_globals=true
     valid_opts=true
     valid_commands=true
     given_opts=""
     index=1
-    export COMP_WORDBREAKS=" "
     for word in "${COMP_WORDS[@]:1:${COMP_CWORD}} "; do
         _OVS_VSCTL_COMP_NOSPACE=false
         local completion
@@ -750,7 +754,10 @@ _ovs_vsctl_bashcomp () {
             valid_commands=true
             given_opts=""
         fi
-        completion="$(sort -u <<< "$(tr ' ' '\n' <<< ${completion})")"
+        # colon, equal sign will mess up the completion output, just deletes
+        # any completion with those signs.
+        completion="$(sort -u <<< "$(tr ' ' '\n' <<< ${completion})" \
+                      | sed -e '/:/d; /=/d')"
         if [ $index -eq $COMP_CWORD ]; then
             if [ "$test" = "true" ]; then
                 if [ "${_OVS_VSCTL_COMP_NOSPACE}" = "true" ]; then
-- 
1.7.9.5




More information about the dev mailing list