Skip to content

Commit 97ea6f5

Browse files
Bash completion for flags (#254)
Signed-off-by: Mabel Zhang <mabel@openrobotics.org> Signed-off-by: Louise Poubel <louise@openrobotics.org> Co-authored-by: Louise Poubel <louise@openrobotics.org>
1 parent 57458eb commit 97ea6f5

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ find_package(ignition-cmake2 2.8.0 REQUIRED)
1616
#============================================================================
1717
ign_configure_project()
1818

19+
if (UNIX AND NOT APPLE)
20+
set (EXTRA_TEST_LIB_DEPS stdc++fs)
21+
else()
22+
set (EXTRA_TEST_LIB_DEPS)
23+
endif()
24+
1925
#============================================================================
2026
# Set project-specific options
2127
#============================================================================
@@ -69,8 +75,11 @@ ign_find_package(ignition-math6 REQUIRED)
6975
set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR})
7076

7177
#--------------------------------------
72-
# Find if ign command is available
78+
# Find if command is available. This is used to enable tests.
79+
# Note that CLI files are installed regardless of whether the dependency is
80+
# available during build time
7381
find_program(HAVE_IGN_TOOLS ign)
82+
set(IGN_TOOLS_VER 1)
7483

7584
#--------------------------------------
7685
# Find Tinyxml2

src/cmd/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,17 @@ file(GENERATE
4242

4343
# Install the ruby command line library in an unversioned location.
4444
install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition)
45+
46+
47+
#===============================================================================
48+
# Bash completion
49+
50+
# Tack version onto and install the bash completion script
51+
configure_file(
52+
"msgs.bash_completion.sh"
53+
"${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY)
54+
install(
55+
FILES
56+
${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh
57+
DESTINATION
58+
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d)

src/cmd/msgs.bash_completion.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (C) 2022 Open Source Robotics Foundation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# bash tab-completion
19+
20+
# This is a per-library function definition, used in conjunction with the
21+
# top-level entry point in ign-tools.
22+
23+
GZ_MSGS_COMPLETION_LIST="
24+
-i --info
25+
-l --list
26+
-h --help
27+
--force-version
28+
--versions
29+
"
30+
31+
function _gz_msg
32+
{
33+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
34+
# Specify options (-*) word list for this subcommand
35+
COMPREPLY=($(compgen -W "$GZ_MSGS_COMPLETION_LIST" \
36+
-- "${COMP_WORDS[COMP_CWORD]}" ))
37+
return
38+
else
39+
# Just use bash default auto-complete, because we never have two
40+
# subcommands in the same line. If that is ever needed, change here to
41+
# detect subsequent subcommands
42+
COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}"))
43+
return
44+
fi
45+
}
46+
47+
function _gz_msgs_flags
48+
{
49+
for word in $GZ_MSGS_COMPLETION_LIST; do
50+
echo "$word"
51+
done
52+
}

tools/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ if (MSVC)
1212
list(REMOVE_ITEM test_sources ign_TEST.cc)
1313
endif()
1414

15-
1615
if (HAVE_IGN_TOOLS)
17-
ign_build_tests(TYPE UNIT SOURCES ${test_sources})
16+
ign_build_tests(
17+
TYPE UNIT
18+
SOURCES ${test_sources}
19+
LIB_DEPS
20+
${EXTRA_TEST_LIB_DEPS})
1821
endif ()

tools/ign_TEST.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*
1616
*/
1717

18+
#include <filesystem>
19+
#include <fstream>
1820
#include <string>
1921
#include <gtest/gtest.h>
2022
#include <ignition/msgs/config.hh>
@@ -89,6 +91,36 @@ TEST(CmdLine, MsgInfo)
8991
<< output;
9092
}
9193

94+
/////////////////////////////////////////////////
95+
TEST(CmdLine, MsgHelpVsCompletionFlags)
96+
{
97+
// Flags in help message
98+
auto helpOutput = custom_exec_str("ign msg --help --force-version "
99+
+ g_version);
100+
101+
// Call the output function in the bash completion script
102+
std::filesystem::path scriptPath = PROJECT_SOURCE_PATH;
103+
scriptPath = scriptPath / "src" / "cmd" / "msgs.bash_completion.sh";
104+
105+
// Equivalent to:
106+
// sh -c "bash -c \". /path/to/msgs.bash_completion.sh; _gz_msgs_flags\""
107+
std::string cmd = "bash -c \". " + scriptPath.string() + "; _gz_msgs_flags\"";
108+
std::string scriptOutput = custom_exec_str(cmd);
109+
110+
// Tokenize script output
111+
std::istringstream iss(scriptOutput);
112+
std::vector<std::string> flags((std::istream_iterator<std::string>(iss)),
113+
std::istream_iterator<std::string>());
114+
115+
EXPECT_GT(flags.size(), 0u);
116+
117+
// Match each flag in script output with help message
118+
for (std::string flag : flags)
119+
{
120+
EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput;
121+
}
122+
}
123+
92124
/////////////////////////////////////////////////
93125
/// Main
94126
int main(int argc, char **argv)

0 commit comments

Comments
 (0)