ndnpeek: replace --link-file with --fwhint

refs #4207, #5187

Change-Id: I8213e971520d3edab631634a03bf7950d265dc11
diff --git a/core/program-options-ext.cpp b/core/program-options-ext.cpp
new file mode 100644
index 0000000..5667aab
--- /dev/null
+++ b/core/program-options-ext.cpp
@@ -0,0 +1,45 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2022,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "core/program-options-ext.hpp"
+
+namespace ndn {
+
+void
+validate(boost::any& v, const std::vector<std::string>& values, Name*, int)
+{
+  using namespace boost::program_options;
+
+  validators::check_first_occurrence(v);
+  const std::string& s = validators::get_single_string(values);
+
+  try {
+    v = Name(s);
+  } catch (const Name::Error&) {
+    throw invalid_option_value(s);
+  }
+}
+
+} // namespace ndn
diff --git a/core/program-options-ext.hpp b/core/program-options-ext.hpp
new file mode 100644
index 0000000..16b4e41
--- /dev/null
+++ b/core/program-options-ext.hpp
@@ -0,0 +1,42 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2022,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP
+#define NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP
+
+#include <ndn-cxx/name.hpp>
+#include <boost/program_options/value_semantic.hpp>
+
+namespace ndn {
+
+/**
+ * @brief Provide a Boost.Program_options custom validator for ndn::Name type.
+ */
+void
+validate(boost::any& v, const std::vector<std::string>& values, Name*, int);
+
+} // namespace ndn
+
+#endif // NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP
diff --git a/manpages/ndnpeek.rst b/manpages/ndnpeek.rst
index 2b50603..9d42a7a 100644
--- a/manpages/ndnpeek.rst
+++ b/manpages/ndnpeek.rst
@@ -28,8 +28,9 @@
 ``-f, --fresh``
   If specified, include ``MustBeFresh`` element in the Interest packet.
 
-``--link-file <file>``
-  Read Link object from ``file`` and add it as ``ForwardingHint`` to the Interest packet.
+``-F, --fwhint <name>``
+  Add a delegation name to the ``ForwardingHint`` of the Interest packet.
+  This option may be repeated to specify multiple delegation names.
 
 ``-l, --lifetime <lifetime>``
   Set ``lifetime`` (in milliseconds) as the ``InterestLifetime``.
diff --git a/tests/peek/ndnpeek.t.cpp b/tests/peek/ndnpeek.t.cpp
index 9951e5c..336b1de 100644
--- a/tests/peek/ndnpeek.t.cpp
+++ b/tests/peek/ndnpeek.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2020,  Arizona Board of Regents.
+ * Copyright (c) 2014-2022,  Arizona Board of Regents.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -155,12 +155,13 @@
 
   OutputCheck::checkOutput(output, *data);
   BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
-  BOOST_CHECK(face.sentInterests.back().getHopLimit() == nullopt);
-  BOOST_CHECK(!face.sentInterests.back().hasApplicationParameters());
+  const auto& interest = face.sentInterests.back();
+  BOOST_CHECK_EQUAL(interest.getCanBePrefix(), false);
+  BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
+  BOOST_CHECK_EQUAL(interest.getForwardingHint().empty(), true);
+  BOOST_CHECK_EQUAL(interest.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
+  BOOST_CHECK(interest.getHopLimit() == nullopt);
+  BOOST_CHECK(!interest.hasApplicationParameters());
   BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
 }
 
@@ -169,6 +170,7 @@
   auto options = OutputCheck::makeOptions();
   options.canBePrefix = true;
   options.mustBeFresh = true;
+  options.forwardingHint.emplace_back("/fh");
   options.interestLifetime = 200_ms;
   options.hopLimit = 64;
   initialize(options);
@@ -187,12 +189,14 @@
 
   OutputCheck::checkOutput(output, *data);
   BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
-  BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms);
-  BOOST_CHECK(face.sentInterests.back().getHopLimit() == 64);
-  BOOST_CHECK(!face.sentInterests.back().hasApplicationParameters());
+  const auto& interest = face.sentInterests.back();
+  BOOST_CHECK_EQUAL(interest.getCanBePrefix(), true);
+  BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
+  BOOST_TEST(interest.getForwardingHint() == std::vector<Name>({"/fh"}),
+             boost::test_tools::per_element());
+  BOOST_CHECK_EQUAL(interest.getInterestLifetime(), 200_ms);
+  BOOST_CHECK(interest.getHopLimit() == 64);
+  BOOST_CHECK(!interest.hasApplicationParameters());
   BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
 }
 
