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