blob: a988ca504b605e9818f017e005552eeb4f5a20d3 [file] [log] [blame]
vishwa13555bd2015-11-10 12:10:38 -06001#include <iostream>
2#include <iterator>
3#include <algorithm>
4#include <cassert>
Matthew Barth155c34f2016-10-18 14:33:17 -05005#include "argument.hpp"
vishwa13555bd2015-11-10 12:10:38 -06006
7ArgumentParser::ArgumentParser(int argc, char** argv)
8{
9 int option = 0;
10 while(-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
11 {
12 if ((option == '?') || (option == 'h'))
13 {
14 usage(argv);
15 exit(-1);
16 }
17
18 auto i = &options[0];
19 while ((i->val != option) && (i->val != 0)) ++i;
20
21 if (i->val)
22 arguments[i->name] = (i->has_arg ? optarg : true_string);
23 }
24}
25
26const std::string& ArgumentParser::operator[](const std::string& opt)
27{
28 auto i = arguments.find(opt);
29 if (i == arguments.end())
30 {
31 return empty_string;
32 }
33 else
34 {
35 return i->second;
36 }
37}
38
39void ArgumentParser::usage(char** argv)
40{
41 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
42 std::cerr << "Options:" << std::endl;
43 std::cerr << " --eeprom=<eeprom file path> Absolute file name of eeprom"
44 << std::endl;
45 std::cerr << " --fruid=<FRU ID> valid fru id in integer"
46 << std::endl;
47 std::cerr << " --help display help"
48 << std::endl;
49}
50
51const option ArgumentParser::options[] =
52{
53 { "eeprom", required_argument, NULL, 'e' },
54 { "fruid", required_argument, NULL, 'f' },
55 { "help", no_argument, NULL, 'h' },
56 { 0, 0, 0, 0},
57};
58
59const char* ArgumentParser::optionstr = "e:f:?h";
60
61const std::string ArgumentParser::true_string = "true";
62const std::string ArgumentParser::empty_string = "";