blob: 61b1f650d6698bd8b6c2646a1cf2fd7eb987b305 [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
31const char* ArgumentParser::optionStr = "p:s:t:?h";
32const 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
41ArgumentParser::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
65const 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
78void 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