blob: 7917b8a8ec1a7ecb875a2ede36d58f63cf84ef50 [file] [log] [blame]
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05301#pragma once
2
3#include <getopt.h>
Patrick Venturedace6802018-11-01 16:52:10 -07004
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05305#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;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053024
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);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053032
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);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053040
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);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053046
Patrick Venturedace6802018-11-01 16:52:10 -070047 /** @brief Set to 'true' when an option is passed */
48 static const std::string trueString;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053049
Patrick Venturedace6802018-11-01 16:52:10 -070050 /** @brief Set to '' when an option is not passed */
51 static const std::string emptyString;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053052
Patrick Venturedace6802018-11-01 16:52:10 -070053 private:
54 /** @brief Option to argument mapping */
55 std::map<const std::string, std::string> arguments;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053056
Patrick Venturedace6802018-11-01 16:52:10 -070057 /** @brief Array of struct options as needed by getopt_long */
58 static const option options[];
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053059
Patrick Venturedace6802018-11-01 16:52:10 -070060 /** @brief optstring as needed by getopt_long */
61 static const char* optionStr;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053062};
63
64} // namespace gpio
65} // namespace phosphor