Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 1 | #ifndef __ARGUMENT_H |
| 2 | #define __ARGUMENT_H |
| 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 | /** @brief Set to 'true' when an option is passed */ |
| 40 | static const std::string true_string; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 41 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 42 | /** @brief Set to '' when an option is not passed */ |
| 43 | static const std::string empty_string; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 44 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 45 | private: |
| 46 | /** @brief Option to argument mapping */ |
| 47 | std::map<const std::string, std::string> arguments; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 48 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 49 | /** @brief Array of struct options as needed by getopt_long */ |
| 50 | static const option options[]; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 51 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 52 | /** @brief optstring as needed by getopt_long */ |
| 53 | static const char* optionstr; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // namespace led |
| 57 | } // namespace phosphor |
| 58 | |
| 59 | #endif |