ncsi: Add code to accept command line arguments

Accepts command line arguments for interface index,
ncsi channel,package etc.

Change-Id: Ib2b049a2fb017593a0a4329b26bbd64c65c8b580
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 459a377..8696d03 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,7 @@
 	xyz/openbmc_project/Network/VLAN/Create/server.hpp \
 	xyz/openbmc_project/Network/IP/Create/server.hpp
 
-sbin_PROGRAMS = phosphor-network-manager
+sbin_PROGRAMS = phosphor-network-manager ncsi-netlink
 
 noinst_HEADERS = \
 		ethernet_interface.hpp \
@@ -20,7 +20,13 @@
 		rtnetlink_server.hpp \
 		timer.hpp \
 		dns_updater.hpp \
-		watch.hpp
+		watch.hpp \
+		argument.hpp
+
+ncsi_netlink_SOURCES = \
+		argument.cpp \
+		ncsi_netlink_main.cpp
+
 
 phosphor_network_manager_SOURCES = \
 		ethernet_interface.cpp \
diff --git a/argument.cpp b/argument.cpp
new file mode 100644
index 0000000..603a54c
--- /dev/null
+++ b/argument.cpp
@@ -0,0 +1,99 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * 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.
+ */
+#include <iostream>
+#include <iterator>
+#include <algorithm>
+#include "argument.hpp"
+
+namespace phosphor
+{
+namespace network
+{
+namespace ncsi
+{
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+    int option = 0;
+    while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
+    {
+        if ((option == '?') || (option == 'h'))
+        {
+            usage(argv);
+            exit(-1);
+        }
+
+        auto i = &options[0];
+        while ((i->val != option) && (i->val != 0))
+        {
+            ++i;
+        }
+
+        if (i->val)
+        {
+            arguments[i->name] = (i->has_arg ? optarg : trueString);
+        }
+    }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+    auto i = arguments.find(opt);
+    if (i == arguments.end())
+    {
+        return emptyString;
+    }
+    else
+    {
+        return i->second;
+    }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+    std::cerr << "Usage: " << argv[0] << " [options]\n";
+    std::cerr << "Options:\n";
+    std::cerr << "    --help            Print this menu.\n";
+    std::cerr << "    --info=<info>     Retrieve info about NCSI topology.\n";
+    std::cerr << "    --set=<set>       Set a specific package/channel.\n";
+    std::cerr << "    --clear=<clear>   Clear all the settings on the interface.\n";
+    std::cerr << "    --package=<package>  Specify a package.\n";
+    std::cerr << "    --channel=<channel> Specify a channel.\n";
+    std::cerr << "    --index=<device index> Specify device ifindex.\n";
+    std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] =
+{
+    { "info", no_argument, NULL, 'i' },
+    { "set",   no_argument, NULL, 's' },
+    { "clear",   no_argument, NULL, 'r' },
+    { "package",  required_argument, NULL, 'p' },
+    { "channel",   required_argument, NULL, 'c' },
+    { "index",   required_argument, NULL, 'x' },
+    { "help",   no_argument,       NULL, 'h' },
+    { 0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionStr = "i:s:r:p:c:x:h?";
+
+const std::string ArgumentParser::trueString = "true";
+const std::string ArgumentParser::emptyString = "";
+
+} //namespace ncsi
+} //namespace network
+} //namespece phosphor
+
diff --git a/argument.hpp b/argument.hpp
new file mode 100644
index 0000000..51438b4
--- /dev/null
+++ b/argument.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace network
+{
+namespace ncsi
+{
+/** @brief Class - Encapsulates parsing command line options and
+ *                 populating arguments
+ */
+class ArgumentParser
+{
+    public:
+        ArgumentParser() = delete;
+        ~ArgumentParser() = default;
+        ArgumentParser(const ArgumentParser&) = delete;
+        ArgumentParser& operator=(const ArgumentParser&) = delete;
+        ArgumentParser(ArgumentParser&&) = default;
+        ArgumentParser& operator=(ArgumentParser&&) = default;
+
+        /** @brief Contructs Argument object
+         *
+         *  @param argc - the main function's argc passed as is
+         *  @param argv - the main function's argv passed as is
+         *  @return Object constructed
+         */
+        ArgumentParser(int argc, char** argv);
+
+        /** @brief Given an option, returns its argument(optarg)
+         *
+         *  @param opt - command line option string
+         *
+         *  @return argument which is a standard optarg
+         */
+        const std::string& operator[](const std::string& opt);
+
+        /** @brief Displays usage
+         *
+         *  @param argv - the main function's argv passed as is
+         */
+        static void usage(char** argv);
+
+        /** @brief Set to 'true' when an option is passed */
+        static const std::string trueString;
+
+        /** @brief Set to '' when an option is not passed */
+        static const std::string emptyString;
+
+    private:
+        /** @brief Option to argument mapping */
+        std::map<const std::string, std::string> arguments;
+
+        /** @brief Array of struct options as needed by getopt_long */
+        static const option options[];
+
+        /** @brief optstring as needed by getopt_long */
+        static const char* optionStr;
+};
+
+} // namespace ncsi
+} // namespace network
+} // namespace phosphor
+
diff --git a/ncsi_netlink_main.cpp b/ncsi_netlink_main.cpp
new file mode 100644
index 0000000..2441383
--- /dev/null
+++ b/ncsi_netlink_main.cpp
@@ -0,0 +1,22 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * 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.
+ */
+
+int main(int argc, char** argv)
+{
+    //TODO will implement later
+    return 0;
+}
+