xref: /openbmc/witherspoon-pfault-analysis/power-sequencer/argument.cpp (revision f0f02b9ae21ece3d691c1bbea348b468787ed2ff)
1afb39132SMatt Spinler /**
2afb39132SMatt Spinler  * Copyright © 2017 IBM Corporation
3afb39132SMatt Spinler  *
4afb39132SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5afb39132SMatt Spinler  * you may not use this file except in compliance with the License.
6afb39132SMatt Spinler  * You may obtain a copy of the License at
7afb39132SMatt Spinler  *
8afb39132SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9afb39132SMatt Spinler  *
10afb39132SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11afb39132SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12afb39132SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afb39132SMatt Spinler  * See the License for the specific language governing permissions and
14afb39132SMatt Spinler  * limitations under the License.
15afb39132SMatt Spinler  */
16*f0f02b9aSMatt Spinler #include "argument.hpp"
17*f0f02b9aSMatt Spinler 
18*f0f02b9aSMatt Spinler #include <algorithm>
19afb39132SMatt Spinler #include <iostream>
20afb39132SMatt Spinler #include <iterator>
21afb39132SMatt Spinler 
22afb39132SMatt Spinler namespace witherspoon
23afb39132SMatt Spinler {
24afb39132SMatt Spinler namespace power
25afb39132SMatt Spinler {
26afb39132SMatt Spinler 
usage(char ** argv)27afb39132SMatt Spinler void ArgumentParser::usage(char** argv)
28afb39132SMatt Spinler {
29afb39132SMatt Spinler     std::cerr << "Usage: " << argv[0] << " [options]\n";
30afb39132SMatt Spinler     std::cerr << "Options:\n";
31afb39132SMatt Spinler     std::cerr << "    --help                Print this menu\n";
327084927eSMatt Spinler     std::cerr << "    --action=<action>     Action: pgood-monitor "
337084927eSMatt Spinler                  "or runtime-monitor\n";
3478c5c2b0SMatt Spinler     std::cerr << "    --interval=<interval> Interval in milliseconds:\n";
357084927eSMatt Spinler     std::cerr << "      PGOOD monitor:   time allowed for PGOOD to come up\n";
367084927eSMatt Spinler     std::cerr << "      Runtime monitor: polling interval.\n";
377084927eSMatt Spinler 
38afb39132SMatt Spinler     std::cerr << std::flush;
39afb39132SMatt Spinler }
40afb39132SMatt Spinler 
41*f0f02b9aSMatt Spinler const option ArgumentParser::options[] = {
42afb39132SMatt Spinler     {"action", required_argument, NULL, 'a'},
43afb39132SMatt Spinler     {"interval", required_argument, NULL, 'i'},
44afb39132SMatt Spinler     {"help", no_argument, NULL, 'h'},
45afb39132SMatt Spinler     {0, 0, 0, 0},
46afb39132SMatt Spinler };
47afb39132SMatt Spinler 
48afb39132SMatt Spinler const char* ArgumentParser::optionStr = "a:i:h?";
ArgumentParser(int argc,char ** argv)49afb39132SMatt Spinler ArgumentParser::ArgumentParser(int argc, char** argv)
50afb39132SMatt Spinler {
51afb39132SMatt Spinler     int option = 0;
52afb39132SMatt Spinler     while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
53afb39132SMatt Spinler     {
54afb39132SMatt Spinler         if ((option == '?') || (option == 'h'))
55afb39132SMatt Spinler         {
56afb39132SMatt Spinler             usage(argv);
57afb39132SMatt Spinler             exit(-1);
58afb39132SMatt Spinler         }
59afb39132SMatt Spinler 
60afb39132SMatt Spinler         auto i = &options[0];
61afb39132SMatt Spinler         while ((i->val != option) && (i->val != 0))
62afb39132SMatt Spinler         {
63afb39132SMatt Spinler             ++i;
64afb39132SMatt Spinler         }
65afb39132SMatt Spinler 
66afb39132SMatt Spinler         if (i->val)
67afb39132SMatt Spinler         {
68afb39132SMatt Spinler             arguments[i->name] = (i->has_arg ? optarg : trueString);
69afb39132SMatt Spinler         }
70afb39132SMatt Spinler     }
71afb39132SMatt Spinler }
72afb39132SMatt Spinler 
operator [](const std::string & opt)73afb39132SMatt Spinler const std::string& ArgumentParser::operator[](const std::string& opt)
74afb39132SMatt Spinler {
75afb39132SMatt Spinler     auto i = arguments.find(opt);
76afb39132SMatt Spinler     if (i == arguments.end())
77afb39132SMatt Spinler     {
78afb39132SMatt Spinler         return emptyString;
79afb39132SMatt Spinler     }
80afb39132SMatt Spinler     else
81afb39132SMatt Spinler     {
82afb39132SMatt Spinler         return i->second;
83afb39132SMatt Spinler     }
84afb39132SMatt Spinler }
85afb39132SMatt Spinler 
86afb39132SMatt Spinler const std::string ArgumentParser::trueString = "true";
87afb39132SMatt Spinler const std::string ArgumentParser::emptyString = "";
88afb39132SMatt Spinler 
89*f0f02b9aSMatt Spinler } // namespace power
90*f0f02b9aSMatt Spinler } // namespace witherspoon
91