Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -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 | |
| 17 | #include <iostream> |
| 18 | #include <iterator> |
| 19 | #include <algorithm> |
| 20 | #include "argument.hpp" |
| 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace gpio |
| 25 | { |
| 26 | |
| 27 | using namespace std::string_literals; |
| 28 | |
| 29 | const std::string ArgumentParser::trueString = "true"s; |
| 30 | const std::string ArgumentParser::emptyString = ""s; |
| 31 | |
| 32 | const char* ArgumentParser::optionStr = "p:k:n:i:?h"; |
| 33 | const option ArgumentParser::options[] = |
| 34 | { |
| 35 | { "path", required_argument, nullptr, 'p' }, |
| 36 | { "key", required_argument, nullptr, 'k' }, |
| 37 | { "name", required_argument, nullptr, 'n' }, |
| 38 | { "inventory", required_argument, nullptr, 'i' }, |
| 39 | { "help", no_argument, nullptr, 'h' }, |
| 40 | { 0, 0, 0, 0}, |
| 41 | }; |
| 42 | |
| 43 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 44 | { |
| 45 | auto option = 0; |
| 46 | while (-1 != (option = getopt_long(argc, argv, |
| 47 | optionStr, options, nullptr))) |
| 48 | { |
| 49 | if ((option == '?') || (option == 'h')) |
| 50 | { |
| 51 | usage(argv); |
| 52 | exit(-1); |
| 53 | } |
| 54 | |
| 55 | auto i = &options[0]; |
| 56 | while ((i->val != option) && (i->val != 0)) |
| 57 | { |
| 58 | ++i; |
| 59 | } |
| 60 | |
| 61 | if (i->val) |
| 62 | { |
| 63 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 69 | { |
| 70 | auto i = arguments.find(opt); |
| 71 | if (i == arguments.end()) |
| 72 | { |
| 73 | return emptyString; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | return i->second; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void ArgumentParser::usage(char** argv) |
| 82 | { |
| 83 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| 84 | std::cerr << "Options:\n"; |
| 85 | std::cerr << " --help Print this menu\n"; |
| 86 | std::cerr << " --inventory=<inventory> Object path under inventory" |
| 87 | " that will be created\n"; |
| 88 | std::cerr << " --path=<path> Path of device to read for GPIO pin" |
| 89 | " state to determine presence of inventory item\n"; |
| 90 | std::cerr << " --key=<key> Input GPIO key number\n"; |
| 91 | std::cerr << " --name=<name> Pretty name of the inventory" |
| 92 | " item\n"; |
| 93 | std::cerr << std::flush; |
| 94 | exit(-1); |
| 95 | } |
| 96 | } // namespace gpio |
| 97 | } // namespace phosphor |