blob: 673b08093e4f371b57356d2fa814823c8e996945 [file] [log] [blame]
#include "args.hpp"
#include <getopt.h>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace openpower
{
namespace vpd
{
namespace args
{
static constexpr auto shortForm = "v:f:o:h";
static const option longForm[] = {
{"vpd", required_argument, nullptr, 'v'},
{"fru", required_argument, nullptr, 'f'},
{"object", required_argument, nullptr, 'o'},
{"help", no_argument, nullptr, 'h'},
{0, 0, 0, 0},
};
void usage(char** argv)
{
std::cerr << "\nUsage: " << argv[0] << " args\n";
std::cerr << "args:\n";
std::cerr << "--vpd=<vpd file> pathname of file containing vpd,";
std::cerr << " for eg an eeprom file\n";
std::cerr << "--fru=<FRU type>, supported types:\n";
std::cerr << "\t"
<< "bmc\n";
std::cerr << "\t"
<< "ethernet\n";
std::cerr << "Specify multiple FRU types via comma-separated list\n";
std::cerr << "--object=<FRU object path> for eg,";
std::cerr << " chassis/bmc0/planar\n";
std::cerr << "Specify multiple object paths via comma-separated list, "
"ordered as the FRU types\n";
std::cerr << "--help display usage\n";
}
Args parse(int argc, char** argv)
{
Args args;
int option = 0;
if (1 == argc)
{
usage(argv);
}
while (-1 !=
(option = getopt_long(argc, argv, shortForm, longForm, nullptr)))
{
if (('h' == option) || ('?' == option))
{
usage(argv);
}
else
{
auto which = &longForm[0];
// Figure out which option
while ((which->val != option) && (which->val != 0))
{
++which;
}
// If option needs an argument, note the argument value
if ((no_argument != which->has_arg) && optarg)
{
using argList = std::vector<std::string>;
argList values;
// There could be a comma-separated list
std::string opts(optarg);
std::istringstream stream(std::move(opts));
std::string input{};
while (std::getline(stream, input, ','))
{
values.push_back(std::move(input));
}
args.emplace(which->name, std::move(values));
}
}
}
return args;
}
} // namespace args
} // namespace vpd
} // namespace openpower