]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kresctl: tab-completion: escape for compgen '--' arguments and minor adjustments
authorFrantisek Tobias <frantisek.tobias@nic.cz>
Tue, 15 Oct 2024 10:41:52 +0000 (12:41 +0200)
committerFrantisek Tobias <frantisek.tobias@nic.cz>
Tue, 15 Oct 2024 10:41:52 +0000 (12:41 +0200)
python/knot_resolver/client/command.py
python/knot_resolver/client/commands/completion.py
utils/shell-completion/client.bash

index 814c30d4ad19821a20266783453237d19fb3e6f0..6c1334b1ac6fb7cf2468d551624c82dbcedf159c 100644 (file)
@@ -30,9 +30,12 @@ def get_subparsers_words(subparser_actions: List[argparse.Action]) -> CompWords:
 
 
 def get_subparser_by_name(name: str, parser_actions: List[argparse.Action]) -> Optional[argparse.ArgumentParser]:
+    if name in ["-s", "--socket", "-c", "--config"]:
+        return None
+
     for action in parser_actions:
         if isinstance(action, argparse._SubParsersAction):  # pylint: disable=protected-access
-            if action.choices and name in action.choices:
+            if action.choices and name in action.choices.keys():
                 return action.choices[name]
     return None
 
index 6ac0ef91f8dec85d6dd9cfee6fd1e5ee1171ad90..3fca6314953877e84285236baf2f4a51d03b0820 100644 (file)
@@ -78,14 +78,14 @@ class CompletionCommand(Command):
                             words = cmd.completion(subparser, subparser_args)
                         else:
                             words = cmd.completion(subparser)
-                    break
-                elif uarg in ["-s", "--socket", "-c", "--config"]:
-                    # if arg is socket config, skip next arg
-                    next(uargs)
-                    continue
-                elif uarg in words:
-                    # uarg is valid (complete) arg, continue
-                    continue
+                    break
+                elif uarg in ["-s", "--socket", "-c", "--config"]:
+                    # if arg is socket config, skip next arg
+                    next(uargs)
+                    continue
+                elif uarg in words:
+                    # uarg is valid (complete) arg, continue
+                    continue
 
         # print completion words
         # based on required bash/fish shell format
index e99943aa5be9cc74e22ef676395330beb82bfc95..778630fb92b2852b3e6b26dfa07d25a02eb4826f 100644 (file)
@@ -1,36 +1,65 @@
 #/usr/bin/env bash
 
+_kresctl_skip_next=0
+
+_kresctl_filter_switches()
+{
+    # skip kresctl, it is not a valid argument
+    local words=("${COMP_WORDS[@]:1}")
+    local new_words=()
+    local count=0
+
+    for WORD in "${words[@]}"
+    do
+        if [[ $_kresctl_skip_next -eq 0 ]]
+        then
+            if [[ ! $WORD =~ ^-{1,2} ]]
+            then
+                new_words[count]="$WORD"
+                ((count++))
+            else
+                _kresctl_skip_next=1
+            fi
+        else
+            _kresctl_skip_next=0
+        fi
+
+    done
+
+    printf "%s\n" "${new_words[@]}"
+    return $count
+}
+
+
 _kresctl_completion()
 {
     COMPREPLY=()
-    local cur prev opts
+    local cur opts cmp_words
 
     cur="${COMP_WORDS[COMP_CWORD]}"
-    prev="${COMP_WORDS[COMP_CWORD-1]}"
 
-    # skip kresctl, it is not a valid argument
-    local cmd_words=("${COMP_WORDS[@]:1}")
+    cmp_words=($(_kresctl_filter_switches))
 
     # check if there is a word is empty
     # that means there is a space after last non-empty word
     if [[ -z "$cur" ]]
     then
         # no word to complete, return all posible options
-        opts=$(kresctl completion --bash --space "${cmd_words[@]}")
+        opts=$(kresctl completion --bash --space "${cmp_words[@]}")
     else
-        opts=$(kresctl completion --bash "${cmd_words[@]}")
+        opts=$(kresctl completion --bash "${cmp_words[@]}")
     fi
 
     # if there is no completion from kresctl
     # auto-complete just directories and files
     if [[ -z "$opts" ]]
     then
-        COMPREPLY=($(compgen -d -f "${cur}"))
+        COMPREPLY=($(compgen -d -f -- "${cur}"))
     else
-        COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
+        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
     fi
 
     return 0
 }
 
-complete -o filenames -o dirnames -F _kresctl_completion kresctl
+complete -o filenames -o dirnames -o nosort -F _kresctl_completion kresctl