diff options
author | Patrick Venture <venture@google.com> | 2019-03-11 14:14:30 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2019-03-21 17:40:53 +0000 |
commit | 17cfad6ce0a217697703d0b9c40bdc3ebd3f1b43 (patch) | |
tree | 9ff9964821288be830c86e9295a3eea2c2d95c6d | |
parent | 69a68eddc25381ca6bee24ab668ba67a975a1954 (diff) | |
download | phosphor-hwmon-17cfad6ce0a217697703d0b9c40bdc3ebd3f1b43.tar.gz phosphor-hwmon-17cfad6ce0a217697703d0b9c40bdc3ebd3f1b43.zip |
swap out getopts for cli11
Swap out getopt for cli11 for parsing parameters.
Tested: Verified on quanta-q71l-variant that phosphor-hwmon started as
before.
Change-Id: Ic0137432afd254b77133842b5d73a5bab1e415aa
Signed-off-by: Patrick Venture <venture@google.com>
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | argument.cpp | 84 | ||||
-rw-r--r-- | argument.hpp | 29 | ||||
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | readd.cpp | 35 |
5 files changed, 26 insertions, 129 deletions
diff --git a/Makefile.am b/Makefile.am index c01f490..b4577f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,7 +57,6 @@ libhwmon_la_CXXFLAGS = \ $(CODE_COVERAGE_CXXFLAGS) libhwmon_la_SOURCES = \ - argument.cpp \ sensorset.cpp \ mainloop.cpp \ sysfs.cpp \ diff --git a/argument.cpp b/argument.cpp deleted file mode 100644 index 99ee710..0000000 --- a/argument.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright © 2016 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 "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]\n"; - std::cerr << "Options:\n"; - std::cerr << " --help print this menu\n"; - std::cerr << " --path=<path> sysfs location to monitor\n"; - std::cerr << " --dev-path=<path> device path to monitor\n"; - std::cerr << std::flush; -} - -// clang-format off -const option ArgumentParser::options[] = { - {"path", required_argument, NULL, 'p'}, - {"dev-path", required_argument, NULL, 'o'}, - {"help", no_argument, NULL, 'h'}, - {0, 0, 0, 0}, -}; -// clang-format on - -const char* ArgumentParser::optionstr = "o:p:?h"; - -const std::string ArgumentParser::true_string = "true"; -const std::string ArgumentParser::empty_string = ""; - -// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/argument.hpp b/argument.hpp deleted file mode 100644 index e6acaf3..0000000 --- a/argument.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#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(){}; -}; - -// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/configure.ac b/configure.ac index b203944..dea63da 100644 --- a/configure.ac +++ b/configure.ac @@ -31,6 +31,12 @@ PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus]) PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces]) PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging]) PKG_CHECK_MODULES([GPIOPLUS], [gpioplus]) +# We need the header only CLI library +AC_CHECK_HEADERS( + [CLI/CLI.hpp], + [], + [AC_MSG_ERROR([Could not find CLI11 CLI/CLI.hpp])] +) AX_PTHREAD([], [AC_MSG_ERROR(["pthread required and not found"])]) # Checks for typedefs, structures, and compiler characteristics. @@ -15,17 +15,16 @@ */ #include "config.h" -#include "argument.hpp" #include "mainloop.hpp" #include "sysfs.hpp" +#include <CLI/CLI.hpp> #include <iostream> #include <memory> -static void exit_with_error(const char* err, char** argv) +static void exit_with_error(const std::string& help, const char* err) { - ArgumentParser::usage(argv); - std::cerr << std::endl; + std::cerr << help << std::endl; std::cerr << "ERROR: " << err << std::endl; exit(-1); } @@ -33,12 +32,19 @@ static void exit_with_error(const char* err, char** argv) int main(int argc, char** argv) { // Read arguments. - auto options = std::make_unique<ArgumentParser>(argc, argv); + std::string syspath = ""; + std::string devpath = ""; + + CLI::App app{"OpenBMC Hwmon Daemon"}; + app.add_option("-p,--path", syspath, "sysfs location to monitor"); + app.add_option("-o,--dev-path", devpath, "device path to monitor"); + + CLI11_PARSE(app, argc, argv); // Parse out path argument. - auto path = (*options)["dev-path"]; + auto path = devpath; auto param = path; - if (path != ArgumentParser::empty_string) + if (!path.empty()) { // This path may either be a device path (starts with // /devices), or an open firmware device tree path. @@ -52,25 +58,24 @@ int main(int argc, char** argv) } } - if (path == ArgumentParser::empty_string) + if (path.empty()) { - path = (*options)["path"]; + path = syspath; param = path; } - if (path == ArgumentParser::empty_string) + if (path.empty()) { - exit_with_error("Path not specified or invalid.", argv); + exit_with_error(app.help("", CLI::AppFormatMode::All), + "Path not specified or invalid."); } - // Finished getting options out, so cleanup the parser. - options.reset(); - // Determine the physical device sysfs path. auto calloutPath = sysfs::findCalloutPath(path); if (calloutPath.empty()) { - exit_with_error("Unable to determine callout path.", argv); + exit_with_error(app.help("", CLI::AppFormatMode::All), + "Unable to determine callout path."); } MainLoop loop(sdbusplus::bus::new_default(), param, path, calloutPath, |