blob: 8a25aeaea0c9ba692157acd102fabd41d76c7680 [file] [log] [blame]
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05301#pragma once
2
3#include <getopt.h>
4#include <map>
5#include <string>
William A. Kennington III5d307182018-01-23 22:00:55 -08006#include <vector>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05307
8namespace phosphor
9{
10namespace watchdog
11{
12/** @brief Class ArgumentParser - Encapsulates parsing command line options
13 * and populating arguments
14 */
15class 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
Gunnar Mills7512a122018-04-08 14:30:36 -050025 /** @brief Constructs ArgumentParser object
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053026 *
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 */
William A. Kennington III6c094a22018-01-29 12:09:29 -080031 ArgumentParser(int argc, char * const argv[]);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053032
33 /** @brief Given an option, returns its argument(optarg)
34 *
35 * @param opt - command line option string
36 *
37 * @return argument which is a standard optarg
38 */
William A. Kennington III5d307182018-01-23 22:00:55 -080039 const std::vector<std::string>& operator[](const std::string& opt);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053040
41 /** @brief Displays usage
42 *
43 * @param argv - the main function's argv passed as is
44 */
William A. Kennington III6c094a22018-01-29 12:09:29 -080045 static void usage(char * const argv[]);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053046
47 /** @brief Set to 'true' when an option is passed */
48 static const std::string trueString;
49
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053050 private:
51 /** @brief Option to argument mapping */
William A. Kennington III5d307182018-01-23 22:00:55 -080052 std::map<const std::string, std::vector<std::string> > arguments;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053053
54 /** @brief Array of struct options as needed by getopt_long */
55 static const option options[];
56
57 /** @brief optstring as needed by getopt_long */
58 static const char* optionStr;
59};
60
61} // namespace watchdog
62} // namespace phosphor