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

Merged
merged 18 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ set(IGN_MSGS_VER ${ignition-msgs5_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 QT
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ ign_build_tests(TYPE UNIT
LIB_DEPS
${IGNITION-MATH_LIBRARIES}
TINYXML2::TINYXML2
TEST_LIST
gtest_targets
)

foreach(test ${gtest_targets})
target_compile_definitions(${test} PRIVATE
"IGN_GUI_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\"")
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
endforeach()

add_subdirectory(cmd)
add_subdirectory(plugins)
14 changes: 13 additions & 1 deletion src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generate a the ruby script.
# Generate the ruby script.
# Note that the major version of the library is included in the name.
if (APPLE)
set(IGN_LIBRARY_NAME lib${PROJECT_NAME_LOWER}.dylib)
Expand All @@ -11,3 +11,15 @@ configure_file(

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

# Tack version onto and install the bash completion script
configure_file(
"gui.bash_completion.sh"
"${CMAKE_CURRENT_BINARY_DIR}/gui${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY)
if (HAVE_IGN_TOOLS)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/gui${PROJECT_VERSION_MAJOR}.bash_completion.sh
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d)
endif()
29 changes: 29 additions & 0 deletions src/cmd/gui.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/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_gui
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
COMPREPLY=($(compgen -W "
-l --list
-s --standalone
-c --config
-v --verbose
-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
}
31 changes: 31 additions & 0 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <string>

#include <ignition/common/Filesystem.hh>
Expand Down Expand Up @@ -96,3 +97,33 @@ TEST_F(CmdLine, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(list))
EXPECT_TRUE(common::exists(common::joinPaths(this->kFakeHome, ".ignition",
"gui")));
}

//////////////////////////////////////////////////
/// \brief Check --help message and bash completion script for consistent flags
TEST(ignTest, GuiHelpVsCompletionFlags)
chapulina marked this conversation as resolved.
Show resolved Hide resolved
chapulina marked this conversation as resolved.
Show resolved Hide resolved
{
// Flags in help message
std::string output = custom_exec_str("ign gui --help");
EXPECT_NE(std::string::npos, output.find("--list")) << output;
EXPECT_NE(std::string::npos, output.find("--standalone")) << output;
EXPECT_NE(std::string::npos, output.find("--config")) << output;
EXPECT_NE(std::string::npos, output.find("--verbose")) << 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(IGN_GUI_SOURCE_DIR),
"src", "cmd", "gui.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("--list")) << script;
EXPECT_NE(std::string::npos, script.find("--standalone")) << script;
EXPECT_NE(std::string::npos, script.find("--config")) << script;
EXPECT_NE(std::string::npos, script.find("--verbose")) << 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;
}