Matt Spinler | e3b859c | 2017-05-25 11:15:43 -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 | */ |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 16 | #include "argument.hpp" |
| 17 | |
| 18 | #include <algorithm> |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iterator> |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace gpio |
| 25 | { |
| 26 | |
| 27 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 28 | { |
| 29 | int 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"; |
| 68 | std::cerr << " --help Print this menu.\n"; |
| 69 | std::cerr << " --gpio=<gpio> The GPIO number. Example: 1\n"; |
| 70 | std::cerr << " --path=<path> The path to the GPIO device." |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 71 | " Example: /dev/gpiochip0\n"; |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 72 | std::cerr << " --delay=<delay> The delay in ms in between a toggle." |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 73 | " Example: 5\n"; |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 74 | std::cerr << " --action=<action> The action to do.\n"; |
| 75 | std::cerr << " Valid actions: low, high, low_high, " |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 76 | "high_low\n"; |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 77 | std::cerr << std::flush; |
| 78 | } |
| 79 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 80 | const option ArgumentParser::options[] = { |
| 81 | {"action", required_argument, NULL, 'a'}, |
| 82 | {"gpio", required_argument, NULL, 'g'}, |
| 83 | {"delay", required_argument, NULL, 'd'}, |
| 84 | {"path", required_argument, NULL, 'p'}, |
| 85 | {"help", no_argument, NULL, 'h'}, |
| 86 | {0, 0, 0, 0}, |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | const char* ArgumentParser::optionStr = "a:g:d:p:h?"; |
| 90 | |
| 91 | const std::string ArgumentParser::trueString = "true"; |
| 92 | const std::string ArgumentParser::emptyString = ""; |
| 93 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 94 | } // namespace gpio |
| 95 | } // namespace phosphor |