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