Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 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 | #include <iostream> |
| 17 | #include <iterator> |
| 18 | #include <algorithm> |
| 19 | #include "argument.hpp" |
| 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace network |
| 24 | { |
| 25 | namespace ncsi |
| 26 | { |
| 27 | |
| 28 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 29 | { |
| 30 | int option = 0; |
| 31 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL))) |
| 32 | { |
| 33 | if ((option == '?') || (option == 'h')) |
| 34 | { |
| 35 | usage(argv); |
| 36 | exit(-1); |
| 37 | } |
| 38 | |
| 39 | auto i = &options[0]; |
| 40 | while ((i->val != option) && (i->val != 0)) |
| 41 | { |
| 42 | ++i; |
| 43 | } |
| 44 | |
| 45 | if (i->val) |
| 46 | { |
| 47 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 53 | { |
| 54 | auto i = arguments.find(opt); |
| 55 | if (i == arguments.end()) |
| 56 | { |
| 57 | return emptyString; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | return i->second; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void ArgumentParser::usage(char** argv) |
| 66 | { |
| 67 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| 68 | std::cerr << "Options:\n"; |
| 69 | std::cerr << " --help Print this menu.\n"; |
| 70 | std::cerr << " --info=<info> Retrieve info about NCSI topology.\n"; |
| 71 | std::cerr << " --set=<set> Set a specific package/channel.\n"; |
| 72 | std::cerr << " --clear=<clear> Clear all the settings on the interface.\n"; |
| 73 | std::cerr << " --package=<package> Specify a package.\n"; |
| 74 | std::cerr << " --channel=<channel> Specify a channel.\n"; |
| 75 | std::cerr << " --index=<device index> Specify device ifindex.\n"; |
| 76 | std::cerr << std::flush; |
| 77 | } |
| 78 | |
| 79 | const option ArgumentParser::options[] = |
| 80 | { |
| 81 | { "info", no_argument, NULL, 'i' }, |
| 82 | { "set", no_argument, NULL, 's' }, |
| 83 | { "clear", no_argument, NULL, 'r' }, |
| 84 | { "package", required_argument, NULL, 'p' }, |
| 85 | { "channel", required_argument, NULL, 'c' }, |
| 86 | { "index", required_argument, NULL, 'x' }, |
| 87 | { "help", no_argument, NULL, 'h' }, |
| 88 | { 0, 0, 0, 0}, |
| 89 | }; |
| 90 | |
| 91 | const char* ArgumentParser::optionStr = "i:s:r:p:c:x:h?"; |
| 92 | |
| 93 | const std::string ArgumentParser::trueString = "true"; |
| 94 | const std::string ArgumentParser::emptyString = ""; |
| 95 | |
| 96 | } //namespace ncsi |
| 97 | } //namespace network |
| 98 | } //namespece phosphor |
| 99 | |