blob: 6afb762586c93d9b23273b5ce885434adf991db5 [file] [log] [blame]
Andrew Jefferyb24c42f2023-02-07 15:46:20 +10301#ifndef ARGUMENT_H
2#define ARGUMENT_H
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +05303#include <getopt.h>
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +09304
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +05305#include <map>
6#include <string>
7
8namespace phosphor
9{
10namespace led
11{
12/** @brief Class - Encapsulates parsing command line options and
13 * populating arguments
14 */
15class ArgumentParser
16{
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093017 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 Subbanna835571e2016-11-30 11:29:30 +053024
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093025 /** @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 Subbanna835571e2016-11-30 11:29:30 +053032
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093033 /** @brief Given a option, returns its argument(optarg) */
34 const std::string& operator[](const std::string& opt);
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053035
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093036 /** @brief Displays usage */
37 static void usage(char** argv);
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053038
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093039 private:
40 /** @brief Option to argument mapping */
41 std::map<const std::string, std::string> arguments;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053042
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093043 /** @brief Array of struct options as needed by getopt_long */
Andrew Jefferycbdbed52023-02-07 16:26:00 +103044 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
Andrew Jeffery1d88e682023-02-03 16:10:44 +103045 static inline const option options[] = {
46 {"path", required_argument, nullptr, 'p'},
47 {"help", no_argument, nullptr, 'h'},
48 {nullptr, 0, nullptr, 0},
49 };
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053050
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093051 /** @brief optstring as needed by getopt_long */
Andrew Jeffery84c80542023-02-07 16:39:11 +103052 static inline const char* const optionstr = "p:?h";
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053053};
54
55} // namespace led
56} // namespace phosphor
57
58#endif