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 | */ |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 16 | #include "argument.hpp" |
| 17 | |
| 18 | #include <algorithm> |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iterator> |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace network |
| 25 | { |
| 26 | namespace ncsi |
| 27 | { |
| 28 | |
| 29 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 30 | { |
| 31 | int option = 0; |
| 32 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL))) |
| 33 | { |
| 34 | if ((option == '?') || (option == 'h')) |
| 35 | { |
| 36 | usage(argv); |
| 37 | exit(-1); |
| 38 | } |
| 39 | |
| 40 | auto i = &options[0]; |
| 41 | while ((i->val != option) && (i->val != 0)) |
| 42 | { |
| 43 | ++i; |
| 44 | } |
| 45 | |
| 46 | if (i->val) |
| 47 | { |
| 48 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 54 | { |
| 55 | auto i = arguments.find(opt); |
| 56 | if (i == arguments.end()) |
| 57 | { |
| 58 | return emptyString; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | return i->second; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void ArgumentParser::usage(char** argv) |
| 67 | { |
| 68 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 69 | std::cerr |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame^] | 70 | << "Options:\n" |
| 71 | " --help | -h Print this menu.\n" |
| 72 | " --index=<device index> | -x <device index> Specify device ifindex.\n" |
| 73 | " --package=<package> | -p <package> Specify a package.\n" |
| 74 | " --channel=<channel> | -c <channel> Specify a channel.\n" |
| 75 | " --info | -i Retrieve info about NCSI topology.\n" |
| 76 | " --set | -s Set a specific package/channel.\n" |
| 77 | " --clear | -r Clear all the settings on the interface.\n" |
| 78 | " --oem-payload=<hex data...> | -o <hex data...> Send an OEM command with payload.\n" |
| 79 | "\n" |
| 80 | "Example commands:\n" |
| 81 | " 1) Retrieve topology information:\n" |
| 82 | " ncsi-netlink -x 3 -p 0 -i\n" |
| 83 | " 2) Set preferred package\n" |
| 84 | " ncsi-netlink -x 3 -p 0 -s\n" |
| 85 | " 3) Set preferred channel\n" |
| 86 | " ncsi-netlink -x 3 -p 0 -c 1 -s\n" |
| 87 | " 4) Clear preferred channel\n" |
| 88 | " ncsi-netlink -x 3 -p 0 -r\n" |
| 89 | " 5) Send NCSI Command\n" |
| 90 | " ncsi-netlink -x 3 -p 0 -c 0 -o 50000001572100\n" |
| 91 | "\n"; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 92 | } |
| 93 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | const option ArgumentParser::options[] = { |
| 95 | {"info", no_argument, NULL, 'i'}, |
| 96 | {"set", no_argument, NULL, 's'}, |
| 97 | {"clear", no_argument, NULL, 'r'}, |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 98 | {"oem-payload", required_argument, NULL, 'o'}, |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 99 | {"package", required_argument, NULL, 'p'}, |
| 100 | {"channel", required_argument, NULL, 'c'}, |
| 101 | {"index", required_argument, NULL, 'x'}, |
| 102 | {"help", no_argument, NULL, 'h'}, |
| 103 | {0, 0, 0, 0}, |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 104 | }; |
| 105 | |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame^] | 106 | const char* ArgumentParser::optionStr = "irsx:o:p:c:h?"; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 107 | |
| 108 | const std::string ArgumentParser::trueString = "true"; |
| 109 | const std::string ArgumentParser::emptyString = ""; |
| 110 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 111 | } // namespace ncsi |
| 112 | } // namespace network |
| 113 | } // namespace phosphor |