power-sequencer: Replace Argument class with CLI11
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I8f48a1fb18ed2c190c8292317e883b05754a5558
diff --git a/argument.hpp b/argument.hpp
deleted file mode 100644
index 1fad60a..0000000
--- a/argument.hpp
+++ /dev/null
@@ -1,66 +0,0 @@
-#pragma once
-
-#include <getopt.h>
-
-#include <map>
-#include <string>
-
-namespace phosphor
-{
-namespace power
-{
-
-/** @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 Constructs Argument object
- *
- * @param[in] argc - the main function's argc passed as is
- * @param[in] 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[in] opt - command line option string
- *
- * @return argument which is a standard optarg
- */
- const std::string& operator[](const std::string& opt);
-
- /** @brief Displays usage
- *
- * @param[in] 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 power
-} // namespace phosphor
diff --git a/power-sequencer/argument.cpp b/power-sequencer/argument.cpp
deleted file mode 100644
index 7a60587..0000000
--- a/power-sequencer/argument.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/**
- * Copyright © 2017 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 <iostream>
-#include <iterator>
-
-namespace phosphor
-{
-namespace power
-{
-
-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 << " --action=<action> Action: pgood-monitor "
- "or runtime-monitor\n";
- std::cerr << " --interval=<interval> Interval in milliseconds:\n";
- std::cerr << " PGOOD monitor: time allowed for PGOOD to come up\n";
- std::cerr << " Runtime monitor: polling interval.\n";
-
- std::cerr << std::flush;
-}
-
-const option ArgumentParser::options[] = {
- {"action", required_argument, NULL, 'a'},
- {"interval", required_argument, NULL, 'i'},
- {"help", no_argument, NULL, 'h'},
- {0, 0, 0, 0},
-};
-
-const char* ArgumentParser::optionStr = "a:i:h?";
-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;
- }
-}
-
-const std::string ArgumentParser::trueString = "true";
-const std::string ArgumentParser::emptyString = "";
-
-} // namespace power
-} // namespace phosphor
diff --git a/power-sequencer/main.cpp b/power-sequencer/main.cpp
index 72f96fb..6f1dca0 100644
--- a/power-sequencer/main.cpp
+++ b/power-sequencer/main.cpp
@@ -15,12 +15,12 @@
*/
#include "config.h"
-#include "argument.hpp"
#include "mihawk-cpld.hpp"
#include "pgood_monitor.hpp"
#include "runtime_monitor.hpp"
#include "ucd90160.hpp"
+#include <CLI/CLI.hpp>
#include <phosphor-logging/log.hpp>
#include <sdeventplus/event.hpp>
@@ -32,17 +32,31 @@
int main(int argc, char** argv)
{
- ArgumentParser args{argc, argv};
- auto action = args["action"];
+ CLI::App app{"Phosphor sequencer monitor"};
+ std::string action{};
+ std::string interVal{};
- if ((action != "pgood-monitor") && (action != "runtime-monitor"))
+ std::vector<std::string> actionTypes = {"pgood-monitor", "runtime-monitor"};
+ app.add_option("-a,--action", action,
+ "Action: pgood-monitor or runtime-monitor\n")
+ ->required()
+ ->transform(CLI::IsMember(actionTypes));
+ app.add_option("-i,--interval", interVal,
+ "Interval in milliseconds:\n"
+ "PGOOD monitor: time allowed for PGOOD to come up\n"
+ "Runtime monitor: polling interval.\n")
+ ->required();
+
+ try
{
- std::cerr << "Invalid action\n";
- args.usage(argv);
- exit(EXIT_FAILURE);
+ app.parse(argc, argv);
+ }
+ catch (CLI::Error& e)
+ {
+ return app.exit(e);
}
- auto i = strtoul(args["interval"].c_str(), nullptr, 10);
+ auto i = strtoul(interVal.c_str(), nullptr, 10);
if (i == 0)
{
std::cerr << "Invalid interval value\n";
diff --git a/power-sequencer/meson.build b/power-sequencer/meson.build
index ea72127..b38cae5 100644
--- a/power-sequencer/meson.build
+++ b/power-sequencer/meson.build
@@ -23,7 +23,6 @@
executable(
'pseq-monitor',
- 'argument.cpp',
error_hpp,
'main.cpp',
'pgood_monitor.cpp',