Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <getopt.h> |
| 4 | |
| 5 | #include <map> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace led |
| 11 | { |
| 12 | /** @brief Class - Encapsulates parsing command line options and |
| 13 | * populating arguments |
| 14 | */ |
| 15 | class ArgumentParser |
| 16 | { |
| 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; |
| 24 | |
| 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 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) |
| 32 | ArgumentParser(int argc, char* argv[]); |
| 33 | |
| 34 | /** @brief Given a option, returns its argument(optarg) */ |
| 35 | const std::string& operator[](const std::string& opt); |
| 36 | |
| 37 | /** @brief Displays usage */ |
| 38 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) |
| 39 | static void usage(char* argv[]); |
| 40 | |
| 41 | private: |
| 42 | /** @brief Option to argument mapping */ |
| 43 | std::map<const std::string, std::string> arguments; |
| 44 | |
| 45 | /** @brief Array of struct options as needed by getopt_long */ |
| 46 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) |
| 47 | static inline const option options[] = { |
| 48 | {"path", required_argument, nullptr, 'p'}, |
| 49 | {"help", no_argument, nullptr, 'h'}, |
| 50 | {nullptr, 0, nullptr, 0}, |
| 51 | }; |
| 52 | |
| 53 | /** @brief optstring as needed by getopt_long */ |
| 54 | static inline const char* const optionstr = "p:?h"; |
| 55 | }; |
| 56 | |
| 57 | } // namespace led |
| 58 | } // namespace phosphor |