blob: 8ec90dded26cb7f3d11289a2b71471e1ce7fcf58 [file] [log] [blame]
Deepak Kodihalli0361e3f2016-11-29 00:46:27 -06001#include <getopt.h>
2#include <string>
3#include <vector>
4#include <utility>
5#include <iostream>
6#include <sstream>
7#include "args.hpp"
8
9namespace openpower
10{
11namespace vpd
12{
13namespace args
14{
15
16static constexpr auto shortForm = "v:f:o:h";
17static const option longForm[] =
18{
19 { "vpd", required_argument, nullptr, 'v' },
20 { "fru", required_argument, nullptr, 'f' },
21 { "object", required_argument, nullptr, 'o' },
22 { "help", no_argument, nullptr, 'h' },
23 { 0, 0, 0, 0},
24};
25
26void usage(char** argv)
27{
28 std::cerr << "\nUsage: " << argv[0] << " args\n";
29 std::cerr << "args:\n";
30 std::cerr << "--vpd=<vpd file> pathname of file containing vpd,";
31 std::cerr << " for eg an eeprom file\n";
32 std::cerr << "--fru=<FRU type>, supported types:\n";
33 std::cerr << "\t" << "bmc\n";
34 std::cerr << "\t" << "ethernet\n";
35 std::cerr << "Specify multiple FRU types via comma-separated list\n";
36 std::cerr << "--object=<FRU object path> for eg,";
37 std::cerr << " chassis/bmc0/planar\n";
38 std::cerr << "Specify multiple object paths via comma-separated list, "
39 "ordered as the FRU types\n";
40 std::cerr << "--help display usage\n";
41}
42
43Args parse(int argc, char** argv)
44{
45 Args args;
46 int option = 0;
47 if (1 == argc)
48 {
49 usage(argv);
50 }
51 while (-1 !=
52 (option = getopt_long(argc, argv, shortForm, longForm, nullptr)))
53 {
54 if (('h' == option) || ('?' == option))
55 {
56 usage(argv);
57 }
58 else
59 {
60 auto which = &longForm[0];
61 // Figure out which option
62 while ((which->val != option) && (which->val != 0))
63 {
64 ++which;
65 }
66 // If option needs an argument, note the argument value
67 if ((no_argument != which->has_arg) && optarg)
68 {
69 using argList = std::vector<std::string>;
70 argList values;
71 // There could be a comma-separated list
72 std::string opts(optarg);
73 std::istringstream stream(std::move(opts));
74 std::string input {};
75 while (std::getline(stream, input, ','))
76 {
77 values.push_back(std::move(input));
78 }
79 args.emplace(which->name, std::move(values));
80 }
81 }
82 }
83
84 return args;
85}
86
87} // namespace args
88} // namespace vpd
89} // namespace openpower