blob: 3493b147bcd406853d82799feb688bb76079e6e0 [file] [log] [blame]
/**
* 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 <iostream>
#include <cassert>
#include "argument.hpp"
namespace phosphor
{
namespace watchdog
{
using namespace std::string_literals;
const std::vector<std::string> emptyArg;
const std::string ArgumentParser::trueString = "true"s;
const char* ArgumentParser::optionStr = "p:s:t:ch";
const option ArgumentParser::options[] =
{
{ "path", required_argument, nullptr, 'p' },
{ "service", required_argument, nullptr, 's' },
{ "target", required_argument, nullptr, 't' },
{ "continue", no_argument, nullptr, 'c' },
{ "help", no_argument, nullptr, 'h' },
{ 0, 0, 0, 0},
};
ArgumentParser::ArgumentParser(int argc, char * const argv[])
{
int option;
int opt_idx = 0;
// We have to reset optind because getopt_long keeps global state
// and uses optind to track what argv index it needs to process next.
// Since this object may be instantiated more than once or test suites may
// already process instructions, optind may not be initialized to point to
// the beginning of our argv.
optind = 0;
while (-1 != (option = getopt_long(argc, argv, optionStr, options, &opt_idx)))
{
if (option == '?' || option == 'h')
{
usage(argv);
exit(-1);
}
auto i = &options[opt_idx];
while (i->val && i->val != option)
{
++i;
}
if (i->val)
{
arguments[i->name].push_back(optarg ? optarg : trueString);
}
opt_idx = 0;
}
}
const std::vector<std::string>& ArgumentParser::operator[](const std::string& opt)
{
auto i = arguments.find(opt);
if (i == arguments.end())
{
return emptyArg;
}
else
{
return i->second;
}
}
void ArgumentParser::usage(char * const argv[])
{
std::cerr << "Usage: " << argv[0] << " options\n";
std::cerr << "Options:\n";
std::cerr << " --help Print this menu\n";
std::cerr << " --path=<Dbus Object path> Dbus Object path. "
"Ex: /xyz/openbmc_project/state/watchdog/host0\n";
std::cerr << " --service=<Dbus Service name> Dbus Service name. "
"Ex: xyz.openbmc_project.State.Watchdog.Host\n";
std::cerr << " [--target=<systemd unit>] Systemd unit to be called "
"on timeout for all actions but NONE. Deprecated, use "
"--action_target instead.\n";
std::cerr << " [--action_target=<action>=<systemd unit>] Map of action to "
"systemd unit to be called on timeout if that action is "
"set for ExpireAction when the timer expires.\n";
std::cerr << " [--continue] Continue daemon after "
"watchdog timeout.\n";
}
} // namespace watchdog
} // namespace phosphor