xrnserver-cli-autocomplete.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. _xrnserver-cli_autocomplete() {
  2. local cur prev opts
  3. COMPREPLY=()
  4. cur="${COMP_WORDS[COMP_CWORD]}"
  5. #prev="${COMP_WORDS[COMP_CWORD-1]}"
  6. command=$( echo "${COMP_WORDS[@]}" | tr -s ' ' '\n' | awk '/--/{print $0}' )
  7. opts_start_server="-input-tar -cache-dir -output-tar -input-log-json -output-log-json"
  8. # Convert space-separated strings to arrays
  9. read -r -a array0 <<< "$opts_start_server"
  10. read -r -a array1 <<< "${COMP_WORDS[@]}"
  11. # Initialize an empty array to store the result
  12. result=()
  13. # Loop over each element in the first array
  14. for item in "${array0[@]}"; do
  15. # Check if the element exists in the second array
  16. if [[ ! " ${array1[@]} " =~ " $item " ]]; then
  17. # Add the element to the result array
  18. result+=("$item")
  19. fi
  20. done
  21. opts_start_server_sub="${result[*]}"
  22. # Check if an option has already been used
  23. local used_opts=$(echo "${COMP_WORDS[@]}" | grep -oE -- '(\s|^)--[a-zA-Z0-9]+' | sort | uniq)
  24. case "${command}" in
  25. --start-server)
  26. COMPREPLY=( $(compgen -W "${opts_start_server_sub}" -- ${cur}) )
  27. return 0
  28. ;;
  29. *)
  30. COMPREPLY=( $(compgen -W "--start-server $(echo "${opts}" | grep -v -E "${used_opts}")" -- ${cur}) )
  31. return 0
  32. ;;
  33. esac
  34. }
  35. complete -F _xrnserver-cli_autocomplete xrnserver-cli