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