blob: 7a60587584996964e9ba6dda509314b640e24014 [file] [log] [blame]
Matt Spinlerafb39132017-08-14 13:53:07 -05001/**
2 * Copyright © 2017 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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016#include "argument.hpp"
17
18#include <algorithm>
Matt Spinlerafb39132017-08-14 13:53:07 -050019#include <iostream>
20#include <iterator>
Matt Spinlerafb39132017-08-14 13:53:07 -050021
Lei YUab093322019-10-09 16:43:22 +080022namespace phosphor
Matt Spinlerafb39132017-08-14 13:53:07 -050023{
24namespace power
25{
26
27void ArgumentParser::usage(char** argv)
28{
29 std::cerr << "Usage: " << argv[0] << " [options]\n";
30 std::cerr << "Options:\n";
31 std::cerr << " --help Print this menu\n";
Matt Spinler70849272017-08-22 09:14:40 -050032 std::cerr << " --action=<action> Action: pgood-monitor "
33 "or runtime-monitor\n";
Matt Spinler78c5c2b2017-12-14 10:42:07 -060034 std::cerr << " --interval=<interval> Interval in milliseconds:\n";
Matt Spinler70849272017-08-22 09:14:40 -050035 std::cerr << " PGOOD monitor: time allowed for PGOOD to come up\n";
36 std::cerr << " Runtime monitor: polling interval.\n";
37
Matt Spinlerafb39132017-08-14 13:53:07 -050038 std::cerr << std::flush;
39}
40
Matt Spinlerf0f02b92018-10-25 16:12:43 -050041const option ArgumentParser::options[] = {
42 {"action", required_argument, NULL, 'a'},
Matt Spinlerafb39132017-08-14 13:53:07 -050043 {"interval", required_argument, NULL, 'i'},
Matt Spinlerf0f02b92018-10-25 16:12:43 -050044 {"help", no_argument, NULL, 'h'},
Matt Spinlerafb39132017-08-14 13:53:07 -050045 {0, 0, 0, 0},
46};
47
48const char* ArgumentParser::optionStr = "a:i:h?";
49ArgumentParser::ArgumentParser(int argc, char** argv)
50{
51 int option = 0;
52 while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
53 {
54 if ((option == '?') || (option == 'h'))
55 {
56 usage(argv);
57 exit(-1);
58 }
59
60 auto i = &options[0];
61 while ((i->val != option) && (i->val != 0))
62 {
63 ++i;
64 }
65
66 if (i->val)
67 {
68 arguments[i->name] = (i->has_arg ? optarg : trueString);
69 }
70 }
71}
72
73const std::string& ArgumentParser::operator[](const std::string& opt)
74{
75 auto i = arguments.find(opt);
76 if (i == arguments.end())
77 {
78 return emptyString;
79 }
80 else
81 {
82 return i->second;
83 }
84}
85
86const std::string ArgumentParser::trueString = "true";
87const std::string ArgumentParser::emptyString = "";
88
Matt Spinlerf0f02b92018-10-25 16:12:43 -050089} // namespace power
Lei YUab093322019-10-09 16:43:22 +080090} // namespace phosphor