Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +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 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 17 | #include "argument.hpp" |
| 18 | |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cassert> |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <iterator> |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 23 | |
| 24 | namespace phosphor |
| 25 | { |
| 26 | namespace led |
| 27 | { |
| 28 | |
| 29 | const std::string ArgumentParser::true_string = "true"; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 30 | |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 31 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 32 | { |
| 33 | int option = 0; |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 34 | while (-1 != |
Andrew Jeffery | 9648770 | 2023-02-03 16:28:49 +1030 | [diff] [blame] | 35 | (option = getopt_long(argc, argv, optionstr, &options[0], nullptr))) |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 36 | { |
| 37 | if ((option == '?') || (option == 'h')) |
| 38 | { |
| 39 | usage(argv); |
| 40 | exit(-1); |
| 41 | } |
| 42 | |
Andrew Jeffery | 1d88e68 | 2023-02-03 16:10:44 +1030 | [diff] [blame] | 43 | const auto* i = &options[0]; |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 44 | while ((i->val != option) && (i->val != 0)) |
Andrew Jeffery | d8e3da6 | 2023-02-03 16:30:12 +1030 | [diff] [blame^] | 45 | { |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 46 | ++i; |
Andrew Jeffery | d8e3da6 | 2023-02-03 16:30:12 +1030 | [diff] [blame^] | 47 | } |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 48 | |
| 49 | if (i->val) |
Andrew Jeffery | d8e3da6 | 2023-02-03 16:30:12 +1030 | [diff] [blame^] | 50 | { |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 51 | arguments[i->name] = (i->has_arg ? optarg : true_string); |
Andrew Jeffery | d8e3da6 | 2023-02-03 16:30:12 +1030 | [diff] [blame^] | 52 | } |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 57 | { |
Andrew Jeffery | f9e6cd3 | 2023-02-03 15:49:44 +1030 | [diff] [blame] | 58 | static const std::string emptyString{}; |
| 59 | |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 60 | auto i = arguments.find(opt); |
| 61 | if (i == arguments.end()) |
| 62 | { |
Andrew Jeffery | f9e6cd3 | 2023-02-03 15:49:44 +1030 | [diff] [blame] | 63 | return emptyString; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 64 | } |
| 65 | else |
| 66 | { |
| 67 | return i->second; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void ArgumentParser::usage(char** argv) |
| 72 | { |
| 73 | std::cerr << "Usage: " << argv[0] << " [options]" << std::endl; |
| 74 | std::cerr << "Options:" << std::endl; |
| 75 | std::cerr << " --help Print this menu" << std::endl; |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 76 | std::cerr << " --path=<path> absolute path of LED in sysfs; like"; |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 77 | std::cerr << " /sys/class/leds/<name>" << std::endl; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 78 | } |
| 79 | } // namespace led |
| 80 | } // namespace phosphor |