From e21fbb42610d522f197a51101235a320ab19ffd3 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Mon, 25 Apr 2022 18:36:39 -0400 Subject: [PATCH 01/12] fix variable name Signed-off-by: Mabel Zhang --- etc/ign.bash_completion.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 12c4347..3d95e36 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -10,7 +10,7 @@ function _ign # searching for the command for ((i=1; $i<=$COMP_CWORD; i++)); do - if [[ ${COMPWORDS[i]} != -* ]]; then + if [[ ${COMP_WORDS[i]} != -* ]]; then cmd="${COMP_WORDS[i]}" break fi From ced56e6e04d508abfcf1c9977efd47a7e7708812 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Mon, 25 Apr 2022 18:38:13 -0400 Subject: [PATCH 02/12] add help as available subcommand Signed-off-by: Mabel Zhang --- etc/ign.bash_completion.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 3d95e36..9b969fb 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -30,6 +30,6 @@ function _ign opts=$(ign --commands) fi - COMPREPLY=($(compgen -W "${opts}" -- ${cur})) + COMPREPLY=($(compgen -W "${opts} help" -- ${cur})) } complete -F "_ign" "ign" From fb03962c28ea818c75ccc36da52527a2fc6d66d6 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Mon, 25 Apr 2022 18:59:55 -0400 Subject: [PATCH 03/12] show help completion only for top-level cmd Signed-off-by: Mabel Zhang --- etc/ign.bash_completion.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 9b969fb..94c1872 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -27,9 +27,9 @@ function _ign fi else - opts=$(ign --commands) + opts="$(ign --commands) help" fi - COMPREPLY=($(compgen -W "${opts} help" -- ${cur})) + COMPREPLY=($(compgen -W "${opts}" -- ${cur})) } complete -F "_ign" "ign" From b6a97e9361df6e20117101d582f6e70265d40d96 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Sat, 30 Apr 2022 01:53:06 -0400 Subject: [PATCH 04/12] subcommand bash completeion for gui and plugin Signed-off-by: Mabel Zhang --- etc/ign.bash_completion.sh | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 94c1872..6975520 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -1,14 +1,21 @@ +#!/usr/bin/env bash + # 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 +23,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" - return + # Complete subcommands + case "$cmd" in + gui) + _ign_gui + return + ;; + plugin) + _ign_plugin + return + ;; + esac fi + # on first word, top-level command (ign) else opts="$(ign --commands) help" fi From 0649de74fc9091d5a1cafa558dfedfb80c60d41c Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Sat, 30 Apr 2022 02:46:10 -0400 Subject: [PATCH 05/12] rename functions ign->gz Signed-off-by: Mabel Zhang --- etc/ign.bash_completion.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 6975520..ecf2289 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -35,11 +35,11 @@ function _ign # Complete subcommands case "$cmd" in gui) - _ign_gui + _gz_gui return ;; plugin) - _ign_plugin + _gz_plugin return ;; esac From 1efb619ed81c5a8a81a8fc4e62c2e04148892f8d Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Tue, 3 May 2022 04:17:12 -0400 Subject: [PATCH 06/12] add installation Signed-off-by: Mabel Zhang --- CMakeLists.txt | 1 + etc/CMakeLists.txt | 3 +++ etc/ignition.completion | 9 +++++++++ 3 files changed, 13 insertions(+) create mode 100644 etc/CMakeLists.txt create mode 100644 etc/ignition.completion 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..7454632 --- /dev/null +++ b/etc/CMakeLists.txt @@ -0,0 +1,3 @@ +# Install the bash completion script in an unversioned location. +install(FILES ign.bash_completion.sh DESTINATION share/ignition/ignition.completion.d) +install(FILES ignition.completion DESTINATION share/ignition) diff --git a/etc/ignition.completion b/etc/ignition.completion new file mode 100644 index 0000000..453423d --- /dev/null +++ b/etc/ignition.completion @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# Directory of current script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +# Source the bash completion script for each subcommand +for f in $SCRIPT_DIR/ignition.completion.d/*.bash_completion.sh ; do + source $f +done From 1fba3f16d5b71edb5b169b3d00412744a469a9b3 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Tue, 10 May 2022 23:38:55 -0400 Subject: [PATCH 07/12] ign->gz Signed-off-by: Mabel Zhang --- etc/CMakeLists.txt | 4 ++-- etc/{ignition.completion => gz.completion} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename etc/{ignition.completion => gz.completion} (74%) diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt index 7454632..1e41a2d 100644 --- a/etc/CMakeLists.txt +++ b/etc/CMakeLists.txt @@ -1,3 +1,3 @@ # Install the bash completion script in an unversioned location. -install(FILES ign.bash_completion.sh DESTINATION share/ignition/ignition.completion.d) -install(FILES ignition.completion DESTINATION share/ignition) +install(FILES ign.bash_completion.sh DESTINATION share/gz/gz.completion.d) +install(FILES gz.completion DESTINATION share/gz) diff --git a/etc/ignition.completion b/etc/gz.completion similarity index 74% rename from etc/ignition.completion rename to etc/gz.completion index 453423d..c7c0c09 100644 --- a/etc/ignition.completion +++ b/etc/gz.completion @@ -4,6 +4,6 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" # Source the bash completion script for each subcommand -for f in $SCRIPT_DIR/ignition.completion.d/*.bash_completion.sh ; do +for f in $SCRIPT_DIR/gz.completion.d/*.bash_completion.sh ; do source $f done From 847713decafd4a9edf3502a232058c58361fafb9 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Wed, 11 May 2022 00:05:20 -0400 Subject: [PATCH 08/12] construct subcommand function name automatically Signed-off-by: Mabel Zhang --- etc/gz.completion | 2 ++ etc/ign.bash_completion.sh | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/etc/gz.completion b/etc/gz.completion index c7c0c09..2ded925 100644 --- a/etc/gz.completion +++ b/etc/gz.completion @@ -1,5 +1,7 @@ #!/usr/bin/env bash +# Source scripts from different libraries for subcommand bash completions + # Directory of current script SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index ecf2289..396146b 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -33,16 +33,16 @@ function _ign # Subcommand is a library name or an option (-*) else # Complete subcommands - case "$cmd" in - gui) - _gz_gui - return - ;; - plugin) - _gz_plugin - return - ;; - esac + # 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) From 6b39016c311f03b8da051bf28c7a78f58ccb07e9 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Thu, 19 May 2022 22:09:16 -0400 Subject: [PATCH 09/12] install to versioned place Signed-off-by: Mabel Zhang --- etc/CMakeLists.txt | 18 +++++++++++++++--- etc/gz.completion | 5 ++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt index 1e41a2d..4564b0f 100644 --- a/etc/CMakeLists.txt +++ b/etc/CMakeLists.txt @@ -1,3 +1,15 @@ -# Install the bash completion script in an unversioned location. -install(FILES ign.bash_completion.sh DESTINATION share/gz/gz.completion.d) -install(FILES gz.completion DESTINATION share/gz) +# Tack version onto and install the bash completion scripts +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 share/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 index 2ded925..9bdac35 100644 --- a/etc/gz.completion +++ b/etc/gz.completion @@ -6,6 +6,9 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" # Source the bash completion script for each subcommand -for f in $SCRIPT_DIR/gz.completion.d/*.bash_completion.sh ; do +# 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@PROJECT_MAJOR_VERSION@.completion.d/*.bash_completion.sh ; do source $f done From 79478658a9abbf332687e65830a7e8d507ab6ee7 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Thu, 19 May 2022 23:04:11 -0400 Subject: [PATCH 10/12] unversion gz.completion per manual tests Signed-off-by: Mabel Zhang --- etc/CMakeLists.txt | 10 +++------- etc/gz.completion | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt index 4564b0f..3a59f70 100644 --- a/etc/CMakeLists.txt +++ b/etc/CMakeLists.txt @@ -1,11 +1,7 @@ -# Tack version onto and install the bash completion scripts -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 share/gz) +# Install script to source all bash completion scripts, of all library versions. +install(FILES gz.completion DESTINATION share/gz) +# Tack version onto and install the bash completion script configure_file( "ign.bash_completion.sh" "${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh" @ONLY) diff --git a/etc/gz.completion b/etc/gz.completion index 9bdac35..9585fb3 100644 --- a/etc/gz.completion +++ b/etc/gz.completion @@ -9,6 +9,6 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" # 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@PROJECT_MAJOR_VERSION@.completion.d/*.bash_completion.sh ; do +for f in $SCRIPT_DIR/gz*.completion.d/*.bash_completion.sh ; do source $f done From f453a433c456b86b3ba8e96b2ff85b3b856d1024 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Wed, 1 Jun 2022 03:59:44 -0400 Subject: [PATCH 11/12] license Signed-off-by: Mabel Zhang --- etc/gz.completion | 15 +++++++++++++++ etc/ign.bash_completion.sh | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/etc/gz.completion b/etc/gz.completion index 9585fb3..8c8d716 100644 --- a/etc/gz.completion +++ b/etc/gz.completion @@ -1,4 +1,19 @@ #!/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 diff --git a/etc/ign.bash_completion.sh b/etc/ign.bash_completion.sh index 396146b..0b70ee2 100644 --- a/etc/ign.bash_completion.sh +++ b/etc/ign.bash_completion.sh @@ -1,4 +1,19 @@ #!/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 From d879208dc29540c748e1807b014636edf63d66ba Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Wed, 1 Jun 2022 03:59:58 -0400 Subject: [PATCH 12/12] install gz.completion script to versioned name Signed-off-by: Mabel Zhang --- etc/CMakeLists.txt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt index 3a59f70..3a321e0 100644 --- a/etc/CMakeLists.txt +++ b/etc/CMakeLists.txt @@ -1,7 +1,14 @@ +# Tack version onto and install the bash completion scripts + # Install script to source all bash completion scripts, of all library versions. -install(FILES gz.completion DESTINATION share/gz) +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) -# Tack version onto and install the bash completion script configure_file( "ign.bash_completion.sh" "${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh" @ONLY)