From 8e7fd1f40d1ece27a774bf20e0e0cab0ba75de91 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 17 Oct 2014 13:03:01 +0200 Subject: [PATCH] Implement auto-completion support for type fields fixes #7401 --- lib/base/clicommand.cpp | 21 +++++++++++++++++++++ lib/base/clicommand.hpp | 1 + lib/cli/CMakeLists.txt | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/base/clicommand.cpp b/lib/base/clicommand.cpp index 3ddbac6f0..9f70967ae 100644 --- a/lib/base/clicommand.cpp +++ b/lib/base/clicommand.cpp @@ -69,6 +69,27 @@ static std::vector BashArgumentCompletionHelper(const String& type, cons return result; } +void icinga::AddTypeFields(const String& type, boost::program_options::options_description& desc) +{ + const Type *ptype = Type::GetByName(type); + + if (!ptype) + return; + + for (int i = 0; i < ptype->GetFieldCount(); i++) { + Field field = ptype->GetFieldInfo(i); + + if (strcmp(field.Name, "__name") == 0) + continue; + + if (!(field.Attributes & FAConfig)) + continue; + + desc.add_options() + (field.Name, po::value(), field.Name); + } +} + ArgumentCompletionCallback icinga::BashArgumentCompletion(const String& type) { return boost::bind(BashArgumentCompletionHelper, type, _1); diff --git a/lib/base/clicommand.hpp b/lib/base/clicommand.hpp index 77af27105..a5f4153af 100644 --- a/lib/base/clicommand.hpp +++ b/lib/base/clicommand.hpp @@ -33,6 +33,7 @@ typedef boost::function (const String&)> ArgumentCompletionC typedef std::map ArgumentCompletionDescription; I2_BASE_API ArgumentCompletionCallback BashArgumentCompletion(const String& type); +I2_BASE_API void AddTypeFields(const String& type, boost::program_options::options_description& desc); /** * A CLI command. diff --git a/lib/cli/CMakeLists.txt b/lib/cli/CMakeLists.txt index 0347ab827..b97c10df3 100644 --- a/lib/cli/CMakeLists.txt +++ b/lib/cli/CMakeLists.txt @@ -28,7 +28,7 @@ endif() add_library(cli SHARED ${cli_SOURCES}) -target_link_libraries(cli ${Boost_LIBRARIES} base config remote) +target_link_libraries(cli ${Boost_LIBRARIES} base config remote icinga) set_target_properties ( cli PROPERTIES