transition to cli11 instead of custom arg parser
Transition to use cli11 for argument parsing instead of a custom
argument parser.
Tested: Not tested on hardware.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I9c516c8c2d76e35c6c31592f96e8f281e9d47ad6
diff --git a/Makefile.am b/Makefile.am
index 1dd50c3..6e65ea1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,7 +5,7 @@
sbin_PROGRAMS = phosphor-read-eeprom
-phosphor_read_eeprom_SOURCES = readeeprom.cpp argument.cpp
+phosphor_read_eeprom_SOURCES = readeeprom.cpp
phosphor_read_eeprom_LDFLAGS = $(SYSTEMD_LIBS) \
$(libmapper_LIBS) \
$(PHOSPHOR_LOGGING_LIBS)
diff --git a/argument.cpp b/argument.cpp
deleted file mode 100644
index f46206a..0000000
--- a/argument.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include "argument.hpp"
-
-#include <algorithm>
-#include <cassert>
-#include <iostream>
-#include <iterator>
-
-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 = "";
diff --git a/argument.hpp b/argument.hpp
deleted file mode 100644
index 849ccb5..0000000
--- a/argument.hpp
+++ /dev/null
@@ -1,28 +0,0 @@
-#ifndef __ARGUMENT_H
-#define __ARGUMENT_H
-#include <getopt.h>
-
-#include <map>
-#include <string>
-class ArgumentParser
-{
- public:
- ArgumentParser(int argc, char** argv);
- const std::string& operator[](const std::string& opt);
-
- static void usage(char** argv);
-
- static const std::string true_string;
- static const std::string empty_string;
-
- private:
- std::map<const std::string, std::string> arguments;
-
- static const option options[];
- static const char* optionstr;
-
- private:
- ArgumentParser(){};
-};
-
-#endif
diff --git a/configure.ac b/configure.ac
index 7ee9a75..19da3d3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -24,6 +24,12 @@
PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus])
PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging])
PKG_CHECK_MODULES([LIBIPMID], [libipmid])
+# We need the header only CLI library
+AC_CHECK_HEADERS(
+ [CLI/CLI.hpp],
+ [],
+ [AC_MSG_ERROR([Could not find CLI11 CLI/CLI.hpp])]
+)
# Checks for header files.
AC_CHECK_HEADER(systemd/sd-bus.h, ,[AC_MSG_ERROR([Could not find systemd/sd-bus.h...systemd development package required])])
diff --git a/readeeprom.cpp b/readeeprom.cpp
index f5fa148..2c74fbd 100644
--- a/readeeprom.cpp
+++ b/readeeprom.cpp
@@ -1,6 +1,6 @@
-#include "argument.hpp"
#include "writefrudata.hpp"
+#include <CLI/CLI.hpp>
#include <cstdlib>
#include <cstring>
#include <iostream>
@@ -9,52 +9,6 @@
using namespace phosphor::logging;
-static void exit_with_error(const char* err, char** argv)
-{
- ArgumentParser::usage(argv);
- std::cerr << std::endl;
- std::cerr << "ERROR: " << err << std::endl;
- exit(-1);
-}
-
-static uint8_t parse_fruid_or_exit(const char* fruid_str, char** argv)
-{
- const uint8_t MAX_FRU_ID = 0xfe;
- unsigned long fruid;
- char* endptr = NULL;
-
- // The FRUID string must not be empty.
- if (fruid_str == nullptr || *fruid_str == '\0')
- {
- exit_with_error("Empty fruid.", argv);
- }
-
- errno = 0;
- fruid = std::strtoul(fruid_str, &endptr, 16);
-
- // Handle error cases
- if (errno == ERANGE)
- {
- exit_with_error("fruid is out of range.", argv);
- }
- if (errno != 0)
- {
- exit_with_error("Could not parse fruid.", argv);
- }
- if (*endptr != '\0')
- {
- // The string was not fully parsed, e.g. contains invalid characters
- exit_with_error("Invalid fruid.", argv);
- }
- if (fruid > MAX_FRU_ID)
- {
- // The string was parsed, but the set FRUID is too large.
- exit_with_error("fruid is out of range.", argv);
- }
-
- return fruid;
-}
-
//--------------------------------------------------------------------------
// This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
//--------------------------------------------------------------------------
@@ -62,30 +16,17 @@
{
int rc = 0;
uint8_t fruid;
+ std::string eeprom_file;
+ const int MAX_FRU_ID = 0xfe;
+
+ CLI::App app{"OpenBMC IPMI-FRU-Parser"};
+ app.add_option("-e,--eeprom", eeprom_file, "Absolute file name of eeprom")
+ ->check(CLI::ExistingFile);
+ app.add_option("-f,--fruid", fruid, "valid fru id in integer")
+ ->check(CLI::Range(0, MAX_FRU_ID));
// Read the arguments.
- auto cli_options = std::make_unique<ArgumentParser>(argc, argv);
-
- // Parse out each argument.
- auto eeprom_file = (*cli_options)["eeprom"];
- if (eeprom_file == ArgumentParser::empty_string)
- {
- // User has not passed in the appropriate argument value
- exit_with_error("eeprom data not found.", argv);
- }
-
- auto fruid_str = (*cli_options)["fruid"];
- if (fruid_str == ArgumentParser::empty_string)
- {
- // User has not passed in the appropriate argument value
- exit_with_error("fruid data not found.", argv);
- }
-
- // Extract the fruid
- fruid = parse_fruid_or_exit(fruid_str.c_str(), argv);
-
- // Finished getting options out, so release the parser.
- cli_options.release();
+ CLI11_PARSE(app, argc, argv);
// Now that we have the file that contains the eeprom data, go read it
// and update the Inventory DB.