Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 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 <cassert> |
| 20 | #include "argument.hpp" |
| 21 | |
| 22 | namespace witherspoon |
| 23 | { |
| 24 | namespace power |
| 25 | { |
| 26 | |
| 27 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 28 | { |
| 29 | auto option = 0; |
| 30 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL))) |
| 31 | { |
| 32 | if ((option == '?') || (option == 'h')) |
| 33 | { |
| 34 | usage(argv); |
| 35 | exit(-1); |
| 36 | } |
| 37 | |
| 38 | auto i = &options[0]; |
| 39 | while ((i->val != option) && (i->val != 0)) |
| 40 | { |
| 41 | ++i; |
| 42 | } |
| 43 | |
| 44 | if (i->val) |
| 45 | { |
| 46 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 52 | { |
| 53 | auto i = arguments.find(opt); |
| 54 | if (i == arguments.end()) |
| 55 | { |
| 56 | return emptyString; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | return i->second; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void ArgumentParser::usage(char** argv) |
| 65 | { |
| 66 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| 67 | std::cerr << "Options:\n"; |
Matt Spinler | 15b5f40 | 2018-01-18 14:37:34 -0600 | [diff] [blame] | 68 | std::cerr << " --help Print this menu\n"; |
| 69 | std::cerr << " --path=<objpath> Path to location to" |
| 70 | " monitor\n"; |
| 71 | std::cerr << " --instance=<instance number> Instance number for" |
| 72 | " this power supply\n"; |
| 73 | std::cerr << " --inventory=<inventory path> Inventory path for" |
| 74 | " this power supply\n"; |
| 75 | std::cerr << " --num-history-records=<num records> Number of input" |
| 76 | " power history records to provide on D-Bus\n"; |
| 77 | std::cerr << " --sync-gpio-path=<path> GPIO chip device" |
| 78 | " for the GPIO that performs the sync function\n"; |
| 79 | std::cerr << " --sync-gpio-num=<path> GPIO number for the" |
| 80 | " GPIO that performs the sync function\n"; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 81 | std::cerr << std::flush; |
| 82 | } |
| 83 | |
| 84 | const option ArgumentParser::options[] = |
| 85 | { |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 86 | { "path", required_argument, NULL, 'p' }, |
| 87 | { "instance", required_argument, NULL, 'n' }, |
| 88 | { "inventory", required_argument, NULL, 'i' }, |
Matt Spinler | 15b5f40 | 2018-01-18 14:37:34 -0600 | [diff] [blame] | 89 | { "num-history-records", required_argument, NULL, 'r' }, |
| 90 | { "sync-gpio-path", required_argument, NULL, 'a' }, |
| 91 | { "sync-gpio-num", required_argument, NULL, 'u' }, |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 92 | { "help", no_argument, NULL, 'h' }, |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 93 | { 0, 0, 0, 0}, |
| 94 | }; |
| 95 | |
Matt Spinler | 15b5f40 | 2018-01-18 14:37:34 -0600 | [diff] [blame] | 96 | const char* ArgumentParser::optionStr = "p:n:i:r:a:u:h"; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 97 | |
| 98 | const std::string ArgumentParser::trueString = "true"; |
| 99 | const std::string ArgumentParser::emptyString = ""; |
| 100 | |
| 101 | } |
| 102 | } |