Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 1 | /** |
| 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 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace watchdog |
| 24 | { |
| 25 | |
| 26 | using namespace std::string_literals; |
| 27 | |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 28 | const std::vector<std::string> emptyArg; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 29 | const std::string ArgumentParser::trueString = "true"s; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 30 | |
William A. Kennington III | 36a318c | 2018-01-25 22:17:11 -0800 | [diff] [blame] | 31 | const char* ArgumentParser::optionStr = "p:s:t:ch"; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 32 | const option ArgumentParser::options[] = |
| 33 | { |
William A. Kennington III | 955629d | 2018-01-25 17:33:12 -0800 | [diff] [blame] | 34 | { "path", required_argument, nullptr, 'p' }, |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 35 | { "service", required_argument, nullptr, 's' }, |
| 36 | { "target", required_argument, nullptr, 't' }, |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 37 | { "continue", no_argument, nullptr, 'c' }, |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 38 | { "help", no_argument, nullptr, 'h' }, |
| 39 | { 0, 0, 0, 0}, |
| 40 | }; |
| 41 | |
William A. Kennington III | 6c094a2 | 2018-01-29 12:09:29 -0800 | [diff] [blame] | 42 | ArgumentParser::ArgumentParser(int argc, char * const argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 43 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 44 | int option; |
| 45 | int opt_idx = 0; |
William A. Kennington III | 8728491 | 2018-01-23 22:07:25 -0800 | [diff] [blame] | 46 | |
| 47 | // We have to reset optind because getopt_long keeps global state |
| 48 | // and uses optind to track what argv index it needs to process next. |
| 49 | // Since this object may be instantiated more than once or test suites may |
| 50 | // already process instructions, optind may not be initialized to point to |
| 51 | // the beginning of our argv. |
| 52 | optind = 0; |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 53 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, &opt_idx))) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 54 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 55 | if (option == '?' || option == 'h') |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 56 | { |
| 57 | usage(argv); |
| 58 | exit(-1); |
| 59 | } |
| 60 | |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 61 | auto i = &options[opt_idx]; |
| 62 | while (i->val && i->val != option) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 63 | { |
| 64 | ++i; |
| 65 | } |
| 66 | |
| 67 | if (i->val) |
| 68 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 69 | arguments[i->name].push_back(optarg ? optarg : trueString); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 70 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 71 | |
| 72 | opt_idx = 0; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 76 | const std::vector<std::string>& ArgumentParser::operator[](const std::string& opt) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 77 | { |
| 78 | auto i = arguments.find(opt); |
| 79 | if (i == arguments.end()) |
| 80 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 81 | return emptyArg; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
| 85 | return i->second; |
| 86 | } |
| 87 | } |
| 88 | |
William A. Kennington III | 6c094a2 | 2018-01-29 12:09:29 -0800 | [diff] [blame] | 89 | void ArgumentParser::usage(char * const argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 90 | { |
| 91 | std::cerr << "Usage: " << argv[0] << " options\n"; |
| 92 | std::cerr << "Options:\n"; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 93 | std::cerr << " --help Print this menu\n"; |
William A. Kennington III | 180b690 | 2018-02-22 14:29:57 -0800 | [diff] [blame^] | 94 | std::cerr << " --path=<Dbus Object path> Dbus Object path. " |
| 95 | "Ex: /xyz/openbmc_project/state/watchdog/host0\n"; |
| 96 | std::cerr << " --service=<Dbus Service name> Dbus Service name. " |
| 97 | "Ex: xyz.openbmc_project.State.Watchdog.Host\n"; |
| 98 | std::cerr << " [--target=<systemd unit>] Systemd unit to be called " |
| 99 | "on timeout for all actions but NONE. Deprecated, use " |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 100 | "--action_target instead.\n"; |
William A. Kennington III | 180b690 | 2018-02-22 14:29:57 -0800 | [diff] [blame^] | 101 | std::cerr << " [--action_target=<action>=<systemd unit>] Map of action to " |
| 102 | "systemd unit to be called on timeout if that action is " |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 103 | "set for ExpireAction when the timer expires.\n"; |
William A. Kennington III | 180b690 | 2018-02-22 14:29:57 -0800 | [diff] [blame^] | 104 | std::cerr << " [--continue] Continue daemon after " |
| 105 | "watchdog timeout.\n"; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 106 | } |
| 107 | } // namespace watchdog |
| 108 | } // namespace phosphor |