diff --git a/CMakeLists.txt b/CMakeLists.txt index 79e93df766..4e2f9e21cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,9 @@ set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR}) ign_find_package(ignition-tools REQUIRED PKGCONFIG "ignition-tools") +# Note that CLI files are installed regardless of whether the dependency is +# available during build time +set(IGN_TOOLS_VER 1) #-------------------------------------- # Find protobuf diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index a1ff8091c4..11e7ef7e00 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -117,3 +117,25 @@ set(ign_model_ruby_path "${cmd_model_script_generated_test}") configure_file( "model.yaml.in" "${CMAKE_BINARY_DIR}/test/conf/model${PROJECT_VERSION_MAJOR}.yaml" @ONLY) + +#=============================================================================== +# Bash completion + +# Tack version onto and install the bash completion script +configure_file( + "gazebo.bash_completion.sh" + "${CMAKE_CURRENT_BINARY_DIR}/gazebo${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/gazebo${PROJECT_VERSION_MAJOR}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) + +configure_file( + "model.bash_completion.sh" + "${CMAKE_CURRENT_BINARY_DIR}/model${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/model${PROJECT_VERSION_MAJOR}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) diff --git a/src/cmd/gazebo.bash_completion.sh b/src/cmd/gazebo.bash_completion.sh new file mode 100644 index 0000000000..7d6f6d3042 --- /dev/null +++ b/src/cmd/gazebo.bash_completion.sh @@ -0,0 +1,76 @@ +#!/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. +# + +# bash tab-completion + +# This is a per-library function definition, used in conjunction with the +# top-level entry point in ign-tools. + +GZ_SIM_COMPLETION_LIST=" + -g + --iterations + --levels + --network-role + --network-secondaries + --record + --record-path + --record-resources + --record-topic + --log-overwrite + --log-compress + --playback + -r + -s + -v --verbose + --gui-config + --physics-engine + --render-engine + --render-engine-gui + --render-engine-server + --version + -z + -h --help + --force-version + --versions +" + +# TODO(anyone): In Garden+, replace `gazebo` in function name to align with +# subcommand, for ign-tools/etc/ign.bash_completion.sh to find this function. +function _gz_gazebo +{ + if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then + # Specify options (-*) word list for this subcommand + # TODO(anyone): In Fortress+, add --headless-rendering. + # Update ../ign_TEST.cc accordingly. + COMPREPLY=($(compgen -W "$GZ_SIM_COMPLETION_LIST" \ + -- "${COMP_WORDS[COMP_CWORD]}" )) + return + else + # Just use bash default auto-complete, because we never have two + # subcommands in the same line. If that is ever needed, change here to + # detect subsequent subcommands + COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}")) + return + fi +} + +function _gz_sim_flags +{ + for word in $GZ_SIM_COMPLETION_LIST; do + echo "$word" + done +} diff --git a/src/cmd/model.bash_completion.sh b/src/cmd/model.bash_completion.sh new file mode 100644 index 0000000000..6ddc4f4128 --- /dev/null +++ b/src/cmd/model.bash_completion.sh @@ -0,0 +1,55 @@ +#!/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. +# + +# bash tab-completion + +# This is a per-library function definition, used in conjunction with the +# top-level entry point in ign-tools. + +GZ_MODEL_COMPLETION_LIST=" + --list + -m --model + -p --pose + -l --link + -j --joint + -h --help + --force-version + --versions +" + +function _gz_model +{ + if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then + # Specify options (-*) word list for this subcommand + COMPREPLY=($(compgen -W "$GZ_MODEL_COMPLETION_LIST" \ + -- "${COMP_WORDS[COMP_CWORD]}" )) + return + else + # Just use bash default auto-complete, because we never have two + # subcommands in the same line. If that is ever needed, change here to + # detect subsequent subcommands + COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}")) + return + fi +} + +function _gz_model_flags +{ + for word in $GZ_MODEL_COMPLETION_LIST; do + echo "$word" + done +} diff --git a/src/ign_TEST.cc b/src/ign_TEST.cc index a1d7e71871..a66def21b3 100644 --- a/src/ign_TEST.cc +++ b/src/ign_TEST.cc @@ -19,7 +19,9 @@ #include #include +#include #include +#include #include #include @@ -29,6 +31,8 @@ static const std::string kBinPath(PROJECT_BINARY_PATH); static const std::string kIgnCommand( std::string(BREW_RUBY) + std::string(IGN_PATH) + "/ign gazebo -s "); +static const std::string kIgnModelCommand( + std::string(BREW_RUBY) + std::string(IGN_PATH) + "/ign model "); ///////////////////////////////////////////////// std::string customExecStr(std::string _cmd) @@ -160,3 +164,65 @@ TEST(CmdLine, ResourcePath) EXPECT_EQ(output.find("Unable to find file plugins.sdf"), std::string::npos) << output; } + +////////////////////////////////////////////////// +/// \brief Check --help message and bash completion script for consistent flags +TEST(CmdLine, GazeboHelpVsCompletionFlags) +{ + // Flags in help message + std::string helpOutput = customExecStr(kIgnCommand + " gazebo --help"); + + // Call the output function in the bash completion script + std::string scriptPath = ignition::common::joinPaths( + std::string(PROJECT_SOURCE_PATH), + "src", "cmd", "gazebo.bash_completion.sh"); + + // Equivalent to: + // sh -c "bash -c \". /path/to/gazebo.bash_completion.sh; _gz_sim_flags\"" + std::string cmd = "bash -c \". " + scriptPath + "; _gz_sim_flags\""; + std::string scriptOutput = customExecStr(cmd); + + // Tokenize script output + std::istringstream iss(scriptOutput); + std::vector flags((std::istream_iterator(iss)), + std::istream_iterator()); + + EXPECT_GT(flags.size(), 0u); + + // Match each flag in script output with help message + for (const auto &flag : flags) + { + EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput; + } +} + +////////////////////////////////////////////////// +/// \brief Check --help message and bash completion script for consistent flags +TEST(CmdLine, ModelHelpVsCompletionFlags) +{ + // Flags in help message + std::string helpOutput = customExecStr(kIgnModelCommand + " --help"); + + // Call the output function in the bash completion script + std::string scriptPath = ignition::common::joinPaths( + std::string(PROJECT_SOURCE_PATH), + "src", "cmd", "model.bash_completion.sh"); + + // Equivalent to: + // sh -c "bash -c \". /path/to/model.bash_completion.sh; _gz_model_flags\"" + std::string cmd = "bash -c \". " + scriptPath + "; _gz_model_flags\""; + std::string scriptOutput = customExecStr(cmd); + + // Tokenize script output + std::istringstream iss(scriptOutput); + std::vector flags((std::istream_iterator(iss)), + std::istream_iterator()); + + EXPECT_GT(flags.size(), 0u); + + // Match each flag in script output with help message + for (const auto &flag : flags) + { + EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput; + } +}