Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 1 | #include "args.hpp" |
| 2 | |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 3 | #include <getopt.h> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 4 | |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <sstream> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace vpd |
| 14 | { |
| 15 | namespace args |
| 16 | { |
| 17 | |
Joel Stanley | 9c41524 | 2020-04-17 13:07:07 +0930 | [diff] [blame] | 18 | static constexpr auto shortForm = "v:f:o:dh"; |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 19 | static const option longForm[] = { |
| 20 | {"vpd", required_argument, nullptr, 'v'}, |
| 21 | {"fru", required_argument, nullptr, 'f'}, |
| 22 | {"object", required_argument, nullptr, 'o'}, |
Joel Stanley | 9c41524 | 2020-04-17 13:07:07 +0930 | [diff] [blame] | 23 | {"dump", no_argument, nullptr, 'd'}, |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 24 | {"help", no_argument, nullptr, 'h'}, |
| 25 | {0, 0, 0, 0}, |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | void 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 Stanley | 9c41524 | 2020-04-17 13:07:07 +0930 | [diff] [blame] | 34 | std::cerr << "--dump output contents of parsed VPD\n"; |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 35 | std::cerr << "--fru=<FRU type>, supported types:\n"; |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 36 | std::cerr << "\t" |
| 37 | << "bmc\n"; |
| 38 | std::cerr << "\t" |
| 39 | << "ethernet\n"; |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 40 | 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 44 | "ordered as the FRU types\n"; |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 45 | std::cerr << "--help display usage\n"; |
| 46 | } |
| 47 | |
| 48 | Args 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 79 | std::string input{}; |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 80 | while (std::getline(stream, input, ',')) |
| 81 | { |
| 82 | values.push_back(std::move(input)); |
| 83 | } |
| 84 | args.emplace(which->name, std::move(values)); |
| 85 | } |
Joel Stanley | 9c41524 | 2020-04-17 13:07:07 +0930 | [diff] [blame] | 86 | else |
| 87 | { |
| 88 | // Add options that do not have arguments to the arglist |
| 89 | args.emplace(which->name, std::vector<std::string>{}); |
| 90 | } |
Deepak Kodihalli | 0361e3f | 2016-11-29 00:46:27 -0600 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | return args; |
| 95 | } |
| 96 | |
| 97 | } // namespace args |
| 98 | } // namespace vpd |
| 99 | } // namespace openpower |