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