1 #pragma once
2 
3 #include <getopt.h>
4 
5 #include <map>
6 #include <string>
7 
8 namespace witherspoon
9 {
10 namespace power
11 {
12 
13 /** @brief Class - Encapsulates parsing command line options and
14  *                 populating arguments
15  */
16 class ArgumentParser
17 {
18   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;
25 
26     /** @brief Constructs Argument object
27      *
28      *  @param[in] argc - the main function's argc passed as is
29      *  @param[in] argv - the main function's argv passed as is
30      *  @return Object constructed
31      */
32     ArgumentParser(int argc, char** argv);
33 
34     /** @brief Given an option, returns its argument(optarg)
35      *
36      *  @param[in] opt - command line option string
37      *
38      *  @return argument which is a standard optarg
39      */
40     const std::string& operator[](const std::string& opt);
41 
42     /** @brief Displays usage
43      *
44      *  @param[in] argv - the main function's argv passed as is
45      */
46     static void usage(char** argv);
47 
48     /** @brief Set to 'true' when an option is passed */
49     static const std::string trueString;
50 
51     /** @brief Set to '' when an option is not passed */
52     static const std::string emptyString;
53 
54   private:
55     /** @brief Option to argument mapping */
56     std::map<const std::string, std::string> arguments;
57 
58     /** @brief Array of struct options as needed by getopt_long */
59     static const option options[];
60 
61     /** @brief optstring as needed by getopt_long */
62     static const char* optionStr;
63 };
64 
65 } // namespace power
66 } // namespace witherspoon
67