Andrew Jeffery | b24c42f | 2023-02-07 15:46:20 +1030 | [diff] [blame] | 1 | #ifndef ARGUMENT_H |
| 2 | #define ARGUMENT_H |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 3 | #include <getopt.h> |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 4 | |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 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 | { |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [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; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 24 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [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); |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 32 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 33 | /** @brief Given a option, returns its argument(optarg) */ |
| 34 | const std::string& operator[](const std::string& opt); |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 35 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 36 | /** @brief Displays usage */ |
| 37 | static void usage(char** argv); |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 38 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 39 | private: |
| 40 | /** @brief Option to argument mapping */ |
| 41 | std::map<const std::string, std::string> arguments; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 42 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 43 | /** @brief Array of struct options as needed by getopt_long */ |
Andrew Jeffery | cbdbed5 | 2023-02-07 16:26:00 +1030 | [diff] [blame] | 44 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) |
Andrew Jeffery | 1d88e68 | 2023-02-03 16:10:44 +1030 | [diff] [blame] | 45 | static inline const option options[] = { |
| 46 | {"path", required_argument, nullptr, 'p'}, |
| 47 | {"help", no_argument, nullptr, 'h'}, |
| 48 | {nullptr, 0, nullptr, 0}, |
| 49 | }; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 50 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 51 | /** @brief optstring as needed by getopt_long */ |
Andrew Jeffery | 84c8054 | 2023-02-07 16:39:11 +1030 | [diff] [blame] | 52 | static inline const char* const optionstr = "p:?h"; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | } // namespace led |
| 56 | } // namespace phosphor |
| 57 | |
| 58 | #endif |