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