Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash completion for flags #1504

Merged
merged 16 commits into from
Jun 15, 2022
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR})
ign_find_package(ignition-tools
REQUIRED
PKGCONFIG "ignition-tools")
set(IGN_TOOLS_VER 1)

#--------------------------------------
# Find protobuf
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ 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)


mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
#===============================================================================
# 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)
66 changes: 66 additions & 0 deletions src/cmd/gazebo.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.

# NOTE: 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
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
# NOTE: In Fortress+, add --headless-rendering.
# Update ../ign_TEST.cc accordingly.
COMPREPLY=($(compgen -W "
-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
" -- "${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
}
68 changes: 68 additions & 0 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <cstdio>
#include <cstdlib>

#include <fstream>
#include <string>
#include <ignition/common/Filesystem.hh>
#include <ignition/common/Util.hh>
#include <ignition/utilities/ExtraTestMacros.hh>

Expand Down Expand Up @@ -160,3 +162,69 @@ 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 output = customExecStr(kIgnCommand + " gazebo --help");
EXPECT_NE(std::string::npos, output.find("-g")) << output;
EXPECT_NE(std::string::npos, output.find("--iterations")) << output;
EXPECT_NE(std::string::npos, output.find("--levels")) << output;
EXPECT_NE(std::string::npos, output.find("--network-role")) << output;
EXPECT_NE(std::string::npos, output.find("--network-secondaries")) << output;
EXPECT_NE(std::string::npos, output.find("--record")) << output;
EXPECT_NE(std::string::npos, output.find("--record-path")) << output;
EXPECT_NE(std::string::npos, output.find("--record-resources")) << output;
EXPECT_NE(std::string::npos, output.find("--record-topic")) << output;
EXPECT_NE(std::string::npos, output.find("--log-overwrite")) << output;
EXPECT_NE(std::string::npos, output.find("--log-compress")) << output;
EXPECT_NE(std::string::npos, output.find("--playback")) << output;
EXPECT_NE(std::string::npos, output.find("-r")) << output;
EXPECT_NE(std::string::npos, output.find("-s")) << output;
EXPECT_NE(std::string::npos, output.find("--verbose")) << output;
EXPECT_NE(std::string::npos, output.find("--gui-config")) << output;
EXPECT_NE(std::string::npos, output.find("--physics-engine")) << output;
EXPECT_NE(std::string::npos, output.find("--render-engine")) << output;
EXPECT_NE(std::string::npos, output.find("--render-engine-gui")) << output;
EXPECT_NE(std::string::npos, output.find("--render-engine-server")) << output;
EXPECT_NE(std::string::npos, output.find("--version")) << output;
EXPECT_NE(std::string::npos, output.find("-z")) << output;
EXPECT_NE(std::string::npos, output.find("--help")) << output;
EXPECT_NE(std::string::npos, output.find("--force-version")) << output;
EXPECT_NE(std::string::npos, output.find("--versions")) << output;

// Flags in bash completion
std::string scriptPath = common::joinPaths(std::string(PROJECT_SOURCE_PATH),
"src", "cmd", "gazebo.bash_completion.sh");
std::ifstream scriptFile(scriptPath);
std::string script((std::istreambuf_iterator<char>(scriptFile)),
std::istreambuf_iterator<char>());

EXPECT_NE(std::string::npos, script.find("-g")) << script;
EXPECT_NE(std::string::npos, script.find("--iterations")) << script;
EXPECT_NE(std::string::npos, script.find("--levels")) << script;
EXPECT_NE(std::string::npos, script.find("--network-role")) << script;
EXPECT_NE(std::string::npos, script.find("--network-secondaries")) << script;
EXPECT_NE(std::string::npos, script.find("--record")) << script;
EXPECT_NE(std::string::npos, script.find("--record-path")) << script;
EXPECT_NE(std::string::npos, script.find("--record-resources")) << script;
EXPECT_NE(std::string::npos, script.find("--record-topic")) << script;
EXPECT_NE(std::string::npos, script.find("--log-overwrite")) << script;
EXPECT_NE(std::string::npos, script.find("--log-compress")) << script;
EXPECT_NE(std::string::npos, script.find("--playback")) << script;
EXPECT_NE(std::string::npos, script.find("-r")) << script;
EXPECT_NE(std::string::npos, script.find("-s")) << script;
EXPECT_NE(std::string::npos, script.find("--verbose")) << script;
EXPECT_NE(std::string::npos, script.find("--gui-config")) << script;
EXPECT_NE(std::string::npos, script.find("--physics-engine")) << script;
EXPECT_NE(std::string::npos, script.find("--render-engine")) << script;
EXPECT_NE(std::string::npos, script.find("--render-engine-gui")) << script;
EXPECT_NE(std::string::npos, script.find("--render-engine-server")) << script;
EXPECT_NE(std::string::npos, script.find("--version")) << script;
EXPECT_NE(std::string::npos, script.find("-z")) << script;
EXPECT_NE(std::string::npos, script.find("--help")) << script;
EXPECT_NE(std::string::npos, script.find("--force-version")) << script;
EXPECT_NE(std::string::npos, script.find("--versions")) << script;
}