blob: 41ea9aed461ed277dd87095cebfac4a59e7fe13f [file] [log] [blame]
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05301/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
18#include <cassert>
19#include "argument.hpp"
20
21namespace phosphor
22{
23namespace watchdog
24{
25
26using namespace std::string_literals;
27
28const std::string ArgumentParser::trueString = "true"s;
29const std::string ArgumentParser::emptyString = ""s;
30
Patrick Venture09eebe32017-08-11 15:23:17 -070031const char* ArgumentParser::optionStr = "p:s:t:c:?h";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053032const option ArgumentParser::options[] =
33{
34 { "path", required_argument, nullptr, 'o' },
35 { "service", required_argument, nullptr, 's' },
36 { "target", required_argument, nullptr, 't' },
Patrick Venture09eebe32017-08-11 15:23:17 -070037 { "continue", no_argument, nullptr, 'c' },
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053038 { "help", no_argument, nullptr, 'h' },
39 { 0, 0, 0, 0},
40};
41
42ArgumentParser::ArgumentParser(int argc, char** argv)
43{
44 int option = 0;
45 while (-1 != (option = getopt_long(argc, argv, optionStr, options, nullptr)))
46 {
47 if ((option == '?') || (option == 'h'))
48 {
49 usage(argv);
50 exit(-1);
51 }
52
53 auto i = &options[0];
54 while ((i->val != option) && (i->val != 0))
55 {
56 ++i;
57 }
58
59 if (i->val)
60 {
61 arguments[i->name] = (i->has_arg ? optarg : trueString);
62 }
63 }
64}
65
66const std::string& ArgumentParser::operator[](const std::string& opt)
67{
68 auto i = arguments.find(opt);
69 if (i == arguments.end())
70 {
71 return emptyString;
72 }
73 else
74 {
75 return i->second;
76 }
77}
78
79void ArgumentParser::usage(char** argv)
80{
81 std::cerr << "Usage: " << argv[0] << " options\n";
82 std::cerr << "Options:\n";
83 std::cerr << " --help Print this menu\n";
84 std::cerr << " --path=<Dbus Object path> Dbus Object path."
85 " Ex: /xyz/openbmc_project/"
86 "state/watchdog/host0\n";
87 std::cerr << " --service=<Dbus Service name> Dbus Service name."
88 " Ex: xyz.openbmc_project."
89 "State.Watchdog.Host\n";
90 std::cerr << " [--target=<systemd unit>] Systemd unit to be called on"
91 " timeout\n";
Patrick Venture09eebe32017-08-11 15:23:17 -070092 std::cerr << " [--continue] Continue daemon after"
93 " watchdog timeout.\n";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053094}
95} // namespace watchdog
96} // namespace phosphor