Use boost to parse command line options
authorPawel Broda <p.broda@partner.samsung.com>
Wed, 12 Mar 2014 17:38:24 +0000 (18:38 +0100)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:14 +0000 (13:47 +0200)
[Issue#]        PSDAC-167
[Bug/Feature]   N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Build and run on a target.

Change-Id: If39fd3966d79e6c92eed55b3df554e05b2ccbdd0

CMakeLists.txt
src/server/src/CMakeLists.txt
src/server/src/main.cpp

index b39ca9d..e50ece2 100644 (file)
@@ -49,6 +49,7 @@ ADD_DEFINITIONS("-Wall")   # Generate all warnings
 ADD_DEFINITIONS("-Wextra") # Generate even more extra warnings
 ADD_DEFINITIONS("-pedantic") # Be pedantic
 ADD_DEFINITIONS("-pedantic-errors") # Make pedantic warnings into errors
+ADD_DEFINITIONS(-DPROGRAM_VERSION="${VERSION}")
 
 
 ## Subdirectories ##############################################################
index f2c3b43..1563745 100644 (file)
@@ -26,9 +26,13 @@ SET(SERVER_CODENAME "${PROJECT_NAME}-server")
 ADD_EXECUTABLE(${SERVER_CODENAME} ${project_SRCS} )
 
 ## Link libraries ##############################################################
+FIND_PACKAGE (Boost COMPONENTS program_options REQUIRED)
+MESSAGE ("Include dirs of boost: " ${Boost_INCLUDE_DIRS} )
+MESSAGE ("Libs of boost:         " ${Boost_LIBRARIES} )
+
 PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt json)
-INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS})
-TARGET_LINK_LIBRARIES(${SERVER_CODENAME} ${SERVER_DEPS_LIBRARIES})
+INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
+TARGET_LINK_LIBRARIES(${SERVER_CODENAME} ${SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
 
 ## Install #####################################################################
 INSTALL(TARGETS ${SERVER_CODENAME} DESTINATION bin)
\ No newline at end of file
index a78d768..6fd1fe7 100644 (file)
  * @brief   Main file for the Security Containers Daemon
  */
 
+#include <boost/program_options.hpp>
 #include <iostream>
-#include <getopt.h>  // For getopt
+
+namespace po = boost::program_options;
+
+namespace {
+    const std::string PROGRAM_NAME_AND_VERSION =
+            "Security Containers Server " PROGRAM_VERSION;
+}
 
 int main(int argc, char* argv[])
 {
-    int optIndex = 0;
-
-    const option longOptions[] = {
-        {"help",    no_argument, 0, 'h'},
-        {"version", no_argument, 0, 'v'},
-        {0, 0, 0, 0}
-    };
-
-    for (;;) {
-        int opt = getopt_long(argc, argv,
-                              "hv", // ':' after arg is the parameter
-                              longOptions,
-                              &optIndex);
-        if (opt == -1) {
-            break;
-        }
+    po::options_description desc("Allowed options");
 
-        // If option comes with a parameter,
-        // the param is stored in optarg global variable by getopt_long.
-        switch (opt) {
-        case 0:
-            // A flag was set
-            break;
-
-        case '?':
-            // No such command.
-            // getopt_long already printed an error message to stderr.
-            return 1;
-
-        case 'v':
-            std::cout << "Security Containers Server v. 0.1.0" << std::endl;
-            return 0;
-
-        case 'h':
-            std::cout << "Security Containers Server v. 0.1.0          \n"
-                      << "    Options:                                 \n"
-                      << "        -h,--help     print this help        \n"
-                      << "        -v,--version  show applcation version"
-                      << std::endl;
-            return 0;
-
-        default:
-            break;
-        }
-    }
+    desc.add_options()
+        ("help,h", "print this help")
+        ("version,v", "show application version")
+    ;
+
+    po::variables_map vm;
+    po::basic_parsed_options< char > parsed =
+        po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
+
+    std::vector<std::string> unrecognized_options =
+        po::collect_unrecognized(parsed.options, po::include_positional);
 
-    // Print unknown remaining command line arguments
-    if (optind < argc) {
-        std::cerr << "Unknown options: ";
-        while (optind < argc) {
-            std::cerr << argv[optind++] << " ";
+    if (!unrecognized_options.empty()) {
+        std::cout << "Unrecognized options: ";
+
+        for (auto& uo: unrecognized_options) {
+            std::cout << ' ' << uo;
         }
-        std::cerr << std::endl;
+
+        std::cout << std::endl << std::endl;
+        std::cout << desc << std::endl;
+
         return 1;
     }
+
+    po::store(parsed, vm);
+    po::notify(vm);
+
+    if (vm.count("help")) {
+        std::cout << desc << std::endl;
+        return 0;
+    } else if (vm.count("version")) {
+        std::cout << PROGRAM_NAME_AND_VERSION << std::endl;
+        return 0;
+    }
+
+    return 0;
 }