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 #312

Merged
merged 8 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ endif()
#--------------------------------------
# Find ignition-tools
ign_find_package(ignition-tools QUIET)
if (ignition-tools_FOUND)
set (HAVE_IGN_TOOLS TRUE)
set (IGN_TOOLS_VER 1)
endif()

#--------------------------------------
# Find SQLite3
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ foreach(test ${test_list})
# multi-configuration generators, like Visual Studio.
target_compile_definitions(${test} PRIVATE
"DETAIL_IGN_TRANSPORT_TEST_DIR=\"$<TARGET_FILE_DIR:${test}>\""
"IGN_TEST_LIBRARY_PATH=\"$<TARGET_FILE_DIR:${PROJECT_LIBRARY_TARGET_NAME}>\"")

"IGN_TEST_LIBRARY_PATH=\"$<TARGET_FILE_DIR:${PROJECT_LIBRARY_TARGET_NAME}>\""
"IGN_TRANSPORT_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\"")
endforeach()

if(MSVC)
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)


mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
#===============================================================================
# Bash completion

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

# bash tab-completion

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

function _gz_service
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
# NOTE: In Fortress+, remove --force-version and --versions. Add --help-all.
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
# Update ../ign_TEST.cc accordingly.
COMPREPLY=($(compgen -W "
-h --help
-v --version
-s --service
--reqtype
--reptype
--timeout
-l --list
-i --info
-r --req
--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
}

function _gz_topic
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
# NOTE: In Fortress+, remove --force-version and --versions. Add
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
# `-v --version` and --json-output. Update ../ign_TEST.cc accordingly.
COMPREPLY=($(compgen -W "
-h --help
-v --version
-t --topic
-m --msgtype
-d --duration
-n --num
-l --list
-i --info
-e --echo
-p --pub
--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
}
89 changes: 89 additions & 0 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

#include <fstream>
#include <iostream>
#include <string>
#include <ignition/msgs.hh>
Expand Down Expand Up @@ -443,6 +444,94 @@ TEST(ignTest, TopicEchoNum)
testing::waitAndCleanupFork(pi);
}

//////////////////////////////////////////////////
/// \brief Check 'ign service --help' message and bash completion script for
/// consistent flags
TEST(ignTest, ServiceHelpVsCompletionFlags)
{
// Flags in help message
std::string output = custom_exec_str("ign service --help" + g_ignVersion);
EXPECT_NE(std::string::npos, output.find("--help")) << output;
EXPECT_NE(std::string::npos, output.find("--version")) << output;
EXPECT_NE(std::string::npos, output.find("--service")) << output;
EXPECT_NE(std::string::npos, output.find("--reqtype")) << output;
EXPECT_NE(std::string::npos, output.find("--reptype")) << output;
EXPECT_NE(std::string::npos, output.find("--timeout")) << output;
EXPECT_NE(std::string::npos, output.find("--list")) << output;
EXPECT_NE(std::string::npos, output.find("--info")) << output;
EXPECT_NE(std::string::npos, output.find("--req")) << output;
// NOTE: In Fortress+, remove --force-version and --versions. Add --help-all.
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
// Update cmd/transport.bash_completion.sh accordingly.
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(IGN_TRANSPORT_SOURCE_DIR) +
"/src/cmd/transport.bash_completion.sh");
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
std::string script((std::istreambuf_iterator<char>(scriptFile)),
std::istreambuf_iterator<char>());

EXPECT_NE(std::string::npos, script.find("--help")) << script;
EXPECT_NE(std::string::npos, script.find("--version")) << script;
EXPECT_NE(std::string::npos, script.find("--service")) << script;
EXPECT_NE(std::string::npos, script.find("--reqtype")) << script;
EXPECT_NE(std::string::npos, script.find("--reptype")) << script;
EXPECT_NE(std::string::npos, script.find("--timeout")) << script;
EXPECT_NE(std::string::npos, script.find("--list")) << script;
EXPECT_NE(std::string::npos, script.find("--info")) << script;
EXPECT_NE(std::string::npos, script.find("--req")) << script;
// NOTE: In Fortress+, remove --force-version and --versions. Add --help-all.
// Update cmd/transport.bash_completion.sh accordingly.
EXPECT_NE(std::string::npos, script.find("--force-version")) << script;
EXPECT_NE(std::string::npos, script.find("--versions")) << script;
}

//////////////////////////////////////////////////
/// \brief Check 'ign topic --help' message and bash completion script for
/// consistent flags
TEST(ignTest, TopicHelpVsCompletionFlags)
{
// Flags in help message
std::string output = custom_exec_str("ign topic --help" + g_ignVersion);
EXPECT_NE(std::string::npos, output.find("--help")) << output;
EXPECT_NE(std::string::npos, output.find("--version")) << output;
EXPECT_NE(std::string::npos, output.find("--topic")) << output;
EXPECT_NE(std::string::npos, output.find("--msgtype")) << output;
EXPECT_NE(std::string::npos, output.find("--duration")) << output;
EXPECT_NE(std::string::npos, output.find("--num")) << output;
EXPECT_NE(std::string::npos, output.find("--list")) << output;
EXPECT_NE(std::string::npos, output.find("--info")) << output;
EXPECT_NE(std::string::npos, output.find("--echo")) << output;
EXPECT_NE(std::string::npos, output.find("--pub")) << output;
// NOTE: In Fortress+, remove --force-version and --versions.
// Add --version and --json-output.
// Update cmd/transport.bash_completion.sh accordingly.
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(IGN_TRANSPORT_SOURCE_DIR) +
"/src/cmd/transport.bash_completion.sh");
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
std::string script((std::istreambuf_iterator<char>(scriptFile)),
std::istreambuf_iterator<char>());

EXPECT_NE(std::string::npos, script.find("--help")) << script;
EXPECT_NE(std::string::npos, script.find("--version")) << script;
EXPECT_NE(std::string::npos, script.find("--topic")) << script;
EXPECT_NE(std::string::npos, script.find("--msgtype")) << script;
EXPECT_NE(std::string::npos, script.find("--duration")) << script;
EXPECT_NE(std::string::npos, script.find("--num")) << script;
EXPECT_NE(std::string::npos, script.find("--list")) << script;
EXPECT_NE(std::string::npos, script.find("--info")) << script;
EXPECT_NE(std::string::npos, script.find("--echo")) << script;
EXPECT_NE(std::string::npos, script.find("--pub")) << script;
// NOTE: In Fortress+, remove --force-version and --versions.
// Add --version and --json-output.
// Update cmd/transport.bash_completion.sh accordingly.
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