Make the exit status consistent across all programs

(except nfdc)

Change-Id: Ia3edb11b3f4284df5db8279b06e6fb708ae454e9
diff --git a/tools/ndn-autoconfig-server/main.cpp b/tools/ndn-autoconfig-server/main.cpp
index ba836db..1077a8d 100644
--- a/tools/ndn-autoconfig-server/main.cpp
+++ b/tools/ndn-autoconfig-server/main.cpp
@@ -37,16 +37,16 @@
 static void
 usage(const char* programName)
 {
-  std::cout << "Usage:\n" << programName  << " [-h] [-V] [-p prefix] [-p prefix] ... hub-face\n"
+  std::cout << "Usage: " << programName << " [-h] [-V] [-p prefix]... <hub-face>\n"
+            << "\n"
+            << "Options:\n"
             << "  -h        - print usage and exit\n"
             << "  -V        - print version number and exit\n"
             << "  -p prefix - a local prefix of the HUB\n"
-            << "\n"
-            << "  hub-face  - a FaceUri to reach the HUB\n"
-            << std::endl;
+            << "  hub-face  - a FaceUri to reach the HUB\n";
 }
 
-int
+static int
 main(int argc, char** argv)
 {
   Options options;
@@ -75,7 +75,7 @@
   }
 
   if (!options.hubFaceUri.parse(argv[::optind])) {
-    std::cerr << "ERROR: cannot parse HUB FaceUri\n";
+    std::cerr << "ERROR: cannot parse HUB FaceUri" << std::endl;
     return 2;
   }
 
diff --git a/tools/ndn-autoconfig/main.cpp b/tools/ndn-autoconfig/main.cpp
index b7340a3..6a2a03f 100644
--- a/tools/ndn-autoconfig/main.cpp
+++ b/tools/ndn-autoconfig/main.cpp
@@ -43,11 +43,11 @@
 #pragma clang diagnostic ignored "-Wundefined-func-template"
 #endif
 
+// ndn-autoconfig is an NDN tool not an NFD tool, so it uses ndn::tools::autoconfig namespace.
+// It lives in NFD repository because nfd-start can automatically start ndn-autoconfig in daemon mode.
 namespace ndn {
 namespace tools {
 namespace autoconfig {
-// ndn-autoconfig is an NDN tool not an NFD tool, so it uses ndn::tools::autoconfig namespace.
-// It lives in NFD repository because nfd-start can automatically start ndn-autoconfig in daemon mode.
 
 static const time::nanoseconds DAEMON_INITIAL_DELAY = time::milliseconds(100);
 static const time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = time::hours(1);
@@ -57,13 +57,12 @@
 
 static void
 usage(std::ostream& os,
-      const po::options_description& optionsDescription,
+      const po::options_description& opts,
       const char* programName)
 {
-  os << "Usage:\n"
-     << "  " << programName << " [options]\n"
-     << "\n";
-  os << optionsDescription;
+  os << "Usage: " << programName << " [options]\n"
+     << "\n"
+     << opts;
 }
 
 static void
@@ -77,7 +76,7 @@
       return;
     }
     const char* signalName = ::strsignal(signalNo);
-    std::cerr << "Exit on signal ";
+    std::cerr << "Exiting on signal ";
     if (signalName == nullptr) {
       std::cerr << signalNo;
     }
@@ -115,15 +114,15 @@
   po::options_description optionsDescription("Options");
   optionsDescription.add_options()
     ("help,h", "print this message and exit")
-    ("version,V", "display version and exit")
+    ("version,V", "show version information and exit")
     ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
-     "run in daemon mode, detecting network change events and re-running auto-discovery procedure. "
+     "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
      "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
-     "NOTE: if connection to NFD fails, the daemon will be terminated.")
+     "NOTE: if the connection to NFD fails, the daemon will exit.")
     ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl),
      "URL for NDN-FCH (Find Closest Hub) service")
     ("config,c", po::value<std::string>(&configFile),
-     "configuration file. Exit immediately if `enabled = true` is not specified in config file.")
+     "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
     ;
 
   po::variables_map vm;
@@ -132,7 +131,7 @@
     po::notify(vm);
   }
   catch (const std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << "\n" << "\n\n";
+    std::cerr << "ERROR: " << e.what() << "\n\n";
     usage(std::cerr, optionsDescription, argv[0]);
     return 2;
   }
