power-supply: Replace Argument class with CLI11
Phosphor-power already depends on cli11, delete Argument class and
use cli11 instead.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I4caebf3f9fcb432fd1a80535b9b067aab66b1b71
diff --git a/power-supply/argument.cpp b/power-supply/argument.cpp
deleted file mode 100644
index 7b671d8..0000000
--- a/power-supply/argument.cpp
+++ /dev/null
@@ -1,102 +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 <cassert>
-#include <iostream>
-#include <iterator>
-
-namespace phosphor
-{
-namespace power
-{
-
-ArgumentParser::ArgumentParser(int argc, char** argv)
-{
- auto 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 << " --path=<objpath> Path to location to"
- " monitor\n";
- std::cerr << " --instance=<instance number> Instance number for"
- " this power supply\n";
- std::cerr << " --inventory=<inventory path> Inventory path for"
- " this power supply\n";
- std::cerr << " --num-history-records=<num records> Number of input"
- " power history records to provide on D-Bus\n";
- std::cerr << " --sync-gpio-path=<path> GPIO chip device"
- " for the GPIO that performs the sync function\n";
- std::cerr << " --sync-gpio-num=<path> GPIO number for the"
- " GPIO that performs the sync function\n";
- std::cerr << std::flush;
-}
-
-const option ArgumentParser::options[] = {
- {"path", required_argument, NULL, 'p'},
- {"instance", required_argument, NULL, 'n'},
- {"inventory", required_argument, NULL, 'i'},
- {"num-history-records", required_argument, NULL, 'r'},
- {"sync-gpio-path", required_argument, NULL, 'a'},
- {"sync-gpio-num", required_argument, NULL, 'u'},
- {"help", no_argument, NULL, 'h'},
- {0, 0, 0, 0},
-};
-
-const char* ArgumentParser::optionStr = "p:n:i:r:a:u:h";
-
-const std::string ArgumentParser::trueString = "true";
-const std::string ArgumentParser::emptyString = "";
-
-} // namespace power
-} // namespace phosphor
diff --git a/power-supply/main.cpp b/power-supply/main.cpp
index a0be28c..d441dbe 100644
--- a/power-supply/main.cpp
+++ b/power-supply/main.cpp
@@ -15,10 +15,10 @@
*/
#include "config.h"
-#include "argument.hpp"
#include "device_monitor.hpp"
#include "power_supply.hpp"
+#include <CLI/CLI.hpp>
#include <phosphor-logging/log.hpp>
#include <sdeventplus/event.hpp>
@@ -29,34 +29,42 @@
int main(int argc, char* argv[])
{
- auto options = ArgumentParser(argc, argv);
+ CLI::App app{"PSU Monitor"};
- auto objpath = (options)["path"];
- auto instnum = (options)["instance"];
- auto invpath = (options)["inventory"];
- if (argc < 4)
+ std::string objpath{};
+ std::string instnum{};
+ std::string invpath{};
+ std::string records{};
+ std::string syncGPIOPath{};
+ std::string syncGPIONum{};
+
+ app.add_option("-p,--path", objpath, "Path to location to monitor\n")
+ ->required();
+ app.add_option("-n,--instance", instnum,
+ "Instance number for this power supply\n")
+ ->required();
+ app.add_option("-i,--inventory", invpath,
+ "Inventory path for this power supply\n")
+ ->required();
+ app.add_option(
+ "-r,--num-history-records", records,
+ "Number of input power history records to provide on D-Bus\n")
+ ->expected(0, 1);
+ app.add_option(
+ "-a,--sync-gpio-path", syncGPIOPath,
+ "GPIO chip device for the GPIO that performs the sync function\n")
+ ->expected(0, 1);
+ app.add_option("-u,--sync-gpio-num", syncGPIONum,
+ "GPIO number for the GPIO that performs the sync function\n")
+ ->expected(0, 1);
+
+ try
{
- std::cerr << std::endl << "Too few arguments" << std::endl;
- options.usage(argv);
- return -1;
+ app.parse(argc, argv);
}
-
- if (objpath == ArgumentParser::emptyString)
+ catch (const CLI::Error& e)
{
- log<level::ERR>("Device monitoring path argument required");
- return -2;
- }
-
- if (instnum == ArgumentParser::emptyString)
- {
- log<level::ERR>("Device monitoring instance number argument required");
- return -3;
- }
-
- if (invpath == ArgumentParser::emptyString)
- {
- log<level::ERR>("Device monitoring inventory path argument required");
- return -4;
+ return app.exit(e);
}
auto bus = sdbusplus::bus::new_default();
@@ -87,8 +95,7 @@
// Get the number of input power history records to keep in D-Bus.
long int numRecords = 0;
- auto records = (options)["num-history-records"];
- if (records != ArgumentParser::emptyString)
+ if (!records.empty())
{
numRecords = std::stol(records);
if (numRecords < 0)
@@ -102,20 +109,15 @@
{
// Get the GPIO information for controlling the SYNC signal.
// If one is there, they both must be.
- auto syncGPIOPath = (options)["sync-gpio-path"];
- auto syncGPIONum = (options)["sync-gpio-num"];
-
- if (((syncGPIOPath == ArgumentParser::emptyString) &&
- (syncGPIONum != ArgumentParser::emptyString)) ||
- ((syncGPIOPath != ArgumentParser::emptyString) &&
- (syncGPIONum == ArgumentParser::emptyString)))
+ if ((syncGPIOPath.empty() && !syncGPIONum.empty()) ||
+ (!syncGPIOPath.empty() && syncGPIONum.empty()))
{
std::cerr << "Invalid sync GPIO number or path\n";
return -7;
}
size_t gpioNum = 0;
- if (syncGPIONum != ArgumentParser::emptyString)
+ if (!syncGPIONum.empty())
{
gpioNum = stoul(syncGPIONum);
}
diff --git a/power-supply/meson.build b/power-supply/meson.build
index 4bf1626..20fec89 100644
--- a/power-supply/meson.build
+++ b/power-supply/meson.build
@@ -1,6 +1,5 @@
psu_monitor = executable(
'psu-monitor',
- 'argument.cpp',
error_hpp,
'main.cpp',
'power_supply.cpp',