summaryrefslogtreecommitdiffstats
path: root/args.cpp
blob: 673b08093e4f371b57356d2fa814823c8e996945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#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
OpenPOWER on IntegriCloud