blob: 673b08093e4f371b57356d2fa814823c8e996945 [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
18static constexpr auto shortForm = "v:f:o:h";
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'},
23 {"help", no_argument, nullptr, 'h'},
24 {0, 0, 0, 0},
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060025};
26
27void usage(char** argv)
28{
29 std::cerr << "\nUsage: " << argv[0] << " args\n";
30 std::cerr << "args:\n";
31 std::cerr << "--vpd=<vpd file> pathname of file containing vpd,";
32 std::cerr << " for eg an eeprom file\n";
33 std::cerr << "--fru=<FRU type>, supported types:\n";
Patrick Venturec83c4dc2018-11-01 16:29:18 -070034 std::cerr << "\t"
35 << "bmc\n";
36 std::cerr << "\t"
37 << "ethernet\n";
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060038 std::cerr << "Specify multiple FRU types via comma-separated list\n";
39 std::cerr << "--object=<FRU object path> for eg,";
40 std::cerr << " chassis/bmc0/planar\n";
41 std::cerr << "Specify multiple object paths via comma-separated list, "
Patrick Venturec83c4dc2018-11-01 16:29:18 -070042 "ordered as the FRU types\n";
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060043 std::cerr << "--help display usage\n";
44}
45
46Args parse(int argc, char** argv)
47{
48 Args args;
49 int option = 0;
50 if (1 == argc)
51 {
52 usage(argv);
53 }
54 while (-1 !=
55 (option = getopt_long(argc, argv, shortForm, longForm, nullptr)))
56 {
57 if (('h' == option) || ('?' == option))
58 {
59 usage(argv);
60 }
61 else
62 {
63 auto which = &longForm[0];
64 // Figure out which option
65 while ((which->val != option) && (which->val != 0))
66 {
67 ++which;
68 }
69 // If option needs an argument, note the argument value
70 if ((no_argument != which->has_arg) && optarg)
71 {
72 using argList = std::vector<std::string>;
73 argList values;
74 // There could be a comma-separated list
75 std::string opts(optarg);
76 std::istringstream stream(std::move(opts));
Patrick Venturec83c4dc2018-11-01 16:29:18 -070077 std::string input{};
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -060078 while (std::getline(stream, input, ','))
79 {
80 values.push_back(std::move(input));
81 }
82 args.emplace(which->name, std::move(values));
83 }
84 }
85 }
86
87 return args;
88}
89
90} // namespace args
91} // namespace vpd
92} // namespace openpower