diff --git a/CMakeLists.txt b/CMakeLists.txt index 8822730..7fcecb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,3 +223,4 @@ endif(build_errors) include(CTest) add_subdirectory(src) +add_subdirectory(etc) diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt new file mode 100644 index 0000000..3a321e0 --- /dev/null +++ b/etc/CMakeLists.txt @@ -0,0 +1,18 @@ +# Tack version onto and install the bash completion scripts + +# Install script to source all bash completion scripts, of all library versions. +configure_file( + "gz.completion" + "${CMAKE_CURRENT_BINARY_DIR}/gz${PROJECT_MAJOR_VERSION}.completion" @ONLY) +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/gz${PROJECT_MAJOR_VERSION}.completion + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz) + +configure_file( + "ign.bash_completion.sh" + "${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh" @ONLY) +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${PROJECT_MAJOR_VERSION}.completion.d) diff --git a/etc/gz.completion b/etc/gz.completion new file mode 100644 index 0000000..8c8d716 --- /dev/null +++ b/etc/gz.completion @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# +# Copyright (C) 2022 Open Source Robotics Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Source scripts from different libraries for subcommand bash completions + +# Directory of current script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +# Source the bash completion script for each subcommand +# Files with greater version numbers will be sourced after files with lesser +# version numbers, which means functions from newer versions will overwrite +# those from older versions. +for f in $SCRIPT_DIR/gz*.completion.d/*.bash_completion.sh ; do + source $f +done diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 94c1872..0b70ee2 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -1,14 +1,36 @@ +#!/usr/bin/env bash +# +# Copyright (C) 2022 Open Source Robotics Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + # ign bash completion function _ign { local prev cur cmd opts local ign="$1" - COMPREPLY=() cur="$2" prev="$3" - # searching for the command + # Return value + COMPREPLY=() + + # Look for the first command that is not an option (-*) + # COMP_WORDS: array of words in current command line COMP_LINE + # COMP_CWORD: index of word containing current cursur location + # COMP_LINE: line entered so far for ((i=1; $i<=$COMP_CWORD; i++)); do if [[ ${COMP_WORDS[i]} != -* ]]; then cmd="${COMP_WORDS[i]}" @@ -16,16 +38,29 @@ function _ign fi done + # On a word after top-level command ign. It may be an option (-*) if [[ "$cur" == -* ]] || [[ "$prev" != "ign" ]]; then + # Subcommand is help if [[ "$cmd" == "help" ]]; then opts=$(ign --commands) + + # Subcommand is a library name or an option (-*) else - COMPREPLY=($(compgen -f -- "${COMP_WORDS[${COMP_CWORD}]}" )) - complete -o filenames -o nospace -F "_ign" "ign" + # Complete subcommands + # If subcommand completion function exists (defined by corresponding + # library), call it. + func="_gz_$cmd" + if [[ "$(type -t $func)" == 'function' ]]; then + $func + else + # Use bash default auto-complete + COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}")) + fi return fi + # on first word, top-level command (ign) else opts="$(ign --commands) help" fi