bash-completion.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2023 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # Get the list of commands from the output of the chip-tool,
  18. # where each command is prefixed with the ' | * ' string.
  19. _chip_tool_get_commands() {
  20. "$@" --help 2>&1 | awk '/ [|] [*] /{ print $3 }'
  21. }
  22. # Get the list of options from the output of the chip-tool,
  23. # where each option starts with the '[--' string.
  24. _chip_tool_get_options() {
  25. "$@" --help 2>&1 | awk -F'[[]|[]]' '/^[[]--/{ print $2 }'
  26. }
  27. _chip_tool() {
  28. local cur prev words cword split
  29. _init_completion -s || return
  30. # Get command line arguments up to the cursor position
  31. local args=("${COMP_WORDS[@]:0:$cword+1}")
  32. local command=0
  33. case "$prev" in
  34. --commissioner-name)
  35. readarray -t COMPREPLY < <(compgen -W "alpha beta gamma 4 5 6 7 8 9" -- "$cur")
  36. ;;
  37. --paa-trust-store-path | --cd-trust-store-path)
  38. _filedir -d
  39. ;;
  40. --storage-directory)
  41. _filedir -d
  42. ;;
  43. *)
  44. command=1
  45. ;;
  46. esac
  47. if [ "$command" -eq 1 ]; then
  48. case "$cur" in
  49. -*)
  50. words=$(_chip_tool_get_options "${args[@]}")
  51. ;;
  52. *)
  53. words=$(_chip_tool_get_commands "${args[@]}")
  54. ;;
  55. esac
  56. readarray -t COMPREPLY < <(compgen -W "$words" -- "$cur")
  57. fi
  58. }
  59. complete -F _chip_tool chip-tool