blob: 00f375565b66fe05b78704c396464cd774f45d43 [file] [log] [blame]
Matt Spinler8b633b72017-06-02 12:35:59 -05001#pragma once
2
3#include <getopt.h>
4#include <map>
5#include <string>
6
7namespace phosphor
8{
9namespace unit
10{
11namespace failure
12{
13
14/** @brief Class - Encapsulates parsing command line options and
15 * populating arguments
16 */
17class ArgumentParser
18{
Ed Tanous167e2372018-05-07 11:59:10 -070019 public:
20 ArgumentParser() = delete;
21 ~ArgumentParser() = default;
22 ArgumentParser(const ArgumentParser&) = delete;
23 ArgumentParser& operator=(const ArgumentParser&) = delete;
24 ArgumentParser(ArgumentParser&&) = default;
25 ArgumentParser& operator=(ArgumentParser&&) = default;
Matt Spinler8b633b72017-06-02 12:35:59 -050026
Ed Tanous167e2372018-05-07 11:59:10 -070027 /** @brief Contructs Argument object
28 *
29 * @param argc - the main function's argc passed as is
30 * @param argv - the main function's argv passed as is
31 * @return Object constructed
32 */
33 ArgumentParser(int argc, char** argv);
Matt Spinler8b633b72017-06-02 12:35:59 -050034
Ed Tanous167e2372018-05-07 11:59:10 -070035 /** @brief Given an option, returns its argument(optarg)
36 *
37 * @param opt - command line option string
38 *
39 * @return argument which is a standard optarg
40 */
41 const std::string& operator[](const std::string& opt);
Matt Spinler8b633b72017-06-02 12:35:59 -050042
Ed Tanous167e2372018-05-07 11:59:10 -070043 /** @brief Displays usage
44 *
45 * @param argv - the main function's argv passed as is
46 */
47 static void usage(char** argv);
Matt Spinler8b633b72017-06-02 12:35:59 -050048
Ed Tanous167e2372018-05-07 11:59:10 -070049 /** @brief Set to 'true' when an option is passed */
50 static const std::string trueString;
Matt Spinler8b633b72017-06-02 12:35:59 -050051
Ed Tanous167e2372018-05-07 11:59:10 -070052 /** @brief Set to '' when an option is not passed */
53 static const std::string emptyString;
Matt Spinler8b633b72017-06-02 12:35:59 -050054
Ed Tanous167e2372018-05-07 11:59:10 -070055 private:
56 /** @brief Option to argument mapping */
57 std::map<const std::string, std::string> arguments;
Matt Spinler8b633b72017-06-02 12:35:59 -050058
Ed Tanous167e2372018-05-07 11:59:10 -070059 /** @brief Array of struct options as needed by getopt_long */
60 static const option options[];
Matt Spinler8b633b72017-06-02 12:35:59 -050061
Ed Tanous167e2372018-05-07 11:59:10 -070062 /** @brief optstring as needed by getopt_long */
63 static const char* optionStr;
Matt Spinler8b633b72017-06-02 12:35:59 -050064};
Matt Spinler8b633b72017-06-02 12:35:59 -050065}
66}
67}