blob: fa0d4bce1e9b34466d12eeb619735aa7d131822c [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
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053017#include "argument.hpp"
18
Patrick Venture8f6c5152018-09-11 17:45:33 -070019#include <cassert>
20#include <iostream>
21
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053022namespace phosphor
23{
24namespace watchdog
25{
26
27using namespace std::string_literals;
28
William A. Kennington III5d307182018-01-23 22:00:55 -080029const std::vector<std::string> emptyArg;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053030const std::string ArgumentParser::trueString = "true"s;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053031
William A. Kennington III22352192018-02-27 18:51:44 -080032const char* ArgumentParser::optionStr = "p:s:t:a:f:i:ech";
Patrick Venture8f6c5152018-09-11 17:45:33 -070033// clang-format off
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053034const option ArgumentParser::options[] =
35{
William A. Kennington IIId1331082018-02-27 18:47:05 -080036 { "path", required_argument, nullptr, 'p' },
37 { "service", required_argument, nullptr, 's' },
38 { "target", required_argument, nullptr, 't' },
39 { "action_target", required_argument, nullptr, 'a' },
40 { "fallback_action", required_argument, nullptr, 'f' },
41 { "fallback_interval", required_argument, nullptr, 'i' },
William A. Kennington III22352192018-02-27 18:51:44 -080042 { "fallback_always", no_argument, nullptr, 'e' },
William A. Kennington IIId1331082018-02-27 18:47:05 -080043 { "continue", no_argument, nullptr, 'c' },
44 { "help", no_argument, nullptr, 'h' },
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053045 { 0, 0, 0, 0},
46};
Patrick Venture8f6c5152018-09-11 17:45:33 -070047// clang-format on
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053048
Patrick Venture8f6c5152018-09-11 17:45:33 -070049ArgumentParser::ArgumentParser(int argc, char* const argv[])
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053050{
William A. Kennington III5d307182018-01-23 22:00:55 -080051 int option;
52 int opt_idx = 0;
William A. Kennington III87284912018-01-23 22:07:25 -080053
54 // We have to reset optind because getopt_long keeps global state
55 // and uses optind to track what argv index it needs to process next.
56 // Since this object may be instantiated more than once or test suites may
57 // already process instructions, optind may not be initialized to point to
58 // the beginning of our argv.
59 optind = 0;
Patrick Venture8f6c5152018-09-11 17:45:33 -070060 while (-1 !=
61 (option = getopt_long(argc, argv, optionStr, options, &opt_idx)))
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053062 {
William A. Kennington III5d307182018-01-23 22:00:55 -080063 if (option == '?' || option == 'h')
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053064 {
65 usage(argv);
66 exit(-1);
67 }
68
William A. Kennington III5d307182018-01-23 22:00:55 -080069 auto i = &options[opt_idx];
70 while (i->val && i->val != option)
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053071 {
72 ++i;
73 }
74
75 if (i->val)
76 {
William A. Kennington III5d307182018-01-23 22:00:55 -080077 arguments[i->name].push_back(optarg ? optarg : trueString);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053078 }
William A. Kennington III5d307182018-01-23 22:00:55 -080079
80 opt_idx = 0;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053081 }
82}
83
Patrick Venture8f6c5152018-09-11 17:45:33 -070084const std::vector<std::string>& ArgumentParser::
85 operator[](const std::string& opt)
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053086{
87 auto i = arguments.find(opt);
88 if (i == arguments.end())
89 {
William A. Kennington III5d307182018-01-23 22:00:55 -080090 return emptyArg;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053091 }
92 else
93 {
94 return i->second;
95 }
96}
97
Patrick Venture8f6c5152018-09-11 17:45:33 -070098void ArgumentParser::usage(char* const argv[])
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053099{
100 std::cerr << "Usage: " << argv[0] << " options\n";
101 std::cerr << "Options:\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800102 std::cerr << " --help Print this menu\n";
103 std::cerr << " --path=<Dbus Object path> Dbus Object path. "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700104 "Ex: /xyz/openbmc_project/state/watchdog/host0\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800105 std::cerr << " --service=<Dbus Service name> Dbus Service "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700106 "name. Ex: xyz.openbmc_project.State.Watchdog.Host\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800107 std::cerr << " [--target=<systemd unit>] Systemd unit to "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700108 "be called on timeout for all actions but NONE. "
109 "Deprecated, use --action_target instead.\n";
William A. Kennington III180b6902018-02-22 14:29:57 -0800110 std::cerr << " [--action_target=<action>=<systemd unit>] Map of action to "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700111 "systemd unit to be called on timeout if that action is "
112 "set for ExpireAction when the timer expires.\n";
William A. Kennington IIId1331082018-02-27 18:47:05 -0800113 std::cerr << " [--fallback_action=<action>] Enables the "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700114 "watchdog even when disabled via the dbus interface. "
115 "Perform this action when the fallback expires.\n";
William A. Kennington IIId1331082018-02-27 18:47:05 -0800116 std::cerr << " [--fallback_interval=<interval in ms>] Enables the "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700117 "watchdog even when disabled via the dbus interface. "
118 "Waits for this interval before performing the fallback "
119 "action.\n";
William A. Kennington III22352192018-02-27 18:51:44 -0800120 std::cerr << " [--fallback_always] Enables the "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700121 "watchdog even when disabled by the dbus interface. "
122 "This option is only valid with a fallback specified.\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800123 std::cerr << " [--continue] Continue daemon "
Patrick Venture8f6c5152018-09-11 17:45:33 -0700124 "after watchdog timeout.\n";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530125}
126} // namespace watchdog
127} // namespace phosphor