blob: df61de1ffc1098dc37f3e27fbe95a7c6eff95bc2 [file] [log] [blame]
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05301#pragma once
2
3#include <getopt.h>
Patrick Venture8f6c5152018-09-11 17:45:33 -07004
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05305#include <map>
6#include <string>
William A. Kennington III5d307182018-01-23 22:00:55 -08007#include <vector>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05308
9namespace phosphor
10{
11namespace watchdog
12{
13/** @brief Class ArgumentParser - Encapsulates parsing command line options
14 * and populating arguments
15 */
16class ArgumentParser
17{
Patrick Venture8f6c5152018-09-11 17:45:33 -070018 public:
19 ArgumentParser() = delete;
20 ~ArgumentParser() = default;
21 ArgumentParser(const ArgumentParser&) = delete;
22 ArgumentParser& operator=(const ArgumentParser&) = delete;
23 ArgumentParser(ArgumentParser&&) = default;
24 ArgumentParser& operator=(ArgumentParser&&) = default;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053025
Patrick Venture8f6c5152018-09-11 17:45:33 -070026 /** @brief Constructs ArgumentParser object
27 *
28 * @param argc - the main function's argc passed as is
29 * @param argv - the main function's argv passed as is
30 * @return Object constructed
31 */
32 ArgumentParser(int argc, char* const argv[]);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053033
Patrick Venture8f6c5152018-09-11 17:45:33 -070034 /** @brief Given an option, returns its argument(optarg)
35 *
36 * @param opt - command line option string
37 *
38 * @return argument which is a standard optarg
39 */
40 const std::vector<std::string>& operator[](const std::string& opt);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053041
Patrick Venture8f6c5152018-09-11 17:45:33 -070042 /** @brief Displays usage
43 *
44 * @param argv - the main function's argv passed as is
45 */
46 static void usage(char* const argv[]);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053047
Patrick Venture8f6c5152018-09-11 17:45:33 -070048 /** @brief Set to 'true' when an option is passed */
49 static const std::string trueString;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053050
Patrick Venture8f6c5152018-09-11 17:45:33 -070051 private:
52 /** @brief Option to argument mapping */
53 std::map<const std::string, std::vector<std::string>> arguments;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053054
Patrick Venture8f6c5152018-09-11 17:45:33 -070055 /** @brief Array of struct options as needed by getopt_long */
56 static const option options[];
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053057
Patrick Venture8f6c5152018-09-11 17:45:33 -070058 /** @brief optstring as needed by getopt_long */
59 static const char* optionStr;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053060};
61
62} // namespace watchdog
63} // namespace phosphor