monitor: add cli11 argument parsing
Replace our custom argument parser with one from CLIUtils.
This results in a number of minor behavioral differences.
- The -? option is no longer recognized as an alias for -h/--help.
- The original parser returned EXIT_FAILURE in all cases, but CLIUtils
uses different non-zero error codes in different situations.
- Minor changes to help text descriptions:
----new----
Usage: build/phosphor-unit-failure-monitor.new [OPTIONS]
Options:
-h,--help Print this help message and exit
-s,--source TEXT REQUIRED
-t,--target TEXT REQUIRED
-a,--action ENUM:value in {start->0,stop->1} OR {0,1} REQUIRED
----old----
Usage: build/phosphor-unit-failure-monitor [options]
Options:
--help Print this menu
--source=<source> The source unit to monitor
--target=<target> The target unit to start or stop
--action=<action> Target unit action - start or stop
- The original parser displays the full help text on error, CLIUtils
does not:
----new----
$ phosphor-unit-failure-monitor
--source is required
Run with --help for more information.
----old----
$ phosphor-unit-failure-monitor
ERROR: Source not specified
Usage: build/phosphor-unit-failure-monitor [options]
Options:
--help Print this menu
--source=<source> The source unit to monitor
--target=<target> The target unit to start or stop
--action=<action> Target unit action - start or stop
Change-Id: I2417d9c857c6d8fc04807fe4729d2fa154e746a3
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/fail-monitor/main.cpp b/fail-monitor/main.cpp
index 90c6126..c383a67 100644
--- a/fail-monitor/main.cpp
+++ b/fail-monitor/main.cpp
@@ -20,53 +20,35 @@
* then it will either stop or start the target unit, depending
* on the command line arguments.
*/
-#include "argument.hpp"
#include "monitor.hpp"
-#include <iostream>
+#include <CLI/CLI.hpp>
+
#include <map>
+#include <string>
using namespace phosphor::unit::failure;
-/**
- * Prints usage and exits the program
- *
- * @param[in] err - the error message to print
- * @param[in] argv - argv from main()
- */
-void exitWithError(const char* err, char** argv)
-{
- std::cerr << "ERROR: " << err << "\n";
- ArgumentParser::usage(argv);
- exit(EXIT_FAILURE);
-}
-
static const std::map<std::string, Monitor::Action> actions = {
{"start", Monitor::Action::start}, {"stop", Monitor::Action::stop}};
int main(int argc, char** argv)
{
- ArgumentParser args(argc, argv);
+ CLI::App app;
+ std::string source;
+ std::string target;
+ Monitor::Action action{Monitor::Action::start};
- auto source = args["source"];
- if (source == ArgumentParser::emptyString)
- {
- exitWithError("Source not specified", argv);
- }
+ app.add_option("-s,--source", source, "The source unit to monitor")
+ ->required();
+ app.add_option("-t,--target", target, "The target unit to start or stop")
+ ->required();
+ app.add_option("-a,--action", action, "Target unit action - start or stop")
+ ->required()
+ ->transform(CLI::CheckedTransformer(actions, CLI::ignore_space));
- auto target = args["target"];
- if (target == ArgumentParser::emptyString)
- {
- exitWithError("Target not specified", argv);
- }
-
- auto a = actions.find(args["action"]);
- if (a == actions.end())
- {
- exitWithError("Missing or invalid action specified", argv);
- }
-
- Monitor monitor{source, target, a->second};
+ CLI11_PARSE(app, argc, argv);
+ Monitor monitor{source, target, action};
monitor.analyze();