From 153693d284335977b3e567fee113bc5efe0f5b96 Mon Sep 17 00:00:00 2001
From: Michael Friedrich <michael.friedrich@netways.de>
Date: Wed, 15 Oct 2014 14:12:59 +0200
Subject: [PATCH] CLI: Add filter by name or type to 'object list'

fixes #7199
---
 lib/cli/objectlistcommand.cpp | 26 +++++++++++++++++++-------
 lib/cli/objectlistcommand.hpp |  2 +-
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib/cli/objectlistcommand.cpp b/lib/cli/objectlistcommand.cpp
index 0f83cf316..08d0d3f65 100644
--- a/lib/cli/objectlistcommand.cpp
+++ b/lib/cli/objectlistcommand.cpp
@@ -56,8 +56,9 @@ void ObjectListCommand::InitParameters(boost::program_options::options_descripti
     boost::program_options::options_description& hiddenDesc,
     ArgumentCompletionDescription& argCompletionDesc) const
 {
-	/* Command doesn't support any parameters. */
-	//TODO Add filter regex #7199
+	visibleDesc.add_options()
+		("name,n", po::value<std::string>(), "filter by name matches")
+		("type,t", po::value<std::string>(), "filter by type matches");
 }
 
 /**
@@ -87,8 +88,15 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
 	std::map<String, int> type_count;
 
 	String message;
+	String name_filter, type_filter = "";
+
+	if (vm.count("name"))
+		name_filter = vm["name"].as<std::string>();
+	if (vm.count("type"))
+		type_filter = vm["type"].as<std::string>();
+
 	while (NetString::ReadStringFromStream(sfp, &message)) {
-		ReadObject(message, type_count);
+		ReadObject(message, type_count, name_filter, type_filter);
 		objects_count++;
 	}
 
@@ -97,20 +105,23 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
 
 	std::cout << FormatTypeCounts(type_count) << std::endl;
 
-	Log(LogInformation, "cli", "Found " + Convert::ToString(objects_count) + " objects.");
+	Log(LogInformation, "cli", "Parsed " + Convert::ToString(objects_count) + " objects.");
 
 	return 0;
 }
 
-void ObjectListCommand::ReadObject(const String& message, std::map<String, int>& type_count)
+void ObjectListCommand::ReadObject(const String& message, std::map<String, int>& type_count, String name_filter, String type_filter)
 {
 	Dictionary::Ptr object = JsonDeserialize(message);
 
+	String name = object->Get("name");
 	String type = object->Get("type");
 
-	type_count[type]++;
+	if(!name_filter.IsEmpty() && !Utility::Match(name_filter, name))
+		return;
+	if(!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
+		return;
 
-	String name = object->Get("name");
 	bool abstract = object->Get("abstract");
 	Dictionary::Ptr properties = object->Get("properties");
 	Dictionary::Ptr debug_hints = object->Get("debug_hints");
@@ -129,6 +140,7 @@ void ObjectListCommand::ReadObject(const String& message, std::map<String, int>&
 
 	std::cout << msgbuf.str() << "\n";
 
+	type_count[type]++;
 }
 
 String ObjectListCommand::FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
diff --git a/lib/cli/objectlistcommand.hpp b/lib/cli/objectlistcommand.hpp
index 1de9ae304..2db02fa83 100644
--- a/lib/cli/objectlistcommand.hpp
+++ b/lib/cli/objectlistcommand.hpp
@@ -46,7 +46,7 @@ public:
         virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
 
 private:
-        static void ReadObject(const String& message, std::map<String, int>& type_count);
+        static void ReadObject(const String& message, std::map<String, int>& type_count, String name_filter, String type_filter);
         static String FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
         static String FormatHints(const Dictionary::Ptr& hints, int indent = 0);
         static String FormatHint(const Array::Ptr& msg, int indent = 0);