1 #ifndef ARGUMENT_H 2 #define ARGUMENT_H 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 ArgumentParser(int argc, char** argv); 32 33 /** @brief Given a option, returns its argument(optarg) */ 34 const std::string& operator[](const std::string& opt); 35 36 /** @brief Displays usage */ 37 static void usage(char** argv); 38 39 private: 40 /** @brief Option to argument mapping */ 41 std::map<const std::string, std::string> arguments; 42 43 /** @brief Array of struct options as needed by getopt_long */ 44 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) 45 static inline const option options[] = { 46 {"path", required_argument, nullptr, 'p'}, 47 {"help", no_argument, nullptr, 'h'}, 48 {nullptr, 0, nullptr, 0}, 49 }; 50 51 /** @brief optstring as needed by getopt_long */ 52 static inline const char* const optionstr = "p:?h"; 53 }; 54 55 } // namespace led 56 } // namespace phosphor 57 58 #endif 59