diff --git a/tools/peek/ndnpeek/main.cpp b/tools/peek/ndnpeek/main.cpp
index ff9f2af..22523b7 100644
--- a/tools/peek/ndnpeek/main.cpp
+++ b/tools/peek/ndnpeek/main.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2021,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -28,6 +28,7 @@
  */
 
 #include "ndnpeek.hpp"
+#include "core/program-options-ext.hpp"
 #include "core/version.hpp"
 
 #include <ndn-cxx/util/io.hpp>
@@ -85,7 +86,8 @@
   interestOptDesc.add_options()
     ("prefix,P",   po::bool_switch(&options.canBePrefix), "set CanBePrefix")
     ("fresh,f",    po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
-    ("link-file",  po::value<std::string>(), "set ForwardingHint from a raw binary file")
+    ("fwhint,F",   po::value<std::vector<Name>>(&options.forwardingHint)->composing(),
+                   "add ForwardingHint delegation name")
     ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
                    "set InterestLifetime, in milliseconds")
     ("hop-limit,H",     po::value<int>(), "set HopLimit")
@@ -98,7 +100,7 @@
 
   po::options_description hiddenOptDesc;
   hiddenOptDesc.add_options()
-    ("name", po::value<std::string>(), "Interest name");
+    ("name", po::value<Name>(&options.name), "Interest name");
 
   po::options_description optDesc;
   optDesc.add(visibleOptDesc).add(hiddenOptDesc);
@@ -132,14 +134,6 @@
     return 2;
   }
 
-  try {
-    options.name = vm["name"].as<std::string>();
-  }
-  catch (const Name::Error& e) {
-    std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
-    return 2;
-  }
-
   if (vm.count("timeout") > 0) {
     options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
     if (*options.timeout < 0_ms) {
@@ -148,19 +142,6 @@
     }
   }
 
-  if (vm.count("link-file") > 0) {
-    auto filename = vm["link-file"].as<std::string>();
-    std::ifstream linkFile = openBinaryFile(filename);
-    if (!linkFile) {
-      return 2;
-    }
-    options.link = io::load<Link>(linkFile, io::NO_ENCODING);
-    if (!options.link) {
-      std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl;
-      return 2;
-    }
-  }
-
   options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
   if (options.interestLifetime < 0_ms) {
     std::cerr << "ERROR: InterestLifetime cannot be negative" << std::endl;
diff --git a/tools/peek/ndnpeek/ndnpeek.cpp b/tools/peek/ndnpeek/ndnpeek.cpp
index cc9286c..01fd6de 100644
--- a/tools/peek/ndnpeek/ndnpeek.cpp
+++ b/tools/peek/ndnpeek/ndnpeek.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -63,9 +63,7 @@
   Interest interest(m_options.name);
   interest.setCanBePrefix(m_options.canBePrefix);
   interest.setMustBeFresh(m_options.mustBeFresh);
-  if (m_options.link) {
-    interest.setForwardingHint(m_options.link->getDelegationList());
-  }
+  interest.setForwardingHint(m_options.forwardingHint);
   interest.setInterestLifetime(m_options.interestLifetime);
   interest.setHopLimit(m_options.hopLimit);
   if (m_options.applicationParameters) {
diff --git a/tools/peek/ndnpeek/ndnpeek.hpp b/tools/peek/ndnpeek/ndnpeek.hpp
index afb0ea7..ce37712 100644
--- a/tools/peek/ndnpeek/ndnpeek.hpp
+++ b/tools/peek/ndnpeek/ndnpeek.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2021,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -47,7 +47,7 @@
   Name name;
   bool canBePrefix = false;
   bool mustBeFresh = false;
-  shared_ptr<Link> link;
+  std::vector<Name> forwardingHint;
   time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME;
   optional<uint8_t> hopLimit;
   shared_ptr<Buffer> applicationParameters;