xref: /openbmc/witherspoon-pfault-analysis/power-supply/argument.cpp (revision f0f02b9ae21ece3d691c1bbea348b468787ed2ff)
124e422feSBrandon Wyman /**
224e422feSBrandon Wyman  * Copyright © 2017 IBM Corporation
324e422feSBrandon Wyman  *
424e422feSBrandon Wyman  * Licensed under the Apache License, Version 2.0 (the "License");
524e422feSBrandon Wyman  * you may not use this file except in compliance with the License.
624e422feSBrandon Wyman  * You may obtain a copy of the License at
724e422feSBrandon Wyman  *
824e422feSBrandon Wyman  *     http://www.apache.org/licenses/LICENSE-2.0
924e422feSBrandon Wyman  *
1024e422feSBrandon Wyman  * Unless required by applicable law or agreed to in writing, software
1124e422feSBrandon Wyman  * distributed under the License is distributed on an "AS IS" BASIS,
1224e422feSBrandon Wyman  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1324e422feSBrandon Wyman  * See the License for the specific language governing permissions and
1424e422feSBrandon Wyman  * limitations under the License.
1524e422feSBrandon Wyman  */
16*f0f02b9aSMatt Spinler #include "argument.hpp"
17*f0f02b9aSMatt Spinler 
1824e422feSBrandon Wyman #include <algorithm>
1924e422feSBrandon Wyman #include <cassert>
20*f0f02b9aSMatt Spinler #include <iostream>
21*f0f02b9aSMatt Spinler #include <iterator>
2224e422feSBrandon Wyman 
2324e422feSBrandon Wyman namespace witherspoon
2424e422feSBrandon Wyman {
2524e422feSBrandon Wyman namespace power
2624e422feSBrandon Wyman {
2724e422feSBrandon Wyman 
ArgumentParser(int argc,char ** argv)2824e422feSBrandon Wyman ArgumentParser::ArgumentParser(int argc, char** argv)
2924e422feSBrandon Wyman {
3024e422feSBrandon Wyman     auto option = 0;
3124e422feSBrandon Wyman     while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
3224e422feSBrandon Wyman     {
3324e422feSBrandon Wyman         if ((option == '?') || (option == 'h'))
3424e422feSBrandon Wyman         {
3524e422feSBrandon Wyman             usage(argv);
3624e422feSBrandon Wyman             exit(-1);
3724e422feSBrandon Wyman         }
3824e422feSBrandon Wyman 
3924e422feSBrandon Wyman         auto i = &options[0];
4024e422feSBrandon Wyman         while ((i->val != option) && (i->val != 0))
4124e422feSBrandon Wyman         {
4224e422feSBrandon Wyman             ++i;
4324e422feSBrandon Wyman         }
4424e422feSBrandon Wyman 
4524e422feSBrandon Wyman         if (i->val)
4624e422feSBrandon Wyman         {
4724e422feSBrandon Wyman             arguments[i->name] = (i->has_arg ? optarg : trueString);
4824e422feSBrandon Wyman         }
4924e422feSBrandon Wyman     }
5024e422feSBrandon Wyman }
5124e422feSBrandon Wyman 
operator [](const std::string & opt)5224e422feSBrandon Wyman const std::string& ArgumentParser::operator[](const std::string& opt)
5324e422feSBrandon Wyman {
5424e422feSBrandon Wyman     auto i = arguments.find(opt);
5524e422feSBrandon Wyman     if (i == arguments.end())
5624e422feSBrandon Wyman     {
5724e422feSBrandon Wyman         return emptyString;
5824e422feSBrandon Wyman     }
5924e422feSBrandon Wyman     else
6024e422feSBrandon Wyman     {
6124e422feSBrandon Wyman         return i->second;
6224e422feSBrandon Wyman     }
6324e422feSBrandon Wyman }
6424e422feSBrandon Wyman 
usage(char ** argv)6524e422feSBrandon Wyman void ArgumentParser::usage(char** argv)
6624e422feSBrandon Wyman {
6724e422feSBrandon Wyman     std::cerr << "Usage: " << argv[0] << " [options]\n";
6824e422feSBrandon Wyman     std::cerr << "Options:\n";
6915b5f409SMatt Spinler     std::cerr << "    --help                              Print this menu\n";
7015b5f409SMatt Spinler     std::cerr << "    --path=<objpath>                    Path to location to"
711db9a9e2SBrandon Wyman                  " monitor\n";
7215b5f409SMatt Spinler     std::cerr << "    --instance=<instance number>        Instance number for"
7315b5f409SMatt Spinler                  " this power supply\n";
7415b5f409SMatt Spinler     std::cerr << "    --inventory=<inventory path>        Inventory path for"
7515b5f409SMatt Spinler                  " this power supply\n";
7615b5f409SMatt Spinler     std::cerr << "    --num-history-records=<num records> Number of input"
7715b5f409SMatt Spinler                  " power history records to provide on D-Bus\n";
7815b5f409SMatt Spinler     std::cerr << "    --sync-gpio-path=<path>             GPIO chip device"
7915b5f409SMatt Spinler                  " for the GPIO that performs the sync function\n";
8015b5f409SMatt Spinler     std::cerr << "    --sync-gpio-num=<path>              GPIO number for the"
8115b5f409SMatt Spinler                  " GPIO that performs the sync function\n";
8224e422feSBrandon Wyman     std::cerr << std::flush;
8324e422feSBrandon Wyman }
8424e422feSBrandon Wyman 
85*f0f02b9aSMatt Spinler const option ArgumentParser::options[] = {
8624e422feSBrandon Wyman     {"path", required_argument, NULL, 'p'},
871db9a9e2SBrandon Wyman     {"instance", required_argument, NULL, 'n'},
881db9a9e2SBrandon Wyman     {"inventory", required_argument, NULL, 'i'},
8915b5f409SMatt Spinler     {"num-history-records", required_argument, NULL, 'r'},
9015b5f409SMatt Spinler     {"sync-gpio-path", required_argument, NULL, 'a'},
9115b5f409SMatt Spinler     {"sync-gpio-num", required_argument, NULL, 'u'},
9224e422feSBrandon Wyman     {"help", no_argument, NULL, 'h'},
9324e422feSBrandon Wyman     {0, 0, 0, 0},
9424e422feSBrandon Wyman };
9524e422feSBrandon Wyman 
9615b5f409SMatt Spinler const char* ArgumentParser::optionStr = "p:n:i:r:a:u:h";
9724e422feSBrandon Wyman 
9824e422feSBrandon Wyman const std::string ArgumentParser::trueString = "true";
9924e422feSBrandon Wyman const std::string ArgumentParser::emptyString = "";
10024e422feSBrandon Wyman 
101*f0f02b9aSMatt Spinler } // namespace power
102*f0f02b9aSMatt Spinler } // namespace witherspoon
103