tools: nfdc strategy list/show commands

refs #3865

Change-Id: I6cc115b3c3f3d0840814a329c68356bbe6ad2e3f
diff --git a/tools/nfdc/strategy-choice-module.cpp b/tools/nfdc/strategy-choice-module.cpp
index 4a6b36f..e5fe584 100644
--- a/tools/nfdc/strategy-choice-module.cpp
+++ b/tools/nfdc/strategy-choice-module.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -31,6 +31,59 @@
 namespace nfdc {
 
 void
+StrategyChoiceModule::registerCommands(CommandParser& parser)
+{
+  CommandDefinition defStrategyList("strategy", "list");
+  defStrategyList
+    .setTitle("print strategy choices");
+  parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
+
+  CommandDefinition defStrategyShow("strategy", "show");
+  defStrategyShow
+    .setTitle("show strategy choice of an entry")
+    .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
+  parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
+}
+
+void
+StrategyChoiceModule::list(ExecuteContext& ctx)
+{
+  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
+    [&] (const std::vector<StrategyChoice>& dataset) {
+      for (const StrategyChoice& entry : dataset) {
+        formatItemText(ctx.out, entry);
+        ctx.out << '\n';
+      }
+    },
+    ctx.makeDatasetFailureHandler("strategy choice dataset"),
+    ctx.makeCommandOptions());
+
+  ctx.face.processEvents();
+}
+
+void
+StrategyChoiceModule::show(ExecuteContext& ctx)
+{
+  auto prefix = ctx.args.get<Name>("prefix");
+
+  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
+    [&] (const std::vector<StrategyChoice>& dataset) {
+      StrategyChoice match; // longest prefix match
+      for (const StrategyChoice& entry : dataset) {
+        if (entry.getName().isPrefixOf(prefix) &&
+            entry.getName().size() >= match.getName().size()) {
+          match = entry;
+        }
+      }
+      formatItemText(ctx.out, match, true);
+    },
+    ctx.makeDatasetFailureHandler("strategy choice dataset"),
+    ctx.makeCommandOptions());
+
+  ctx.face.processEvents();
+}
+
+void
 StrategyChoiceModule::fetchStatus(Controller& controller,
                                   const function<void()>& onSuccess,
                                   const Controller::DatasetFailCallback& onFailure,
@@ -68,16 +121,19 @@
 {
   os << "Strategy choices:\n";
   for (const StrategyChoice& item : m_status) {
-    this->formatItemText(os, item);
+    os << "  ";
+    formatItemText(os, item);
+    os << '\n';
   }
 }
 
 void
-StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item) const
+StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
 {
-  os << "  " << item.getName()
-     << " strategy=" << item.getStrategy()
-     << "\n";
+  text::ItemAttributes ia(wantMultiLine, 8);
+  os << ia("prefix") << item.getName()
+     << ia("strategy") << item.getStrategy()
+     << ia.end();
 }
 
 } // namespace nfdc