From f63545c4a541e55fc14e120d830b5fa5b814cba7 Mon Sep 17 00:00:00 2001
From: Pawel Wieczorek
Date: Wed, 21 Jan 2015 14:12:42 +0100
Subject: [PATCH] Add version information to the main executable
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 | 3 +-
src/service/CMakeLists.txt | 1 +
src/service/main/CmdlineParser.cpp | 93 ++++++++++++++++++++++++++++++++++++++
src/service/main/CmdlineParser.h | 50 ++++++++++++++++++++
src/service/main/main.cpp | 10 +++-
5 files changed, 155 insertions(+), 2 deletions(-)
create mode 100644 src/service/main/CmdlineParser.cpp
create mode 100644 src/service/main/CmdlineParser.h
diff --git a/packaging/cynara.spec b/packaging/cynara.spec
index 7076933..2eb9a23 100644
--- a/packaging/cynara.spec
+++ b/packaging/cynara.spec
@@ -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 . \
diff --git a/src/service/CMakeLists.txt b/src/service/CMakeLists.txt
index b28058b..50392cc 100644
--- a/src/service/CMakeLists.txt
+++ b/src/service/CMakeLists.txt
@@ -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
index 0000000..cd8e4cb
--- /dev/null
+++ b/src/service/main/CmdlineParser.cpp
@@ -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
+ * @version 1.0
+ * @brief Helper namespace for Cynara's command-line options parsing
+ */
+
+#include
+#include
+#include
+
+#include "CmdlineParser.h"
+
+namespace Cynara {
+
+namespace CmdlineParser {
+
+std::ostream &operator<<(std::ostream &os, CmdlineOpt opt) {
+ return os << static_cast(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
index 0000000..31d87df
--- /dev/null
+++ b/src/service/main/CmdlineParser.h
@@ -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
+ * @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
+#include
+
+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_ */
diff --git a/src/service/main/main.cpp b/src/service/main/main.cpp
index a89e3a7..969510d 100644
--- a/src/service/main/main.cpp
+++ b/src/service/main/main.cpp
@@ -24,6 +24,7 @@
*/
#include
+#include
#include
#include
@@ -31,12 +32,19 @@
#include
#include
+
+#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();
--
2.7.4