1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: Copyright 2018 IBM Corporation 3 4 #include "argument.hpp" 5 6 #include <iostream> 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"; 55 std::cerr 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" 64 " --pmask=<mask> | -j <mask> Bitmask to enable/disable packages\n" 65 " --cmask=<mask> | -k <mask> Bitmask to enable/disable channels\n" 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" 76 " 5) Set Package Mask\n" 77 " ncsi-netlink -x 3 -j 1\n" 78 " 6) Set Channel Mask\n" 79 " ncsi-netlink -x 3 -p 0 -k 1\n" 80 "\n"; 81 } 82 83 const option ArgumentParser::options[] = { 84 {"info", no_argument, NULL, 'i'}, 85 {"set", no_argument, NULL, 's'}, 86 {"clear", no_argument, NULL, 'r'}, 87 {"oem-payload", required_argument, NULL, 'o'}, 88 {"package", required_argument, NULL, 'p'}, 89 {"channel", required_argument, NULL, 'c'}, 90 {"index", required_argument, NULL, 'x'}, 91 {"help", no_argument, NULL, 'h'}, 92 {"pmask", required_argument, NULL, 'j'}, 93 {"cmask", required_argument, NULL, 'k'}, 94 {0, 0, 0, 0}, 95 }; 96 97 const char* ArgumentParser::optionStr = "irsj:k:x:o:p:c:h?"; 98 99 const std::string ArgumentParser::trueString = "true"; 100 const std::string ArgumentParser::emptyString = ""; 101 102 } // namespace ncsi 103 } // namespace network 104 } // namespace phosphor 105