Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <getopt.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 5 | #include <map> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace ncsi |
| 13 | { |
| 14 | /** @brief Class - Encapsulates parsing command line options and |
| 15 | * populating arguments |
| 16 | */ |
| 17 | class ArgumentParser |
| 18 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 19 | public: |
| 20 | ArgumentParser() = delete; |
| 21 | ~ArgumentParser() = default; |
| 22 | ArgumentParser(const ArgumentParser&) = delete; |
| 23 | ArgumentParser& operator=(const ArgumentParser&) = delete; |
| 24 | ArgumentParser(ArgumentParser&&) = default; |
| 25 | ArgumentParser& operator=(ArgumentParser&&) = default; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 26 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 27 | /** @brief Constructs Argument object |
| 28 | * |
| 29 | * @param argc - the main function's argc passed as is |
| 30 | * @param argv - the main function's argv passed as is |
| 31 | * @return Object constructed |
| 32 | */ |
| 33 | ArgumentParser(int argc, char** argv); |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 34 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | /** @brief Given an option, returns its argument(optarg) |
| 36 | * |
| 37 | * @param opt - command line option string |
| 38 | * |
| 39 | * @return argument which is a standard optarg |
| 40 | */ |
| 41 | const std::string& operator[](const std::string& opt); |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 42 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | /** @brief Displays usage |
| 44 | * |
| 45 | * @param argv - the main function's argv passed as is |
| 46 | */ |
| 47 | static void usage(char** argv); |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 48 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 49 | /** @brief Set to 'true' when an option is passed */ |
| 50 | static const std::string trueString; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 51 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | /** @brief Set to '' when an option is not passed */ |
| 53 | static const std::string emptyString; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 54 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | private: |
| 56 | /** @brief Option to argument mapping */ |
| 57 | std::map<const std::string, std::string> arguments; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 58 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | /** @brief Array of struct options as needed by getopt_long */ |
| 60 | static const option options[]; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 61 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | /** @brief optstring as needed by getopt_long */ |
| 63 | static const char* optionStr; |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace ncsi |
| 67 | } // namespace network |
| 68 | } // namespace phosphor |