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