Add version information to the main executable 97/34397/9
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Wed, 21 Jan 2015 13:12:42 +0000 (14:12 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 24 Feb 2015 15:02:32 +0000 (16:02 +0100)
In order to comply with GNU Coding Standards for command-line
interfaces, Cynara has to report its current version.

Cynara now responds to two command-line options:
* -V, --version    prints installed version of Cynara,
* -h, --help       prints help message.

Change-Id: I386a09d00f1542cbff8db6a4b9eb2ac9a7fab9fb

packaging/cynara.spec
src/service/CMakeLists.txt
src/service/main/CmdlineParser.cpp [new file with mode: 0644]
src/service/main/CmdlineParser.h [new file with mode: 0644]
src/service/main/main.cpp

index 7076933..2eb9a23 100644 (file)
@@ -185,7 +185,8 @@ export CXXFLAGS="$CXXFLAGS -Wp,-U_FORTIFY_SOURCE"
 export CXXFLAGS="$CXXFLAGS -DCYNARA_STATE_PATH=\\\"%{state_path}\\\" \
                            -DCYNARA_LIB_PATH=\\\"%{lib_path}\\\" \
                            -DCYNARA_TESTS_DIR=\\\"%{tests_dir}\\\" \
-                           -DCYNARA_CONFIGURATION_DIR=\\\"%{conf_path}\\\""
+                           -DCYNARA_CONFIGURATION_DIR=\\\"%{conf_path}\\\" \
+                           -DCYNARA_VERSION=\\\"%{version}\\\""
 export LDFLAGS+="-Wl,--rpath=%{_libdir}"
 
 %cmake . \
index b28058b..50392cc 100644 (file)
@@ -22,6 +22,7 @@ SET(CYNARA_SOURCES
     ${CYNARA_SERVICE_PATH}/agent/AgentManager.cpp
     ${CYNARA_SERVICE_PATH}/agent/AgentTalker.cpp
     ${CYNARA_SERVICE_PATH}/logic/Logic.cpp
+    ${CYNARA_SERVICE_PATH}/main/CmdlineParser.cpp
     ${CYNARA_SERVICE_PATH}/main/Cynara.cpp
     ${CYNARA_SERVICE_PATH}/main/main.cpp
     ${CYNARA_SERVICE_PATH}/request/CheckRequestManager.cpp
diff --git a/src/service/main/CmdlineParser.cpp b/src/service/main/CmdlineParser.cpp
new file mode 100644 (file)
index 0000000..cd8e4cb
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/**
+ * @file        src/service/main/CmdlineParser.cpp
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Helper namespace for Cynara's command-line options parsing
+ */
+
+#include <getopt.h>
+#include <iostream>
+#include <sstream>
+
+#include "CmdlineParser.h"
+
+namespace Cynara {
+
+namespace CmdlineParser {
+
+std::ostream &operator<<(std::ostream &os, CmdlineOpt opt) {
+    return os << static_cast<char>(opt);
+}
+
+bool handleCmdlineOptions(int argc, char * const *argv) {
+    const std::string execName(argv[0]);
+    std::stringstream shortOpts;
+    shortOpts << ":" << CmdlineOpt::Help << CmdlineOpt::Version;
+
+    const struct option longOpts[] = {
+        { "help",       no_argument,    NULL, CmdlineOpt::Help },
+        { "version",    no_argument,    NULL, CmdlineOpt::Version },
+        { NULL, 0, NULL, 0 }
+    };
+
+
+    optind = 0; // On entry to `getopt', zero means this is the first call; initialize.
+    int opt;
+    while ((opt = getopt_long(argc, argv, shortOpts.str().c_str(), longOpts, nullptr)) != -1) {
+        switch (opt) {
+            case CmdlineOpt::Help:
+                printHelp(execName);
+                return true;
+            case CmdlineOpt::Version:
+                printVersion();
+                return true;
+            case '?': // Unknown option
+            default:
+                printUnknownOption(execName);
+                return false;
+        }
+    }
+
+    printNoOptions(execName);
+    return false;
+}
+
+void printHelp(const std::string &execName) {
+    std::cout << "Usage: " << execName << " [OPTIONS]" << std::endl << std::endl;
+    std::cout << "  -V, --version                  print version of " << execName << " and exit"
+              << std::endl;
+    std::cout << "  -h, --help                     print this help message and exit" << std::endl;
+}
+
+void printVersion(void) {
+    std::cout << std::string(CYNARA_VERSION) << std::endl;
+}
+
+void printUnknownOption(const std::string &execName) {
+    std::cerr << "Unknown option" << std::endl;
+    printHelp(execName);
+}
+
+void printNoOptions(const std::string &execName) {
+    std::cerr << "No options given" << std::endl;
+    printHelp(execName);
+}
+
+} /* namespace CmdlineOpts */
+
+} /* namespace Cynara */
diff --git a/src/service/main/CmdlineParser.h b/src/service/main/CmdlineParser.h
new file mode 100644 (file)
index 0000000..31d87df
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/**
+ * @file        src/service/main/CmdlineParser.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Helper namespace for Cynara's command-line options parsing
+ */
+
+#ifndef SRC_SERVICE_MAIN_CMDLINEPARSER_H_
+#define SRC_SERVICE_MAIN_CMDLINEPARSER_H_
+
+#include <ostream>
+#include <string>
+
+namespace Cynara {
+
+namespace CmdlineParser {
+
+enum CmdlineOpt {
+    Help = 'h',
+    Version = 'V'
+};
+
+std::ostream &operator<<(std::ostream &os, CmdlineOpt opt);
+
+bool handleCmdlineOptions(int argc, char * const *argv);
+void printHelp(const std::string &execName);
+void printVersion(void);
+void printUnknownOption(const std::string &execName);
+void printNoOptions(const std::string &execName);
+
+} /* namespace CmdlineOpts */
+
+} /* namespace Cynara */
+
+#endif /* SRC_SERVICE_MAIN_CMDLINEPARSER_H_ */
index a89e3a7..969510d 100644 (file)
@@ -24,6 +24,7 @@
  */
 
 #include <exception>
+#include <iostream>
 #include <stdlib.h>
 
 #include <systemd/sd-journal.h>
 
 #include <common.h>
 #include <log/log.h>
+
+#include "CmdlineParser.h"
 #include "Cynara.h"
 
-int main(int argc UNUSED, char **argv UNUSED) {
+int main(int argc, char **argv) {
     init_log();
 
     try {
+        if (1 < argc) {
+            auto handlingSuccess = Cynara::CmdlineParser::handleCmdlineOptions(argc, argv);
+            return (handlingSuccess ? EXIT_SUCCESS : EXIT_FAILURE);
+        }
+
         Cynara::Cynara cynara;
         LOGI("Cynara service is starting ...");
         cynara.init();