1*b6779846SJayashree Dhanapal #pragma once
2*b6779846SJayashree Dhanapal 
3*b6779846SJayashree Dhanapal #include <getopt.h>
4*b6779846SJayashree Dhanapal 
5*b6779846SJayashree Dhanapal #include <map>
6*b6779846SJayashree Dhanapal #include <string>
7*b6779846SJayashree Dhanapal 
8*b6779846SJayashree Dhanapal namespace phosphor
9*b6779846SJayashree Dhanapal {
10*b6779846SJayashree Dhanapal namespace led
11*b6779846SJayashree Dhanapal {
12*b6779846SJayashree Dhanapal /** @brief Class - Encapsulates parsing command line options and
13*b6779846SJayashree Dhanapal  *                 populating arguments
14*b6779846SJayashree Dhanapal  */
15*b6779846SJayashree Dhanapal class ArgumentParser
16*b6779846SJayashree Dhanapal {
17*b6779846SJayashree Dhanapal   public:
18*b6779846SJayashree Dhanapal     ArgumentParser() = delete;
19*b6779846SJayashree Dhanapal     ~ArgumentParser() = default;
20*b6779846SJayashree Dhanapal     ArgumentParser(const ArgumentParser&) = delete;
21*b6779846SJayashree Dhanapal     ArgumentParser& operator=(const ArgumentParser&) = delete;
22*b6779846SJayashree Dhanapal     ArgumentParser(ArgumentParser&&) = default;
23*b6779846SJayashree Dhanapal     ArgumentParser& operator=(ArgumentParser&&) = default;
24*b6779846SJayashree Dhanapal 
25*b6779846SJayashree Dhanapal     /** @brief Constructs Argument object
26*b6779846SJayashree Dhanapal      *
27*b6779846SJayashree Dhanapal      *  @param argc - the main function's argc passed as is
28*b6779846SJayashree Dhanapal      *  @param argv - the main function's argv passed as is
29*b6779846SJayashree Dhanapal      *  @return Object constructed
30*b6779846SJayashree Dhanapal      */
31*b6779846SJayashree Dhanapal     // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
32*b6779846SJayashree Dhanapal     ArgumentParser(int argc, char* argv[]);
33*b6779846SJayashree Dhanapal 
34*b6779846SJayashree Dhanapal     /** @brief Given a option, returns its argument(optarg) */
35*b6779846SJayashree Dhanapal     const std::string& operator[](const std::string& opt);
36*b6779846SJayashree Dhanapal 
37*b6779846SJayashree Dhanapal     /** @brief Displays usage */
38*b6779846SJayashree Dhanapal     // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
39*b6779846SJayashree Dhanapal     static void usage(char* argv[]);
40*b6779846SJayashree Dhanapal 
41*b6779846SJayashree Dhanapal   private:
42*b6779846SJayashree Dhanapal     /** @brief Option to argument mapping */
43*b6779846SJayashree Dhanapal     std::map<const std::string, std::string> arguments;
44*b6779846SJayashree Dhanapal 
45*b6779846SJayashree Dhanapal     /** @brief Array of struct options as needed by getopt_long */
46*b6779846SJayashree Dhanapal     // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
47*b6779846SJayashree Dhanapal     static inline const option options[] = {
48*b6779846SJayashree Dhanapal         {"path", required_argument, nullptr, 'p'},
49*b6779846SJayashree Dhanapal         {"help", no_argument, nullptr, 'h'},
50*b6779846SJayashree Dhanapal         {nullptr, 0, nullptr, 0},
51*b6779846SJayashree Dhanapal     };
52*b6779846SJayashree Dhanapal 
53*b6779846SJayashree Dhanapal     /** @brief optstring as needed by getopt_long */
54*b6779846SJayashree Dhanapal     static inline const char* const optionstr = "p:?h";
55*b6779846SJayashree Dhanapal };
56*b6779846SJayashree Dhanapal 
57*b6779846SJayashree Dhanapal } // namespace led
58*b6779846SJayashree Dhanapal } // namespace phosphor
59