blob: 6d1273f62a870076c7a46248c41b0bd6ae6910af [file] [log] [blame]
Ratan Guptab38401b2018-03-16 12:44:26 +05301/**
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 Venture189d44e2018-07-09 12:30:59 -070016#include "argument.hpp"
17
Ratan Guptab38401b2018-03-16 12:44:26 +053018#include <iostream>
Ratan Guptab38401b2018-03-16 12:44:26 +053019
20namespace phosphor
21{
22namespace network
23{
24namespace ncsi
25{
26
27ArgumentParser::ArgumentParser(int argc, char** argv)
28{
29 int option = 0;
30 while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
31 {
32 if ((option == '?') || (option == 'h'))
33 {
34 usage(argv);
35 exit(-1);
36 }
37
38 auto i = &options[0];
39 while ((i->val != option) && (i->val != 0))
40 {
41 ++i;
42 }
43
44 if (i->val)
45 {
46 arguments[i->name] = (i->has_arg ? optarg : trueString);
47 }
48 }
49}
50
51const std::string& ArgumentParser::operator[](const std::string& opt)
52{
53 auto i = arguments.find(opt);
54 if (i == arguments.end())
55 {
56 return emptyString;
57 }
58 else
59 {
60 return i->second;
61 }
62}
63
64void ArgumentParser::usage(char** argv)
65{
66 std::cerr << "Usage: " << argv[0] << " [options]\n";
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 std::cerr
Johnathan Mantey1ebea282024-02-15 10:26:06 -080068 << "Options:\n"
69 " --help | -h Print this menu.\n"
70 " --index=<device index> | -x <device index> Specify device ifindex.\n"
71 " --package=<package> | -p <package> Specify a package.\n"
72 " --channel=<channel> | -c <channel> Specify a channel.\n"
73 " --info | -i Retrieve info about NCSI topology.\n"
74 " --set | -s Set a specific package/channel.\n"
75 " --clear | -r Clear all the settings on the interface.\n"
Johnathan Mantey5a456062024-02-15 08:45:08 -080076 " --pmask=<mask> | -j <mask> Bitmask to enable/disable packages\n"
77 " --cmask=<mask> | -k <mask> Bitmask to enable/disable channels\n"
Johnathan Mantey1ebea282024-02-15 10:26:06 -080078 "\n"
79 "Example commands:\n"
80 " 1) Retrieve topology information:\n"
81 " ncsi-netlink -x 3 -p 0 -i\n"
82 " 2) Set preferred package\n"
83 " ncsi-netlink -x 3 -p 0 -s\n"
84 " 3) Set preferred channel\n"
85 " ncsi-netlink -x 3 -p 0 -c 1 -s\n"
86 " 4) Clear preferred channel\n"
87 " ncsi-netlink -x 3 -p 0 -r\n"
Jeremy Kerrbad17c02024-11-21 16:44:39 +080088 " 5) Set Package Mask\n"
Johnathan Mantey5a456062024-02-15 08:45:08 -080089 " ncsi-netlink -x 3 -j 1\n"
Jeremy Kerrbad17c02024-11-21 16:44:39 +080090 " 6) Set Channel Mask\n"
Johnathan Mantey5a456062024-02-15 08:45:08 -080091 " ncsi-netlink -x 3 -p 0 -k 1\n"
Johnathan Mantey1ebea282024-02-15 10:26:06 -080092 "\n";
Ratan Guptab38401b2018-03-16 12:44:26 +053093}
94
Gunnar Mills57d9c502018-09-14 14:42:34 -050095const option ArgumentParser::options[] = {
96 {"info", no_argument, NULL, 'i'},
97 {"set", no_argument, NULL, 's'},
98 {"clear", no_argument, NULL, 'r'},
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050099 {"oem-payload", required_argument, NULL, 'o'},
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 {"package", required_argument, NULL, 'p'},
101 {"channel", required_argument, NULL, 'c'},
102 {"index", required_argument, NULL, 'x'},
103 {"help", no_argument, NULL, 'h'},
Johnathan Mantey5a456062024-02-15 08:45:08 -0800104 {"pmask", required_argument, NULL, 'j'},
105 {"cmask", required_argument, NULL, 'k'},
Gunnar Mills57d9c502018-09-14 14:42:34 -0500106 {0, 0, 0, 0},
Ratan Guptab38401b2018-03-16 12:44:26 +0530107};
108
Johnathan Mantey5a456062024-02-15 08:45:08 -0800109const char* ArgumentParser::optionStr = "irsj:k:x:o:p:c:h?";
Ratan Guptab38401b2018-03-16 12:44:26 +0530110
111const std::string ArgumentParser::trueString = "true";
112const std::string ArgumentParser::emptyString = "";
113
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114} // namespace ncsi
115} // namespace network
116} // namespace phosphor