blob: 81876da1ea8e11c2bb4a7b7cfd6a6e629e8061ac [file] [log] [blame]
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +05301#ifndef __ARGUMENT_H
2#define __ARGUMENT_H
3#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 /** @brief Set to 'true' when an option is passed */
40 static const std::string true_string;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053041
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093042 /** @brief Set to '' when an option is not passed */
43 static const std::string empty_string;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053044
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093045 private:
46 /** @brief Option to argument mapping */
47 std::map<const std::string, std::string> arguments;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053048
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093049 /** @brief Array of struct options as needed by getopt_long */
50 static const option options[];
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053051
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093052 /** @brief optstring as needed by getopt_long */
53 static const char* optionstr;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053054};
55
56} // namespace led
57} // namespace phosphor
58
59#endif