blob: 7917b8a8ec1a7ecb875a2ede36d58f63cf84ef50 [file] [log] [blame]
Matt Spinlere3b859c2017-05-25 11:15:43 -05001#pragma once
2
3#include <getopt.h>
Patrick Venturedace6802018-11-01 16:52:10 -07004
Matt Spinlere3b859c2017-05-25 11:15:43 -05005#include <map>
6#include <string>
7
8namespace phosphor
9{
10namespace gpio
11{
12/** @brief Class - Encapsulates parsing command line options and
13 * populating arguments
14 */
15class ArgumentParser
16{
Patrick Venturedace6802018-11-01 16:52:10 -070017 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;
Matt Spinlere3b859c2017-05-25 11:15:43 -050024
Patrick Venturedace6802018-11-01 16:52:10 -070025 /** @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);
Matt Spinlere3b859c2017-05-25 11:15:43 -050032
Patrick Venturedace6802018-11-01 16:52:10 -070033 /** @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 */
39 const std::string& operator[](const std::string& opt);
Matt Spinlere3b859c2017-05-25 11:15:43 -050040
Patrick Venturedace6802018-11-01 16:52:10 -070041 /** @brief Displays usage
42 *
43 * @param argv - the main function's argv passed as is
44 */
45 static void usage(char** argv);
Matt Spinlere3b859c2017-05-25 11:15:43 -050046
Patrick Venturedace6802018-11-01 16:52:10 -070047 /** @brief Set to 'true' when an option is passed */
48 static const std::string trueString;
Matt Spinlere3b859c2017-05-25 11:15:43 -050049
Patrick Venturedace6802018-11-01 16:52:10 -070050 /** @brief Set to '' when an option is not passed */
51 static const std::string emptyString;
Matt Spinlere3b859c2017-05-25 11:15:43 -050052
Patrick Venturedace6802018-11-01 16:52:10 -070053 private:
54 /** @brief Option to argument mapping */
55 std::map<const std::string, std::string> arguments;
Matt Spinlere3b859c2017-05-25 11:15:43 -050056
Patrick Venturedace6802018-11-01 16:52:10 -070057 /** @brief Array of struct options as needed by getopt_long */
58 static const option options[];
Matt Spinlere3b859c2017-05-25 11:15:43 -050059
Patrick Venturedace6802018-11-01 16:52:10 -070060 /** @brief optstring as needed by getopt_long */
61 static const char* optionStr;
Matt Spinlere3b859c2017-05-25 11:15:43 -050062};
63
64} // namespace gpio
65} // namespace phosphor