blob: 8648dddf08540908ac886621c3652fb5cfd772cc [file] [log] [blame]
Matt Spinler8b633b72017-06-02 12:35:59 -05001#pragma once
2
3#include <getopt.h>
Matt Spinlercc6ee9c2018-09-19 13:23:13 -05004
Matt Spinler8b633b72017-06-02 12:35:59 -05005#include <map>
6#include <string>
7
8namespace phosphor
9{
10namespace unit
11{
12namespace failure
13{
14
15/** @brief Class - Encapsulates parsing command line options and
16 * populating arguments
17 */
18class ArgumentParser
19{
Ed Tanous167e2372018-05-07 11:59:10 -070020 public:
21 ArgumentParser() = delete;
22 ~ArgumentParser() = default;
23 ArgumentParser(const ArgumentParser&) = delete;
24 ArgumentParser& operator=(const ArgumentParser&) = delete;
25 ArgumentParser(ArgumentParser&&) = default;
26 ArgumentParser& operator=(ArgumentParser&&) = default;
Matt Spinler8b633b72017-06-02 12:35:59 -050027
Ed Tanous167e2372018-05-07 11:59:10 -070028 /** @brief Contructs Argument object
29 *
30 * @param argc - the main function's argc passed as is
31 * @param argv - the main function's argv passed as is
32 * @return Object constructed
33 */
34 ArgumentParser(int argc, char** argv);
Matt Spinler8b633b72017-06-02 12:35:59 -050035
Ed Tanous167e2372018-05-07 11:59:10 -070036 /** @brief Given an option, returns its argument(optarg)
37 *
38 * @param opt - command line option string
39 *
40 * @return argument which is a standard optarg
41 */
42 const std::string& operator[](const std::string& opt);
Matt Spinler8b633b72017-06-02 12:35:59 -050043
Ed Tanous167e2372018-05-07 11:59:10 -070044 /** @brief Displays usage
45 *
46 * @param argv - the main function's argv passed as is
47 */
48 static void usage(char** argv);
Matt Spinler8b633b72017-06-02 12:35:59 -050049
Ed Tanous167e2372018-05-07 11:59:10 -070050 /** @brief Set to 'true' when an option is passed */
51 static const std::string trueString;
Matt Spinler8b633b72017-06-02 12:35:59 -050052
Ed Tanous167e2372018-05-07 11:59:10 -070053 /** @brief Set to '' when an option is not passed */
54 static const std::string emptyString;
Matt Spinler8b633b72017-06-02 12:35:59 -050055
Ed Tanous167e2372018-05-07 11:59:10 -070056 private:
57 /** @brief Option to argument mapping */
58 std::map<const std::string, std::string> arguments;
Matt Spinler8b633b72017-06-02 12:35:59 -050059
Ed Tanous167e2372018-05-07 11:59:10 -070060 /** @brief Array of struct options as needed by getopt_long */
61 static const option options[];
Matt Spinler8b633b72017-06-02 12:35:59 -050062
Ed Tanous167e2372018-05-07 11:59:10 -070063 /** @brief optstring as needed by getopt_long */
64 static const char* optionStr;
Matt Spinler8b633b72017-06-02 12:35:59 -050065};
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050066} // namespace failure
67} // namespace unit
68} // namespace phosphor