blob: e210b420290fd4567bd199fcba70f8fda0c41705 [file] [log] [blame]
Patrick Venturec83c4dc2018-11-01 16:29:18 -07001#include "args.hpp"
2
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -06003#include <getopt.h>
Patrick Venturec83c4dc2018-11-01 16:29:18 -07004
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -06005#include <iostream>
6#include <sstream>
Patrick Venturec83c4dc2018-11-01 16:29:18 -07007#include <string>
8#include <utility>
9#include <vector>
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060010
11namespace openpower
12{
13namespace vpd
14{
15namespace args
16{
17
Joel Stanley9c415242020-04-17 13:07:07 +093018static constexpr auto shortForm = "v:f:o:dh";
Patrick Venturec83c4dc2018-11-01 16:29:18 -070019static const option longForm[] = {
20 {"vpd", required_argument, nullptr, 'v'},
21 {"fru", required_argument, nullptr, 'f'},
22 {"object", required_argument, nullptr, 'o'},
Joel Stanley9c415242020-04-17 13:07:07 +093023 {"dump", no_argument, nullptr, 'd'},
Patrick Venturec83c4dc2018-11-01 16:29:18 -070024 {"help", no_argument, nullptr, 'h'},
25 {0, 0, 0, 0},
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060026};
27
28void usage(char** argv)
29{
30 std::cerr << "\nUsage: " << argv[0] << " args\n";
31 std::cerr << "args:\n";
32 std::cerr << "--vpd=<vpd file> pathname of file containing vpd,";
33 std::cerr << " for eg an eeprom file\n";
Joel Stanley9c415242020-04-17 13:07:07 +093034 std::cerr << "--dump output contents of parsed VPD\n";
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060035 std::cerr << "--fru=<FRU type>, supported types:\n";
Patrick Venturec83c4dc2018-11-01 16:29:18 -070036 std::cerr << "\t"
37 << "bmc\n";
38 std::cerr << "\t"
39 << "ethernet\n";
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060040 std::cerr << "Specify multiple FRU types via comma-separated list\n";
41 std::cerr << "--object=<FRU object path> for eg,";
42 std::cerr << " chassis/bmc0/planar\n";
43 std::cerr << "Specify multiple object paths via comma-separated list, "
Patrick Venturec83c4dc2018-11-01 16:29:18 -070044 "ordered as the FRU types\n";
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060045 std::cerr << "--help display usage\n";
46}
47
48Args parse(int argc, char** argv)
49{
50 Args args;
51 int option = 0;
52 if (1 == argc)
53 {
54 usage(argv);
55 }
56 while (-1 !=
57 (option = getopt_long(argc, argv, shortForm, longForm, nullptr)))
58 {
59 if (('h' == option) || ('?' == option))
60 {
61 usage(argv);
62 }
63 else
64 {
65 auto which = &longForm[0];
66 // Figure out which option
67 while ((which->val != option) && (which->val != 0))
68 {
69 ++which;
70 }
71 // If option needs an argument, note the argument value
72 if ((no_argument != which->has_arg) && optarg)
73 {
74 using argList = std::vector<std::string>;
75 argList values;
76 // There could be a comma-separated list
77 std::string opts(optarg);
78 std::istringstream stream(std::move(opts));
Patrick Venturec83c4dc2018-11-01 16:29:18 -070079 std::string input{};
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060080 while (std::getline(stream, input, ','))
81 {
82 values.push_back(std::move(input));
83 }
84 args.emplace(which->name, std::move(values));
85 }
Joel Stanley9c415242020-04-17 13:07:07 +093086 else
87 {
88 // Add options that do not have arguments to the arglist
89 args.emplace(which->name, std::vector<std::string>{});
90 }
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060091 }
92 }
93
94 return args;
95}
96
97} // namespace args
98} // namespace vpd
99} // namespace openpower