Skip to content

Commit

Permalink
Bash completion for flags (#1504)
Browse files Browse the repository at this point in the history
Signed-off-by: Mabel Zhang <[email protected]>
Signed-off-by: Louise Poubel <[email protected]>

Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
mabelzhang and chapulina authored Jun 15, 2022
1 parent 900583a commit dc5f117
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
76 changes: 76 additions & 0 deletions src/cmd/gazebo.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions src/cmd/model.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -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
}
66 changes: 66 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 All @@ -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)
Expand Down Expand Up @@ -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<std::string> flags((std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());

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<std::string> flags((std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());

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;
}
}

0 comments on commit dc5f117

Please sign in to comment.