blob: 770db87a65466d91576552651e47ccdc06d732d9 [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
William A. Kennington III5d307182018-01-23 22:00:55 -080028const std::vector<std::string> emptyArg;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053029const std::string ArgumentParser::trueString = "true"s;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053030
William A. Kennington III22352192018-02-27 18:51:44 -080031const char* ArgumentParser::optionStr = "p:s:t:a:f:i:ech";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053032const option ArgumentParser::options[] =
33{
William A. Kennington IIId1331082018-02-27 18:47:05 -080034 { "path", required_argument, nullptr, 'p' },
35 { "service", required_argument, nullptr, 's' },
36 { "target", required_argument, nullptr, 't' },
37 { "action_target", required_argument, nullptr, 'a' },
38 { "fallback_action", required_argument, nullptr, 'f' },
39 { "fallback_interval", required_argument, nullptr, 'i' },
William A. Kennington III22352192018-02-27 18:51:44 -080040 { "fallback_always", no_argument, nullptr, 'e' },
William A. Kennington IIId1331082018-02-27 18:47:05 -080041 { "continue", no_argument, nullptr, 'c' },
42 { "help", no_argument, nullptr, 'h' },
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053043 { 0, 0, 0, 0},
44};
45
William A. Kennington III6c094a22018-01-29 12:09:29 -080046ArgumentParser::ArgumentParser(int argc, char * const argv[])
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053047{
William A. Kennington III5d307182018-01-23 22:00:55 -080048 int option;
49 int opt_idx = 0;
William A. Kennington III87284912018-01-23 22:07:25 -080050
51 // We have to reset optind because getopt_long keeps global state
52 // and uses optind to track what argv index it needs to process next.
53 // Since this object may be instantiated more than once or test suites may
54 // already process instructions, optind may not be initialized to point to
55 // the beginning of our argv.
56 optind = 0;
William A. Kennington III5d307182018-01-23 22:00:55 -080057 while (-1 != (option = getopt_long(argc, argv, optionStr, options, &opt_idx)))
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053058 {
William A. Kennington III5d307182018-01-23 22:00:55 -080059 if (option == '?' || option == 'h')
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053060 {
61 usage(argv);
62 exit(-1);
63 }
64
William A. Kennington III5d307182018-01-23 22:00:55 -080065 auto i = &options[opt_idx];
66 while (i->val && i->val != option)
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053067 {
68 ++i;
69 }
70
71 if (i->val)
72 {
William A. Kennington III5d307182018-01-23 22:00:55 -080073 arguments[i->name].push_back(optarg ? optarg : trueString);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053074 }
William A. Kennington III5d307182018-01-23 22:00:55 -080075
76 opt_idx = 0;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053077 }
78}
79
William A. Kennington III5d307182018-01-23 22:00:55 -080080const std::vector<std::string>& ArgumentParser::operator[](const std::string& opt)
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053081{
82 auto i = arguments.find(opt);
83 if (i == arguments.end())
84 {
William A. Kennington III5d307182018-01-23 22:00:55 -080085 return emptyArg;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053086 }
87 else
88 {
89 return i->second;
90 }
91}
92
William A. Kennington III6c094a22018-01-29 12:09:29 -080093void ArgumentParser::usage(char * const argv[])
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053094{
95 std::cerr << "Usage: " << argv[0] << " options\n";
96 std::cerr << "Options:\n";
William A. Kennington III12926432018-02-25 19:54:59 -080097 std::cerr << " --help Print this menu\n";
98 std::cerr << " --path=<Dbus Object path> Dbus Object path. "
William A. Kennington III180b6902018-02-22 14:29:57 -080099 "Ex: /xyz/openbmc_project/state/watchdog/host0\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800100 std::cerr << " --service=<Dbus Service name> Dbus Service "
101 "name. Ex: xyz.openbmc_project.State.Watchdog.Host\n";
102 std::cerr << " [--target=<systemd unit>] Systemd unit to "
103 "be called on timeout for all actions but NONE. "
104 "Deprecated, use --action_target instead.\n";
William A. Kennington III180b6902018-02-22 14:29:57 -0800105 std::cerr << " [--action_target=<action>=<systemd unit>] Map of action to "
106 "systemd unit to be called on timeout if that action is "
William A. Kennington III27df4b52018-02-02 16:02:05 -0800107 "set for ExpireAction when the timer expires.\n";
William A. Kennington IIId1331082018-02-27 18:47:05 -0800108 std::cerr << " [--fallback_action=<action>] Enables the "
109 "watchdog even when disabled via the dbus interface. "
110 "Perform this action when the fallback expires.\n";
111 std::cerr << " [--fallback_interval=<interval in ms>] Enables the "
112 "watchdog even when disabled via the dbus interface. "
113 "Waits for this interval before performing the fallback "
114 "action.\n";
William A. Kennington III22352192018-02-27 18:51:44 -0800115 std::cerr << " [--fallback_always] Enables the "
116 "watchdog even when disabled by the dbus interface. "
117 "This option is only valid with a fallback specified.\n";
William A. Kennington III12926432018-02-25 19:54:59 -0800118 std::cerr << " [--continue] Continue daemon "
119 "after watchdog timeout.\n";
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530120}
121} // namespace watchdog
122} // namespace phosphor