summaryrefslogtreecommitdiffstats
path: root/argument.cpp
blob: a988ca504b605e9818f017e005552eeb4f5a20d3 (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
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cassert>
#include "argument.hpp"

ArgumentParser::ArgumentParser(int argc, char** argv)
{
    int option = 0;
    while(-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
    {
        if ((option == '?') || (option == 'h'))
        {
            usage(argv);
            exit(-1);
        }

        auto i = &options[0];
        while ((i->val != option) && (i->val != 0)) ++i;

        if (i->val)
            arguments[i->name] = (i->has_arg ? optarg : true_string);
    }
}

const std::string& ArgumentParser::operator[](const std::string& opt)
{
    auto i = arguments.find(opt);
    if (i == arguments.end())
    {
        return empty_string;
    }
    else
    {
        return i->second;
    }
}

void ArgumentParser::usage(char** argv)
{
    std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
    std::cerr << "Options:" << std::endl;
    std::cerr << " --eeprom=<eeprom file path> Absolute file name of eeprom"
        << std::endl;
    std::cerr << " --fruid=<FRU ID>            valid fru id in integer"
        << std::endl;
    std::cerr << " --help                      display help"
        << std::endl;
}

const option ArgumentParser::options[] =
{
    { "eeprom",   required_argument,  NULL, 'e' },
    { "fruid",    required_argument,  NULL, 'f' },
    { "help",     no_argument,        NULL, 'h' },
    { 0, 0, 0, 0},
};

const char* ArgumentParser::optionstr = "e:f:?h";

const std::string ArgumentParser::true_string = "true";
const std::string ArgumentParser::empty_string = "";
OpenPOWER on IntegriCloud