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 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 17 | #include "argument.hpp" |
| 18 | |
| 19 | #include <algorithm> |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <iterator> |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace gpio |
| 26 | { |
| 27 | |
| 28 | using namespace std::string_literals; |
| 29 | |
| 30 | const std::string ArgumentParser::trueString = "true"s; |
| 31 | const std::string ArgumentParser::emptyString = ""s; |
| 32 | |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 33 | const char* ArgumentParser::optionStr = "p:k:n:i:d:?h"; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 34 | const option ArgumentParser::options[] = { |
| 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 | {"drivers", required_argument, nullptr, 'd'}, |
| 40 | {"help", no_argument, nullptr, 'h'}, |
| 41 | {0, 0, 0, 0}, |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 45 | { |
| 46 | auto option = 0; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 47 | while (-1 != |
| 48 | (option = getopt_long(argc, argv, optionStr, options, nullptr))) |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 49 | { |
| 50 | if ((option == '?') || (option == 'h')) |
| 51 | { |
| 52 | usage(argv); |
| 53 | exit(-1); |
| 54 | } |
| 55 | |
| 56 | auto i = &options[0]; |
| 57 | while ((i->val != option) && (i->val != 0)) |
| 58 | { |
| 59 | ++i; |
| 60 | } |
| 61 | |
| 62 | if (i->val) |
| 63 | { |
| 64 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 70 | { |
| 71 | auto i = arguments.find(opt); |
| 72 | if (i == arguments.end()) |
| 73 | { |
| 74 | return emptyString; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | return i->second; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void ArgumentParser::usage(char** argv) |
| 83 | { |
| 84 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| 85 | std::cerr << "Options:\n"; |
| 86 | std::cerr << " --help Print this menu\n"; |
| 87 | std::cerr << " --inventory=<inventory> Object path under inventory" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 88 | " that will be created\n"; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 89 | std::cerr << " --path=<path> Path of device to read for GPIO pin" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 90 | " state to determine presence of inventory item\n"; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 91 | std::cerr << " --key=<key> Input GPIO key number\n"; |
| 92 | std::cerr << " --name=<name> Pretty name of the inventory" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 93 | " item\n"; |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 94 | std::cerr << " --drivers=<drivers> List of drivers to bind when card" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 95 | " is added and unbind when card is removed\n"; |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 96 | std::cerr << " Format is a space separated list" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 97 | " of path,device pairs. For example:\n"; |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 98 | std::cerr << " " |
| 99 | "/sys/bus/i2c/drivers/some-driver,3-0068\n"; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 100 | std::cerr << std::flush; |
| 101 | exit(-1); |
| 102 | } |
| 103 | } // namespace gpio |
| 104 | } // namespace phosphor |