ca-server: cleanup command-line options handling

Change-Id: I1acd7c7c776ee89cbc6c6f0b7e90c0ed42dcbb85
diff --git a/.mailmap b/.mailmap
new file mode 100644
index 0000000..7bb263c
--- /dev/null
+++ b/.mailmap
@@ -0,0 +1,5 @@
+Zhiyi Zhang <zhiyi@cs.ucla.edu> <zhiyi.zhang@ucla.edu>
+Alexander Afanasyev <aa@cs.fiu.edu> <aa@cs.ucla.edu>
+Alexander Afanasyev <aa@cs.fiu.edu> <alexander.afanasyev@ucla.edu>
+<davidepesa@gmail.com> <davide.pesavento@lip6.fr>
+Md Ashiqur Rahman <marahman@email.arizona.edu>
diff --git a/tools/ndncert-ca-server.cpp b/tools/ndncert-ca-server.cpp
index c666d34..4dd518d 100644
--- a/tools/ndncert-ca-server.cpp
+++ b/tools/ndncert-ca-server.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2018, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2019, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -21,61 +21,61 @@
 #include "ca-module.hpp"
 #include "challenge-module.hpp"
 
-#include <iostream>
-#include <sstream>
-#include <string>
-#include <ndn-cxx/util/io.hpp>
-#include <boost/program_options/options_description.hpp>
-#include <boost/program_options/variables_map.hpp>
-#include <boost/program_options/parsers.hpp>
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/security/v2/key-chain.hpp>
+
+#include <boost/asio/ip/tcp.hpp>
+#if BOOST_VERSION < 106700
 #include <boost/date_time/posix_time/posix_time_duration.hpp>
-#include <boost/asio.hpp>
+#endif
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/parsers.hpp>
+#include <boost/program_options/variables_map.hpp>
+
+#include <iostream>
 
 namespace ndn {
 namespace ndncert {
 
-int
+static int
 main(int argc, char* argv[])
 {
-  std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/ca.conf";
-  std::string repoPrefix;
-  std::string repoCaIdentity;
-  std::string repoHost;
-  std::string repoPort;
-  bool isRepoOut = false;
+  std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
+  std::string repoHost("localhost");
+  std::string repoPort("7376");
+  bool wantRepoOut = false;
 
   namespace po = boost::program_options;
-  po::options_description description("General Usage\n  ndncert-ca [-h] [-f] [-r] [-c]\n");
-  description.add_options()
-    ("help,h",
-     "produce help message")
-    ("config-file,f", po::value<std::string>(&configFilePath),
-     "config file name")
-    ("repo-output,r",
-     "when enabled, all issued certificates will be published to repo-ng")
-    ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
-     "repo-ng host")
-    ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
-     "repo-ng port");
+  po::options_description optsDesc("Options");
+  optsDesc.add_options()
+    ("help,h",        "print this help message and exit")
+    ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath),
+                      "path to configuration file")
+    ("repo-output,r", po::bool_switch(&wantRepoOut),
+                      "when enabled, all issued certificates will be published to repo-ng")
+    ("repo-host,H",   po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
+    ("repo-port,P",   po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
 
-  po::positional_options_description p;
   po::variables_map vm;
   try {
-    po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
+    po::store(po::parse_command_line(argc, argv, optsDesc), vm);
     po::notify(vm);
   }
-  catch (const std::exception& e) {
-    std::cerr << "ERROR: " << e.what()
-              << "\n" << description << std::endl;
-    return 1;
+  catch (const po::error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
   }
+  catch (const boost::bad_any_cast& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
+  }
+
   if (vm.count("help") != 0) {
-    std::cerr << description << std::endl;
+    std::cout << "Usage: " << argv[0] << " [options]\n"
+              << "\n"
+              << optsDesc;
     return 0;
   }
-  if (vm.count("repo-ng-output") != 0) {
-    isRepoOut = true;
-  }
 
   Face face;
   security::v2::KeyChain keyChain;
@@ -96,15 +96,13 @@
       return std::make_tuple(recommendedCa, identity);
     });
 
-  if (isRepoOut) {
-    auto config = ca.getCaConf();
-    for (const auto& caItem : config.m_caItems) {
+  if (wantRepoOut) {
+    for (const auto& caItem : ca.getCaConf().m_caItems) {
       ca.setStatusUpdateCallback(caItem.m_caName,
         [&] (const CertificateRequest& request) {
           if (request.getStatus() == ChallengeModule::SUCCESS) {
             auto issuedCert = request.getCert();
-            using namespace boost::asio::ip;
-            tcp::iostream requestStream;
+            boost::asio::ip::tcp::iostream requestStream;
 #if BOOST_VERSION >= 106700
             requestStream.expires_after(std::chrono::seconds(3));
 #else
@@ -113,14 +111,13 @@
             requestStream.connect(repoHost, repoPort);
             if (!requestStream) {
               std::cerr << "ERROR: Cannot publish certificate to repo-ng"
-                        << " (" << requestStream.error().message() << ")"
-                        << std::endl;
+                        << " (" << requestStream.error().message() << ")" << std::endl;
               return;
             }
             requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()),
                                 issuedCert.wireEncode().size());
           }
-        });
+      });
     }
   }