Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 1 | #include "argument.hpp" |
| 2 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <cassert> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <iterator> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 7 | |
| 8 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 9 | { |
| 10 | int option = 0; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 11 | while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL))) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 12 | { |
| 13 | if ((option == '?') || (option == 'h')) |
| 14 | { |
| 15 | usage(argv); |
| 16 | exit(-1); |
| 17 | } |
| 18 | |
| 19 | auto i = &options[0]; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 20 | while ((i->val != option) && (i->val != 0)) |
| 21 | ++i; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 22 | |
| 23 | if (i->val) |
| 24 | arguments[i->name] = (i->has_arg ? optarg : true_string); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | const 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 | |
| 41 | void 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 Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 46 | << std::endl; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 47 | std::cerr << " --fruid=<FRU ID> valid fru id in integer" |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 48 | << std::endl; |
| 49 | std::cerr << " --help display help" << std::endl; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 52 | const 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}, |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | const char* ArgumentParser::optionstr = "e:f:?h"; |
| 60 | |
| 61 | const std::string ArgumentParser::true_string = "true"; |
| 62 | const std::string ArgumentParser::empty_string = ""; |