blob: 0c49b11a0f2941fe029292b415cc71362f8e5655 [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
18#include <algorithm>
Ratan Guptab38401b2018-03-16 12:44:26 +053019#include <iostream>
20#include <iterator>
Ratan Guptab38401b2018-03-16 12:44:26 +053021
22namespace phosphor
23{
24namespace network
25{
26namespace ncsi
27{
28
29ArgumentParser::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
53const 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
66void ArgumentParser::usage(char** argv)
67{
68 std::cerr << "Usage: " << argv[0] << " [options]\n";
69 std::cerr << "Options:\n";
70 std::cerr << " --help Print this menu.\n";
71 std::cerr << " --info=<info> Retrieve info about NCSI topology.\n";
72 std::cerr << " --set=<set> Set a specific package/channel.\n";
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 std::cerr
74 << " --clear=<clear> Clear all the settings on the interface.\n";
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050075 std::cerr
76 << " --oem-payload=<hex data> Send an OEM command with payload.\n";
Ratan Guptab38401b2018-03-16 12:44:26 +053077 std::cerr << " --package=<package> Specify a package.\n";
78 std::cerr << " --channel=<channel> Specify a channel.\n";
79 std::cerr << " --index=<device index> Specify device ifindex.\n";
80 std::cerr << std::flush;
81}
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'},
92 {0, 0, 0, 0},
Ratan Guptab38401b2018-03-16 12:44:26 +053093};
94
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050095const char* ArgumentParser::optionStr = "i:s:r:o:p:c:x:h?";
Ratan Guptab38401b2018-03-16 12:44:26 +053096
97const std::string ArgumentParser::trueString = "true";
98const std::string ArgumentParser::emptyString = "";
99
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100} // namespace ncsi
101} // namespace network
102} // namespace phosphor