tools: normalize exit codes

Change-Id: I6811d1fbfef817e74b5920ccb0026447c3238051
diff --git a/tools/ndngetfile.cpp b/tools/ndngetfile.cpp
index b552ec8..4844fd6 100644
--- a/tools/ndngetfile.cpp
+++ b/tools/ndngetfile.cpp
@@ -23,6 +23,7 @@
 
 #include <fstream>
 #include <iostream>
+#include <unistd.h>
 
 #include <boost/lexical_cast.hpp>
 
@@ -260,20 +261,21 @@
   }
 }
 
-static int
-usage(const std::string& filename)
+static void
+usage(const char* programName)
 {
-  std::cerr << "Usage: \n    "
-            << filename << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n"
-            << "-v: be verbose\n"
-            << "-s: only get single data packet\n"
-            << "-u: versioned: ndn-name contains version component\n"
-            << "    if -u is not specified, this command will return the rightmost child for the prefix\n"
-            << "-l: InterestLifetime in milliseconds\n"
-            << "-w: timeout in milliseconds for whole process (default unlimited)\n"
-            << "-o: write to local file name instead of stdout\n"
-            << "ndn-name: NDN Name prefix for Data to be read\n";
-  return 1;
+  std::cerr << "Usage: "
+            << programName << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n"
+            << "\n"
+            << "  -v: be verbose\n"
+            << "  -s: only get single data packet\n"
+            << "  -u: versioned: ndn-name contains version component\n"
+            << "      if -u is not specified, this command will return the rightmost child for the prefix\n"
+            << "  -l: InterestLifetime in milliseconds\n"
+            << "  -w: timeout in milliseconds for whole process (default unlimited)\n"
+            << "  -o: write to local file name instead of stdout\n"
+            << "  ndn-name: NDN Name prefix for Data to be read\n"
+            << std::endl;
 }
 
 static int
@@ -286,42 +288,46 @@
   int timeout = 0;  // in milliseconds
 
   int opt;
-  while ((opt = getopt(argc, argv, "vsul:w:o:")) != -1) {
+  while ((opt = getopt(argc, argv, "hvsul:w:o:")) != -1) {
     switch (opt) {
-      case 'v':
-        verbose = true;
-        break;
-      case 's':
-        single = true;
-        break;
-      case 'u':
-        versioned = true;
-        break;
-      case 'l':
-        try {
-          interestLifetime = boost::lexical_cast<int>(optarg);
-        }
-        catch (const boost::bad_lexical_cast&) {
-          std::cerr << "ERROR: -l option should be an integer" << std::endl;
-          return 1;
-        }
-        interestLifetime = std::max(interestLifetime, 0);
-        break;
-      case 'w':
-        try {
-          timeout = boost::lexical_cast<int>(optarg);
-        }
-        catch (const boost::bad_lexical_cast&) {
-          std::cerr << "ERROR: -w option should be an integer" << std::endl;
-          return 1;
-        }
-        timeout = std::max(timeout, 0);
-        break;
-      case 'o':
-        outputFile = optarg;
-        break;
-      default:
-        return usage(argv[0]);
+    case 'h':
+      usage(argv[0]);
+      return 0;
+    case 'v':
+      verbose = true;
+      break;
+    case 's':
+      single = true;
+      break;
+    case 'u':
+      versioned = true;
+      break;
+    case 'l':
+      try {
+        interestLifetime = boost::lexical_cast<int>(optarg);
+      }
+      catch (const boost::bad_lexical_cast&) {
+        std::cerr << "ERROR: -l option should be an integer" << std::endl;
+        return 2;
+      }
+      interestLifetime = std::max(interestLifetime, 0);
+      break;
+    case 'w':
+      try {
+        timeout = boost::lexical_cast<int>(optarg);
+      }
+      catch (const boost::bad_lexical_cast&) {
+        std::cerr << "ERROR: -w option should be an integer" << std::endl;
+        return 2;
+      }
+      timeout = std::max(timeout, 0);
+      break;
+    case 'o':
+      outputFile = optarg;
+      break;
+    default:
+      usage(argv[0]);
+      return 2;
     }
   }
 
@@ -330,7 +336,8 @@
   }
 
   if (name.empty()) {
-    return usage(argv[0]);
+    usage(argv[0]);
+    return 2;
   }
 
   std::streambuf* buf;
@@ -340,7 +347,7 @@
     of.open(outputFile, std::ios::out | std::ios::binary | std::ios::trunc);
     if (!of || !of.is_open()) {
       std::cerr << "ERROR: cannot open " << outputFile << std::endl;
-      return 1;
+      return 2;
     }
     buf = of.rdbuf();
   }
@@ -356,6 +363,7 @@
   }
   catch (const std::exception& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
   }
 
   return 0;