1 /** 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 */ 16 #include <iostream> 17 #include <iterator> 18 #include <algorithm> 19 #include "argument.hpp" 20 21 namespace witherspoon 22 { 23 namespace power 24 { 25 26 void ArgumentParser::usage(char** argv) 27 { 28 std::cerr << "Usage: " << argv[0] << " [options]\n"; 29 std::cerr << "Options:\n"; 30 std::cerr << " --help Print this menu\n"; 31 std::cerr << " --action=<action> Action: pgood-monitor " 32 "or runtime-monitor\n"; 33 std::cerr << " --interval=<interval> Interval in seconds:\n"; 34 std::cerr << " PGOOD monitor: time allowed for PGOOD to come up\n"; 35 std::cerr << " Runtime monitor: polling interval.\n"; 36 37 std::cerr << std::flush; 38 } 39 40 const option ArgumentParser::options[] = 41 { 42 {"action", required_argument, NULL, 'a'}, 43 {"interval", required_argument, NULL, 'i'}, 44 {"help", no_argument, NULL, 'h'}, 45 {0, 0, 0, 0}, 46 }; 47 48 const char* ArgumentParser::optionStr = "a:i:h?"; 49 ArgumentParser::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 73 const 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 86 const std::string ArgumentParser::trueString = "true"; 87 const std::string ArgumentParser::emptyString = ""; 88 89 } 90 } 91