|
10 | 10 |
|
11 | 11 | namespace cmd_line_parser { |
12 | 12 |
|
13 | | - struct parser { |
14 | | - inline static const std::string empty; |
| 13 | +struct parser { |
| 14 | + inline static const std::string empty; |
15 | 15 |
|
16 | | - parser(int argc, char **argv) |
17 | | - : m_argc(argc), m_argv(argv), m_required(0) {} |
| 16 | + parser(int argc, char** argv) |
| 17 | + : m_argc(argc) |
| 18 | + , m_argv(argv) |
| 19 | + , m_required(0) {} |
18 | 20 |
|
19 | | - struct cmd { |
20 | | - std::string shorthand, value, descr; |
21 | | - bool is_boolean; |
22 | | - }; |
| 21 | + struct cmd { |
| 22 | + std::string shorthand, value, descr; |
| 23 | + bool is_boolean; |
| 24 | + }; |
23 | 25 |
|
24 | | - bool parse() { |
25 | | - if (m_argc - 1 < m_required) { return abort(); } |
26 | | - for (size_t i{1}, k{0}; i != m_argc; ++i, ++k) { |
27 | | - std::string parsed(m_argv[i]); |
28 | | - if (parsed == "-h" || parsed == "--help") { return abort(); } |
29 | | - size_t id{k}; |
30 | | - bool is_optional = id >= m_required; |
31 | | - if (is_optional) { |
32 | | - if (const auto it = m_shorthands.find(parsed);it == m_shorthands.end()) { |
33 | | - std::cerr << "== error: shorthand '" + parsed + "' not found" << std::endl; |
34 | | - return abort(); |
35 | | - } else { |
36 | | - id = (*it).second; |
37 | | - } |
| 26 | + bool parse() { |
| 27 | + assert(m_argc > 0); |
| 28 | + if (m_argc - 1 < m_required) return abort(); |
| 29 | + |
| 30 | + for (size_t i = 1, k = 0; i != m_argc; ++i, ++k) { |
| 31 | + std::string parsed(m_argv[i]); |
| 32 | + if (parsed == "-h" || parsed == "--help") { |
| 33 | + return abort(); |
| 34 | + } |
| 35 | + size_t id = k; |
| 36 | + bool is_optional = id >= m_required; |
| 37 | + if (is_optional) { |
| 38 | + if (const auto it = m_shorthands.find(parsed); it == m_shorthands.end()) { |
| 39 | + std::cerr << "== error: shorthand '" + parsed + "' not found" << std::endl; |
| 40 | + return abort(); |
| 41 | + } else { |
| 42 | + id = (*it).second; |
38 | 43 | } |
39 | | - assert(id < m_names.size()); |
40 | | - auto const &name = m_names[id]; |
41 | | - auto &c = m_cmds[name]; |
42 | | - if (is_optional) { |
43 | | - if (c.is_boolean) { |
44 | | - parsed = "true"; |
45 | | - } else { |
46 | | - ++i; |
47 | | - if (i == m_argc) { return abort(); } |
48 | | - parsed = m_argv[i]; |
| 44 | + } |
| 45 | + assert(id < m_names.size()); |
| 46 | + auto const& name = m_names[id]; |
| 47 | + auto& c = m_cmds[name]; |
| 48 | + if (is_optional) { |
| 49 | + if (c.is_boolean) { |
| 50 | + parsed = "true"; |
| 51 | + } else { |
| 52 | + ++i; |
| 53 | + if (i == m_argc) { |
| 54 | + return abort(); |
49 | 55 | } |
| 56 | + parsed = m_argv[i]; |
50 | 57 | } |
51 | | - c.value = parsed; |
52 | 58 | } |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | | - void help() const { |
57 | | - std::cerr << "Usage: " << m_argv[0] << " [-h,--help]"; |
58 | | - const auto print = [this](bool with_description) { |
59 | | - for (size_t i = 0; i != m_names.size(); ++i) { |
60 | | - auto const &c = m_cmds.at(m_names[i]); |
61 | | - bool is_optional = i >= m_required; |
62 | | - if (is_optional) { std::cerr << " [" << c.shorthand; } |
63 | | - if (!c.is_boolean) { std::cerr << " " << m_names[i]; } |
64 | | - if (is_optional) { std::cerr << "]"; } |
65 | | - if (with_description) { std::cerr << "\n\t" << c.descr << "\n\n"; } |
66 | | - } |
67 | | - }; |
68 | | - print(false); |
69 | | - std::cerr << "\n\n"; |
70 | | - print(true); |
71 | | - std::cerr << " [-h,--help]\n\tPrint this help text and silently exits." << std::endl; |
| 59 | + c.value = parsed; |
72 | 60 | } |
| 61 | + return true; |
| 62 | + } |
73 | 63 |
|
74 | | - bool add(std::string const &name, std::string const &descr) { |
75 | | - bool ret = m_cmds.emplace(name, cmd{empty, empty, descr, false}).second; |
76 | | - if (ret) { |
77 | | - m_names.push_back(name); |
78 | | - m_required += 1; |
| 64 | + void help() const { |
| 65 | + std::cerr << "Usage: " << m_argv[0] << " [-h,--help]"; |
| 66 | + const auto print = [this](bool with_description) { |
| 67 | + for (size_t i = 0; i != m_names.size(); ++i) { |
| 68 | + auto const& c = m_cmds.at(m_names[i]); |
| 69 | + bool is_optional = i >= m_required; |
| 70 | + if (is_optional) std::cerr << " [" << c.shorthand; |
| 71 | + if (!c.is_boolean) std::cerr << " " << m_names[i]; |
| 72 | + if (is_optional) std::cerr << "]"; |
| 73 | + if (with_description) std::cerr << "\n\t" << c.descr << "\n\n"; |
79 | 74 | } |
80 | | - return ret; |
81 | | - } |
| 75 | + }; |
| 76 | + print(false); |
| 77 | + std::cerr << "\n\n"; |
| 78 | + print(true); |
| 79 | + std::cerr << " [-h,--help]\n\tPrint this help text and silently exits." << std::endl; |
| 80 | + } |
82 | 81 |
|
83 | | - bool add(std::string const &name, std::string const &descr, std::string const &shorthand, |
84 | | - bool is_boolean = true) { |
85 | | - bool ret = |
86 | | - m_cmds.emplace(name, cmd{shorthand, is_boolean ? "false" : empty, descr, is_boolean}) |
87 | | - .second; |
88 | | - if (ret) { |
89 | | - m_names.push_back(name); |
90 | | - m_shorthands.emplace(shorthand, m_names.size() - 1); |
91 | | - } |
92 | | - return ret; |
| 82 | + bool add(std::string const& name, std::string const& descr) { |
| 83 | + bool ret = m_cmds.emplace(name, cmd{empty, empty, descr, false}).second; |
| 84 | + if (ret) { |
| 85 | + m_names.push_back(name); |
| 86 | + m_required += 1; |
93 | 87 | } |
| 88 | + return ret; |
| 89 | + } |
94 | 90 |
|
95 | | - template<typename T> |
96 | | - T get(std::string const &name) const { |
97 | | - auto it = m_cmds.find(name); |
98 | | - if (it == m_cmds.end()) throw std::runtime_error("error: '" + name + "' not found"); |
99 | | - auto const &value = (*it).second.value; |
100 | | - return parse < T > (value); |
| 91 | + bool add(std::string const& name, std::string const& descr, std::string const& shorthand, |
| 92 | + bool is_boolean = true) { |
| 93 | + bool ret = |
| 94 | + m_cmds.emplace(name, cmd{shorthand, is_boolean ? "false" : empty, descr, is_boolean}) |
| 95 | + .second; |
| 96 | + if (ret) { |
| 97 | + m_names.push_back(name); |
| 98 | + m_shorthands.emplace(shorthand, m_names.size() - 1); |
101 | 99 | } |
| 100 | + return ret; |
| 101 | + } |
102 | 102 |
|
103 | | - bool parsed(std::string const &name) const { |
104 | | - auto it = m_cmds.find(name); |
105 | | - if (it == m_cmds.end() || (*it).second.value == empty) { return false; } |
106 | | - return true; |
107 | | - } |
| 103 | + template <typename T> |
| 104 | + T get(std::string const& name) const { |
| 105 | + auto it = m_cmds.find(name); |
| 106 | + if (it == m_cmds.end()) throw std::runtime_error("error: '" + name + "' not found"); |
| 107 | + auto const& value = (*it).second.value; |
| 108 | + return parse<T>(value); |
| 109 | + } |
108 | 110 |
|
109 | | - template<typename T> |
110 | | - T parse(std::string const &value) const { |
111 | | - if constexpr (std::is_same<T, std::string>::value) { |
112 | | - return value; |
113 | | - } else if constexpr (std::is_same<T, char>::value || std::is_same<T, signed char>::value || |
114 | | - std::is_same<T, unsigned char>::value) { |
115 | | - return value.front(); |
116 | | - } else if constexpr (std::is_same<T, unsigned int>::value || std::is_same<T, int>::value || |
117 | | - std::is_same<T, unsigned short int>::value || |
118 | | - std::is_same<T, short int>::value) { |
119 | | - return std::strtol(value.c_str(), nullptr, 10); |
120 | | - } else if constexpr (std::is_same<T, unsigned long int>::value || |
121 | | - std::is_same<T, long int>::value || |
122 | | - std::is_same<T, unsigned long long int>::value || |
123 | | - std::is_same<T, long long int>::value) { |
124 | | - return std::strtoll(value.c_str(), nullptr, 10); |
125 | | - } else if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value || |
126 | | - std::is_same<T, long double>::value) { |
127 | | - return std::strtod(value.c_str(), nullptr); |
128 | | - } else if constexpr (std::is_same<T, bool>::value) { |
129 | | - std::istringstream stream(value); |
130 | | - bool ret; |
131 | | - if (value == "true" || value == "false") { |
132 | | - stream >> std::boolalpha >> ret; |
133 | | - } else { |
134 | | - stream >> std::noboolalpha >> ret; |
135 | | - } |
136 | | - return ret; |
| 111 | + bool parsed(std::string const& name) const { |
| 112 | + auto it = m_cmds.find(name); |
| 113 | + if (it == m_cmds.end() || (*it).second.value == empty) return false; |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + template <typename T> |
| 118 | + T parse(std::string const& value) const { |
| 119 | + if constexpr (std::is_same<T, std::string>::value) { |
| 120 | + return value; |
| 121 | + } else if constexpr (std::is_same<T, char>::value || std::is_same<T, signed char>::value || |
| 122 | + std::is_same<T, unsigned char>::value) { |
| 123 | + return value.front(); |
| 124 | + } else if constexpr (std::is_same<T, unsigned int>::value || std::is_same<T, int>::value || |
| 125 | + std::is_same<T, unsigned short int>::value || |
| 126 | + std::is_same<T, short int>::value) { |
| 127 | + return std::strtol(value.c_str(), nullptr, 10); |
| 128 | + } else if constexpr (std::is_same<T, unsigned long int>::value || |
| 129 | + std::is_same<T, long int>::value || |
| 130 | + std::is_same<T, unsigned long long int>::value || |
| 131 | + std::is_same<T, long long int>::value) { |
| 132 | + return std::strtoll(value.c_str(), nullptr, 10); |
| 133 | + } else if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value || |
| 134 | + std::is_same<T, long double>::value) { |
| 135 | + return std::strtod(value.c_str(), nullptr); |
| 136 | + } else if constexpr (std::is_same<T, bool>::value) { |
| 137 | + std::istringstream stream(value); |
| 138 | + bool ret; |
| 139 | + if (value == "true" || value == "false") { |
| 140 | + stream >> std::boolalpha >> ret; |
| 141 | + } else { |
| 142 | + stream >> std::noboolalpha >> ret; |
137 | 143 | } |
138 | | - assert(false); // should never happen |
139 | | - throw std::runtime_error("unsupported type"); |
| 144 | + return ret; |
140 | 145 | } |
| 146 | + assert(false); // should never happen |
| 147 | + throw std::runtime_error("unsupported type"); |
| 148 | + } |
141 | 149 |
|
142 | | - private: |
143 | | - size_t m_argc; |
144 | | - char **m_argv; |
145 | | - size_t m_required; |
146 | | - std::unordered_map<std::string, cmd> m_cmds; |
147 | | - std::unordered_map<std::string, int> m_shorthands; |
148 | | - std::vector<std::string> m_names; |
149 | | - |
150 | | - bool abort() const { |
151 | | - help(); |
152 | | - return false; |
153 | | - } |
154 | | - }; |
| 150 | +private: |
| 151 | + size_t m_argc; |
| 152 | + char** m_argv; |
| 153 | + size_t m_required; |
| 154 | + std::unordered_map<std::string, cmd> m_cmds; |
| 155 | + std::unordered_map<std::string, int> m_shorthands; |
| 156 | + std::vector<std::string> m_names; |
| 157 | + |
| 158 | + bool abort() const { |
| 159 | + help(); |
| 160 | + return false; |
| 161 | + } |
| 162 | +}; |
155 | 163 |
|
156 | 164 | } // namespace cmd_line_parser |
0 commit comments