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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash completion for flags #254

Merged
merged 6 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR})
#--------------------------------------
# Find if ign command is available
find_program(HAVE_IGN_TOOLS ign)
if (HAVE_IGN_TOOLS)
set(IGN_TOOLS_VER 1)
endif()

#--------------------------------------
# Find Tinyxml2
Expand Down
16 changes: 16 additions & 0 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ file(GENERATE

# Install the ruby command line library in an unversioned location.
install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition)


#===============================================================================
# Bash completion

# Tack version onto and install the bash completion script
configure_file(
"msgs.bash_completion.sh"
"${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY)
if (HAVE_IGN_TOOLS)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d)
endif()
27 changes: 27 additions & 0 deletions src/cmd/msgs.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# bash tab-completion

# This is a per-library function definition, used in conjunction with the
# top-level entry point in ign-tools.

function _gz_msg
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
COMPREPLY=($(compgen -W "
-i --info
-l --list
-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
}
25 changes: 25 additions & 0 deletions tools/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

#include <fstream>
#include <string>
#include <gtest/gtest.h>
#include <ignition/msgs/config.hh>
Expand Down Expand Up @@ -89,6 +90,30 @@ TEST(CmdLine, MsgInfo)
<< output;
}

/////////////////////////////////////////////////
TEST(CmdLine, MsgHelpVsCompletionFlags)
{
// Flags in help message
auto output = custom_exec_str("ign msg --help --force-version " + g_version);
EXPECT_NE(std::string::npos, output.find("--info")) << output;
EXPECT_NE(std::string::npos, output.find("--list")) << 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::ifstream scriptFile(std::string(PROJECT_SOURCE_PATH) +
"/src/cmd/msgs.bash_completion.sh");
std::string script((std::istreambuf_iterator<char>(scriptFile)),
std::istreambuf_iterator<char>());

EXPECT_NE(std::string::npos, script.find("--info")) << script;
EXPECT_NE(std::string::npos, script.find("--list")) << 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;
}

/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
Expand Down