Parsing options in SCS binary.
authorJan Olszak <j.olszak@samsung.com>
Fri, 14 Feb 2014 11:42:22 +0000 (12:42 +0100)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:14 +0000 (13:47 +0200)
         [Issue#]      PSDAC-64
         [Bug]         N/A
         [Cause]       N/A
         [Solution]    N/A
         [Verfication] Build and install all packages with rpm.
                       run: security-containers-server -h
                       run: security-containers-server -v

Change-Id: I9a1e10578968437495209dc5b858af839f822827

src/server/include/sc-server.hpp [deleted file]
src/server/include/scs.hpp [new file with mode: 0644]
src/server/src/main.cpp
src/server/src/scs.cpp [moved from src/server/src/sc-server.cpp with 100% similarity]

diff --git a/src/server/include/sc-server.hpp b/src/server/include/sc-server.hpp
deleted file mode 100644 (file)
index f2b538c..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-#ifndef SC_SERVER_H
-#define SC_SERVER_H
-
-#endif /*SC_SERVER_H*/
diff --git a/src/server/include/scs.hpp b/src/server/include/scs.hpp
new file mode 100644 (file)
index 0000000..ccda2ce
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef SECURITY_CONTAINERS_SERVER_SCS_H
+#define SECURITY_CONTAINERS_SERVER_SCS_H
+
+#endif // SECURITY_CONTAINERS_SERVER_SCS_H
index b8c29e1..243fa6e 100644 (file)
 
 
 #include <iostream>
+#include <getopt.h>             // For getopt
 
-#include <sc-server.hpp>
-
-int main(int argc, const char *argv[])
+int main(int argc, char *argv[])
 {
-    std::cout << "SC FTW!" << std::endl;
-    std::cout << argc << std::endl;
-    for (int i = 0; i < argc; ++i){
-        std::cout << argv[i] << std::endl;
+    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;
+        }
+
+        // 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;
+        }
+    }
+
+    // Print unknown remaining command line arguments
+    if (optind < argc) {
+        std::cerr << "Unknown options: ";
+        while (optind < argc) {
+            std::cerr << argv[optind++] << " ";
+        }
+        std::cerr << std::endl;
+        return 1;
     }
 }
\ No newline at end of file