blob: 127780c268e89e7d8dfdc53c4bd50b14333f02ac [file] [log] [blame]
Alexander Hansen306542d2025-10-30 17:50:11 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 IBM Corporation
3
Patrick Venture189d44e2018-07-09 12:30:59 -07004#include "argument.hpp"
5
Ratan Guptab38401b2018-03-16 12:44:26 +05306#include <iostream>
Ratan Guptab38401b2018-03-16 12:44:26 +05307
8namespace phosphor
9{
10namespace network
11{
12namespace ncsi
13{
14
15ArgumentParser::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
39const 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
52void ArgumentParser::usage(char** argv)
53{
54 std::cerr << "Usage: " << argv[0] << " [options]\n";
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 std::cerr
Johnathan Mantey1ebea282024-02-15 10:26:06 -080056 << "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 Mantey5a456062024-02-15 08:45:08 -080064 " --pmask=<mask> | -j <mask> Bitmask to enable/disable packages\n"
65 " --cmask=<mask> | -k <mask> Bitmask to enable/disable channels\n"
Johnathan Mantey1ebea282024-02-15 10:26:06 -080066 "\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 Kerrbad17c02024-11-21 16:44:39 +080076 " 5) Set Package Mask\n"
Johnathan Mantey5a456062024-02-15 08:45:08 -080077 " ncsi-netlink -x 3 -j 1\n"
Jeremy Kerrbad17c02024-11-21 16:44:39 +080078 " 6) Set Channel Mask\n"
Johnathan Mantey5a456062024-02-15 08:45:08 -080079 " ncsi-netlink -x 3 -p 0 -k 1\n"
Johnathan Mantey1ebea282024-02-15 10:26:06 -080080 "\n";
Ratan Guptab38401b2018-03-16 12:44:26 +053081}
82
Gunnar Mills57d9c502018-09-14 14:42:34 -050083const option ArgumentParser::options[] = {
84 {"info", no_argument, NULL, 'i'},
85 {"set", no_argument, NULL, 's'},
86 {"clear", no_argument, NULL, 'r'},
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050087 {"oem-payload", required_argument, NULL, 'o'},
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 {"package", required_argument, NULL, 'p'},
89 {"channel", required_argument, NULL, 'c'},
90 {"index", required_argument, NULL, 'x'},
91 {"help", no_argument, NULL, 'h'},
Johnathan Mantey5a456062024-02-15 08:45:08 -080092 {"pmask", required_argument, NULL, 'j'},
93 {"cmask", required_argument, NULL, 'k'},
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 {0, 0, 0, 0},
Ratan Guptab38401b2018-03-16 12:44:26 +053095};
96
Johnathan Mantey5a456062024-02-15 08:45:08 -080097const char* ArgumentParser::optionStr = "irsj:k:x:o:p:c:h?";
Ratan Guptab38401b2018-03-16 12:44:26 +053098
99const std::string ArgumentParser::trueString = "true";
100const std::string ArgumentParser::emptyString = "";
101
Gunnar Mills57d9c502018-09-14 14:42:34 -0500102} // namespace ncsi
103} // namespace network
104} // namespace phosphor