blob: 11d981442421a5dd97ddb0b1eeec6de0e6e7d917 [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
William A. Kennington III36a318c2018-01-25 22:17:11 -080031const char* ArgumentParser::optionStr = "p:s:t:ch";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053032const option ArgumentParser::options[] =
33{
William A. Kennington III955629d2018-01-25 17:33:12 -080034 { "path", required_argument, nullptr, 'p' },
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053035 { "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;
William A. Kennington III87284912018-01-23 22:07:25 -080045
46 // We have to reset optind because getopt_long keeps global state
47 // and uses optind to track what argv index it needs to process next.
48 // Since this object may be instantiated more than once or test suites may
49 // already process instructions, optind may not be initialized to point to
50 // the beginning of our argv.
51 optind = 0;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053052 while (-1 != (option = getopt_long(argc, argv, optionStr, options, nullptr)))
53 {
54 if ((option == '?') || (option == 'h'))
55 {
56 usage(argv);
57 exit(-1);
58 }
59
60 auto i = &options[0];
61 while ((i->val != option) && (i->val != 0))
62 {
63 ++i;
64 }
65
66 if (i->val)
67 {
68 arguments[i->name] = (i->has_arg ? optarg : trueString);
69 }
70 }
71}
72
73const std::string& ArgumentParser::operator[](const std::string& opt)
74{
75 auto i = arguments.find(opt);
76 if (i == arguments.end())
77 {
78 return emptyString;
79 }
80 else
81 {
82 return i->second;
83 }
84}
85
86void ArgumentParser::usage(char** argv)
87{
88 std::cerr << "Usage: " << argv[0] << " options\n";
89 std::cerr << "Options:\n";
90 std::cerr << " --help Print this menu\n";
91 std::cerr << " --path=<Dbus Object path> Dbus Object path."
92 " Ex: /xyz/openbmc_project/"
93 "state/watchdog/host0\n";
94 std::cerr << " --service=<Dbus Service name> Dbus Service name."
95 " Ex: xyz.openbmc_project."
96 "State.Watchdog.Host\n";
97 std::cerr << " [--target=<systemd unit>] Systemd unit to be called on"
98 " timeout\n";
Patrick Venture09eebe32017-08-11 15:23:17 -070099 std::cerr << " [--continue] Continue daemon after"
100 " watchdog timeout.\n";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530101}
102} // namespace watchdog
103} // namespace phosphor