@@ -177,7 +176,7 @@
       runDaemon(proc);
     }
     else {
-      proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 3; });
+      proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
       proc.runOnce();
       face.processEvents();
     }
@@ -186,6 +185,7 @@
     std::cerr << ::nfd::getExtendedErrorMessage(e) << std::endl;
     return 1;
   }
+
   return exitCode;
 }
 
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index 5c7473f..5d74e4d 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -23,6 +23,7 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "core/extended-error-message.hpp"
 #include "core/network.hpp"
 #include "core/version.hpp"
 
@@ -75,19 +76,19 @@
   /**
    * \return true if uri has schema allowed to do auto-registrations
    */
-  bool
+  static bool
   hasAllowedSchema(const FaceUri& uri)
   {
     const std::string& scheme = uri.getScheme();
-    return (scheme == "udp4" || scheme == "tcp4" ||
-            scheme == "udp6" || scheme == "tcp6");
+    return scheme == "udp4" || scheme == "tcp4" ||
+           scheme == "udp6" || scheme == "tcp6";
   }
 
   /**
    * \return true if address is blacklisted
    */
   bool
-  isBlacklisted(const boost::asio::ip::address& address)
+  isBlacklisted(const boost::asio::ip::address& address) const
   {
     return std::any_of(m_blackList.begin(), m_blackList.end(),
                        bind(&Network::doesContain, _1, address));
@@ -97,7 +98,7 @@
    * \return true if address is whitelisted
    */
   bool
-  isWhitelisted(const boost::asio::ip::address& address)
+  isWhitelisted(const boost::asio::ip::address& address) const
   {
     return std::any_of(m_whiteList.begin(), m_whiteList.end(),
                        bind(&Network::doesContain, _1, address));
@@ -160,15 +161,14 @@
     m_face.shutdown();
   }
 
-  void
+  static void
   usage(std::ostream& os,
         const boost::program_options::options_description& desc,
         const char* programName)
   {
-    os << "Usage:\n"
-       << "  " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
-       << "\n";
-    os << desc;
+    os << "Usage: " << programName << " [--prefix=</autoreg/prefix>]... [options]\n"
+       << "\n"
+       << desc;
   }
 
   void
@@ -222,41 +222,40 @@
   {
     namespace po = boost::program_options;
 
-    po::options_description optionDesciption;
-    optionDesciption.add_options()
-      ("help,h", "produce help message")
+    po::options_description optionsDesc("Options");
+    optionsDesc.add_options()
+      ("help,h", "print this message and exit")
+      ("version,V", "show version information and exit")
       ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
-       "prefix that should be automatically registered when new a remote non-local face is "
-       "established")
+       "prefix that should be automatically registered when a new non-local face is created")
       ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
        "prefix that should be automatically registered for all TCP and UDP non-local faces "
        "(blacklists and whitelists do not apply to this prefix)")
       ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
-       "FIB cost which should be assigned to autoreg nexthops")
+       "FIB cost that should be assigned to autoreg nexthops")
       ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
        "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
       ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
        "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
-      ("version,V", "show version and exit")
       ;
 
     po::variables_map options;
     try {
-      po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
+      po::store(po::parse_command_line(argc, argv, optionsDesc), options);
       po::notify(options);
     }
     catch (const std::exception& e) {
       std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
-      usage(std::cerr, optionDesciption, argv[0]);
-      return 1;
+      usage(std::cerr, optionsDesc, argv[0]);
+      return 2;
     }
 
-    if (options.count("help")) {
-      usage(std::cout, optionDesciption, argv[0]);
+    if (options.count("help") > 0) {
+      usage(std::cout, optionsDesc, argv[0]);
       return 0;
     }
 
-    if (options.count("version")) {
+    if (options.count("version") > 0) {
       std::cout << NFD_VERSION_BUILD_STRING << std::endl;
       return 0;
     }
@@ -264,7 +263,7 @@
     if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
       std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
                 << std::endl << std::endl;
-      usage(std::cerr, optionDesciption, argv[0]);
+      usage(std::cerr, optionsDesc, argv[0]);
       return 2;
     }
 
@@ -279,8 +278,8 @@
       startProcessing();
     }
     catch (const std::exception& e) {
-      std::cerr << "ERROR: " << e.what() << std::endl;
-      return 2;
+      std::cerr << "ERROR: " << ::nfd::getExtendedErrorMessage(e) << std::endl;
+      return 1;
     }
 
     return 0;