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 | |
| 28 | const std::string ArgumentParser::trueString = "true"s; |
| 29 | const std::string ArgumentParser::emptyString = ""s; |
| 30 | |
| 31 | const char* ArgumentParser::optionStr = "p:s:t:?h"; |
| 32 | const option ArgumentParser::options[] = |
| 33 | { |
| 34 | { "path", required_argument, nullptr, 'o' }, |
| 35 | { "service", required_argument, nullptr, 's' }, |
| 36 | { "target", required_argument, nullptr, 't' }, |
| 37 | { "help", no_argument, nullptr, 'h' }, |
| 38 | { 0, 0, 0, 0}, |
| 39 | }; |
| 40 | |
| 41 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 42 | { |
| 43 | int option = 0; |
| 44 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, nullptr))) |
| 45 | { |
| 46 | if ((option == '?') || (option == 'h')) |
| 47 | { |
| 48 | usage(argv); |
| 49 | exit(-1); |
| 50 | } |
| 51 | |
| 52 | auto i = &options[0]; |
| 53 | while ((i->val != option) && (i->val != 0)) |
| 54 | { |
| 55 | ++i; |
| 56 | } |
| 57 | |
| 58 | if (i->val) |
| 59 | { |
| 60 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 66 | { |
| 67 | auto i = arguments.find(opt); |
| 68 | if (i == arguments.end()) |
| 69 | { |
| 70 | return emptyString; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | return i->second; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void ArgumentParser::usage(char** argv) |
| 79 | { |
| 80 | std::cerr << "Usage: " << argv[0] << " options\n"; |
| 81 | std::cerr << "Options:\n"; |
| 82 | std::cerr << " --help Print this menu\n"; |
| 83 | std::cerr << " --path=<Dbus Object path> Dbus Object path." |
| 84 | " Ex: /xyz/openbmc_project/" |
| 85 | "state/watchdog/host0\n"; |
| 86 | std::cerr << " --service=<Dbus Service name> Dbus Service name." |
| 87 | " Ex: xyz.openbmc_project." |
| 88 | "State.Watchdog.Host\n"; |
| 89 | std::cerr << " [--target=<systemd unit>] Systemd unit to be called on" |
| 90 | " timeout\n"; |
| 91 | } |
| 92 | } // namespace watchdog |
| 93 | } // namespace phosphor |