configure_completion.bash 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. # tools/configure_completion.bash
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one or more
  5. # contributor license agreements. See the NOTICE file distributed with
  6. # this work for additional information regarding copyright ownership. The
  7. # ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance with the
  9. # License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. function _nuttx_configure_completion
  20. {
  21. local colon_idx=2
  22. local num_options=0
  23. # Skip ./tools/configure.sh script options.
  24. while [[ "${#COMP_WORDS[@]}" != "${colon_idx}" ]] && [[ "${COMP_WORDS[$((colon_idx - 1))]}" == '-'* ]]
  25. do
  26. # If "-a" is among the provided arguments, consider the apps directory.
  27. if [ "${COMP_WORDS[$((colon_idx - 1))]}" == "-a" ]; then
  28. colon_idx=$((colon_idx + 2))
  29. num_options=$((num_options + 2))
  30. else
  31. colon_idx=$((colon_idx + 1))
  32. num_options=$((num_options + 1))
  33. fi
  34. done
  35. # When the command line is complete, COMP_WORDS array will have 5 elements
  36. # plus the options.
  37. # The script may return when this state is reached.
  38. if [ "${#COMP_WORDS[@]}" == $((5 + num_options)) ]; then
  39. return
  40. fi
  41. # If the last provided argument is "-a", do not execute autocompletion
  42. # algorithm.
  43. if [ "${COMP_WORDS[-2]}" == "-a" ]; then
  44. return
  45. fi
  46. local search_dir=""
  47. local search_pattern=""
  48. local reply_suffix=""
  49. if [ "${COMP_WORDS[${colon_idx}]}" == ':' ]; then
  50. local board_name=${COMP_WORDS[$((colon_idx - 1))]}
  51. local config_name=${COMP_WORDS[$((colon_idx + 1))]}
  52. search_dir="boards/*/*/${board_name}/configs/"
  53. search_pattern="${config_name}"
  54. reply_suffix=" "
  55. else
  56. search_dir="boards/*/*/"
  57. search_pattern="${COMP_WORDS[$((1 + num_options))]}"
  58. reply_suffix=":"
  59. fi
  60. local wordlist
  61. local suggestions
  62. wordlist=$(find ${search_dir} -maxdepth 1 -mindepth 1 -type d -exec basename {} \;) 2>/dev/null
  63. suggestions=($(compgen -W "${wordlist}" -- "${search_pattern}"))
  64. if [ "${#suggestions[@]}" == "1" ]; then
  65. # If there's only one match, complete with the appropriate reply suffix.
  66. COMPREPLY=("${suggestions[0]}${reply_suffix}")
  67. else
  68. # More than one suggestion resolved, respond with the suggestions intact.
  69. COMPREPLY=("${suggestions[@]}")
  70. fi
  71. }
  72. complete -o bashdefault -o default -o nospace -F _nuttx_configure_completion ./tools/configure.sh