vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <iterator> |
| 3 | #include <algorithm> |
| 4 | #include <cassert> |
Matthew Barth | 155c34f | 2016-10-18 14:33:17 -0500 | [diff] [blame] | 5 | #include "argument.hpp" |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 6 | |
| 7 | ArgumentParser::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 | |
| 26 | const 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 | |
| 39 | void 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 | |
| 51 | const 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 | |
| 59 | const char* ArgumentParser::optionstr = "e:f:?h"; |
| 60 | |
| 61 | const std::string ArgumentParser::true_string = "true"; |
| 62 | const std::string ArgumentParser::empty_string = ""; |