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 | |
| 17 | #include <iostream> |
| 18 | #include <iterator> |
| 19 | #include <algorithm> |
| 20 | #include <cassert> |
| 21 | #include "argument.hpp" |
| 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace led |
| 26 | { |
| 27 | |
| 28 | const std::string ArgumentParser::true_string = "true"; |
| 29 | const std::string ArgumentParser::empty_string = ""; |
| 30 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 31 | const char* ArgumentParser::optionstr = "p:?h"; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 32 | const option ArgumentParser::options[] = |
| 33 | { |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 34 | { "path", required_argument, nullptr, 'p' }, |
| 35 | { "help", no_argument, nullptr, 'h' }, |
| 36 | { 0, 0, 0, 0}, |
| 37 | }; |
| 38 | |
| 39 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 40 | { |
| 41 | int option = 0; |
| 42 | while(-1 != (option = getopt_long(argc, argv, optionstr, options, nullptr))) |
| 43 | { |
| 44 | if ((option == '?') || (option == 'h')) |
| 45 | { |
| 46 | usage(argv); |
| 47 | exit(-1); |
| 48 | } |
| 49 | |
| 50 | auto i = &options[0]; |
| 51 | while ((i->val != option) && (i->val != 0)) ++i; |
| 52 | |
| 53 | if (i->val) |
| 54 | arguments[i->name] = (i->has_arg ? optarg : true_string); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 59 | { |
| 60 | auto i = arguments.find(opt); |
| 61 | if (i == arguments.end()) |
| 62 | { |
| 63 | return empty_string; |
| 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"; |
| 77 | std::cerr << " /sys/class/leds/<name>" |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 78 | << std::endl; |
| 79 | } |
| 80 | } // namespace led |
| 81 | } // namespace phosphor |