summaryrefslogtreecommitdiffstats
path: root/argument.cpp
diff options
context:
space:
mode:
authorMatthew Barth <msbarth@us.ibm.com>2016-10-18 14:33:17 -0500
committerMatthew Barth <msbarth@us.ibm.com>2016-10-18 14:33:17 -0500
commit155c34fbb61071f5b51240c4a50b49391e0877c1 (patch)
treead24d5388d2c8d04ab9e5f1d77a9cf221265d234 /argument.cpp
parent619db930483505aa4352b8ae30d6c6b5a9b569cf (diff)
downloadipmi-fru-parser-155c34fbb61071f5b51240c4a50b49391e0877c1.tar.gz
ipmi-fru-parser-155c34fbb61071f5b51240c4a50b49391e0877c1.zip
Change H->hpp & C->cpp
Change-Id: I4bb8cf7a58c517f348a524d3e027ebcd1dcd0dea Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
Diffstat (limited to 'argument.cpp')
-rw-r--r--argument.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/argument.cpp b/argument.cpp
new file mode 100644
index 0000000..a988ca5
--- /dev/null
+++ b/argument.cpp
@@ -0,0 +1,62 @@
+#include <iostream>
+#include <iterator>
+#include <algorithm>
+#include <cassert>
+#include "argument.hpp"
+
+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 : true_string);
+ }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+ auto i = arguments.find(opt);
+ if (i == arguments.end())
+ {
+ return empty_string;
+ }
+ else
+ {
+ return i->second;
+ }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+ std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
+ std::cerr << "Options:" << std::endl;
+ std::cerr << " --eeprom=<eeprom file path> Absolute file name of eeprom"
+ << std::endl;
+ std::cerr << " --fruid=<FRU ID> valid fru id in integer"
+ << std::endl;
+ std::cerr << " --help display help"
+ << std::endl;
+}
+
+const option ArgumentParser::options[] =
+{
+ { "eeprom", required_argument, NULL, 'e' },
+ { "fruid", required_argument, NULL, 'f' },
+ { "help", no_argument, NULL, 'h' },
+ { 0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionstr = "e:f:?h";
+
+const std::string ArgumentParser::true_string = "true";
+const std::string ArgumentParser::empty_string = "";
OpenPOWER on IntegriCloud