Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cassert> |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <iterator> |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 23 | |
| 24 | namespace phosphor |
| 25 | { |
| 26 | namespace gpio |
| 27 | { |
| 28 | |
| 29 | using namespace std::string_literals; |
| 30 | |
| 31 | const std::string ArgumentParser::trueString = "true"s; |
| 32 | const std::string ArgumentParser::emptyString = ""s; |
| 33 | |
| 34 | const char* ArgumentParser::optionStr = "p:k:r:t:?h"; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 35 | const option ArgumentParser::options[] = { |
| 36 | {"path", required_argument, nullptr, 'p'}, |
| 37 | {"key", required_argument, nullptr, 'k'}, |
| 38 | {"polarity", required_argument, nullptr, 'r'}, |
| 39 | {"target", required_argument, nullptr, 't'}, |
| 40 | {"continue", no_argument, nullptr, 'c'}, |
| 41 | {"help", no_argument, nullptr, 'h'}, |
| 42 | {0, 0, 0, 0}, |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 46 | { |
| 47 | int 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))) |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [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 | { |
Gunnar Mills | 96e01b7 | 2018-08-14 11:59:05 -0500 | [diff] [blame] | 65 | // optional argument may get nullptr for optarg |
Lei YU | bc4a4ff | 2018-04-11 13:33:25 +0800 | [diff] [blame] | 66 | // make it empty string in such case |
| 67 | auto arg = (optarg == nullptr ? "" : optarg); |
| 68 | arguments[i->name] = (i->has_arg ? arg : trueString); |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 74 | { |
| 75 | auto i = arguments.find(opt); |
| 76 | if (i == arguments.end()) |
| 77 | { |
| 78 | return emptyString; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | return i->second; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void ArgumentParser::usage(char** argv) |
| 87 | { |
| 88 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| 89 | std::cerr << "Options:\n"; |
| 90 | std::cerr << " --help Print this menu\n"; |
| 91 | std::cerr << " --path=<path> Path of input device." |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 92 | " Ex: /dev/input/event2\n"; |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 93 | std::cerr << " --key=<key> Input GPIO key number\n"; |
| 94 | std::cerr << " --polarity=<polarity> Asertion polarity to look for." |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 95 | " This is 0 / 1 \n"; |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 96 | std::cerr << " --target=<systemd unit> Systemd unit to be called on GPIO" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 97 | " state change\n"; |
Lei YU | 065b767 | 2018-04-19 10:59:40 +0800 | [diff] [blame] | 98 | std::cerr << " [--continue] Whether or not to continue" |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 99 | " after key pressed\n"; |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 100 | } |
| 101 | } // namespace gpio |
| 102 | } // namespace phosphor |