Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <getopt.h> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 4 | |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 5 | #include <map> |
| 6 | #include <string> |
| 7 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 8 | namespace phosphor |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 9 | { |
| 10 | namespace power |
| 11 | { |
| 12 | |
| 13 | /** @brief Class - Encapsulates parsing command line options and |
| 14 | * populating arguments |
| 15 | */ |
| 16 | class ArgumentParser |
| 17 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [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; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 25 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 26 | /** @brief Constructs Argument object |
| 27 | * |
| 28 | * @param[in] argc - the main function's argc passed as is |
| 29 | * @param[in] argv - the main function's argv passed as is |
| 30 | * @return Object constructed |
| 31 | */ |
| 32 | ArgumentParser(int argc, char** argv); |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 33 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 34 | /** @brief Given an option, returns its argument(optarg) |
| 35 | * |
| 36 | * @param[in] opt - command line option string |
| 37 | * |
| 38 | * @return argument which is a standard optarg |
| 39 | */ |
| 40 | const std::string& operator[](const std::string& opt); |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 41 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 42 | /** @brief Displays usage |
| 43 | * |
| 44 | * @param[in] argv - the main function's argv passed as is |
| 45 | */ |
| 46 | static void usage(char** argv); |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 47 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 48 | /** @brief Set to 'true' when an option is passed */ |
| 49 | static const std::string trueString; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 50 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 51 | /** @brief Set to '' when an option is not passed */ |
| 52 | static const std::string emptyString; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 53 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 54 | private: |
| 55 | /** @brief Option to argument mapping */ |
| 56 | std::map<const std::string, std::string> arguments; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 57 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 58 | /** @brief Array of struct options as needed by getopt_long */ |
| 59 | static const option options[]; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 60 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 61 | /** @brief optstring as needed by getopt_long */ |
| 62 | static const char* optionStr; |
Matt Spinler | 7cde008 | 2017-07-24 13:48:44 -0500 | [diff] [blame] | 63 | }; |
| 64 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 65 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 66 | } // namespace phosphor |