| Alexander Hansen | 306542d | 2025-10-30 17:50:11 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2018 IBM Corporation |
| 3 | |
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include "argument.hpp" |
| 5 | |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 6 | #include <iostream> |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace ncsi |
| 13 | { |
| 14 | |
| 15 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 16 | { |
| 17 | int option = 0; |
| 18 | while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL))) |
| 19 | { |
| 20 | if ((option == '?') || (option == 'h')) |
| 21 | { |
| 22 | usage(argv); |
| 23 | exit(-1); |
| 24 | } |
| 25 | |
| 26 | auto i = &options[0]; |
| 27 | while ((i->val != option) && (i->val != 0)) |
| 28 | { |
| 29 | ++i; |
| 30 | } |
| 31 | |
| 32 | if (i->val) |
| 33 | { |
| 34 | arguments[i->name] = (i->has_arg ? optarg : trueString); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const std::string& ArgumentParser::operator[](const std::string& opt) |
| 40 | { |
| 41 | auto i = arguments.find(opt); |
| 42 | if (i == arguments.end()) |
| 43 | { |
| 44 | return emptyString; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | return i->second; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void ArgumentParser::usage(char** argv) |
| 53 | { |
| 54 | std::cerr << "Usage: " << argv[0] << " [options]\n"; |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | std::cerr |
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 56 | << "Options:\n" |
| 57 | " --help | -h Print this menu.\n" |
| 58 | " --index=<device index> | -x <device index> Specify device ifindex.\n" |
| 59 | " --package=<package> | -p <package> Specify a package.\n" |
| 60 | " --channel=<channel> | -c <channel> Specify a channel.\n" |
| 61 | " --info | -i Retrieve info about NCSI topology.\n" |
| 62 | " --set | -s Set a specific package/channel.\n" |
| 63 | " --clear | -r Clear all the settings on the interface.\n" |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 64 | " --pmask=<mask> | -j <mask> Bitmask to enable/disable packages\n" |
| 65 | " --cmask=<mask> | -k <mask> Bitmask to enable/disable channels\n" |
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 66 | "\n" |
| 67 | "Example commands:\n" |
| 68 | " 1) Retrieve topology information:\n" |
| 69 | " ncsi-netlink -x 3 -p 0 -i\n" |
| 70 | " 2) Set preferred package\n" |
| 71 | " ncsi-netlink -x 3 -p 0 -s\n" |
| 72 | " 3) Set preferred channel\n" |
| 73 | " ncsi-netlink -x 3 -p 0 -c 1 -s\n" |
| 74 | " 4) Clear preferred channel\n" |
| 75 | " ncsi-netlink -x 3 -p 0 -r\n" |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 76 | " 5) Set Package Mask\n" |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 77 | " ncsi-netlink -x 3 -j 1\n" |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 78 | " 6) Set Channel Mask\n" |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 79 | " ncsi-netlink -x 3 -p 0 -k 1\n" |
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 80 | "\n"; |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | const option ArgumentParser::options[] = { |
| 84 | {"info", no_argument, NULL, 'i'}, |
| 85 | {"set", no_argument, NULL, 's'}, |
| 86 | {"clear", no_argument, NULL, 'r'}, |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 87 | {"oem-payload", required_argument, NULL, 'o'}, |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | {"package", required_argument, NULL, 'p'}, |
| 89 | {"channel", required_argument, NULL, 'c'}, |
| 90 | {"index", required_argument, NULL, 'x'}, |
| 91 | {"help", no_argument, NULL, 'h'}, |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 92 | {"pmask", required_argument, NULL, 'j'}, |
| 93 | {"cmask", required_argument, NULL, 'k'}, |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | {0, 0, 0, 0}, |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 95 | }; |
| 96 | |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 97 | const char* ArgumentParser::optionStr = "irsj:k:x:o:p:c:h?"; |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 98 | |
| 99 | const std::string ArgumentParser::trueString = "true"; |
| 100 | const std::string ArgumentParser::emptyString = ""; |
| 101 | |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 102 | } // namespace ncsi |
| 103 | } // namespace network |
| 104 | } // namespace phosphor |