@@ -47,6 +47,7 @@ struct ArgvOption
47
47
std::string description;
48
48
std::string defaultValue;
49
49
std::function<void (std::string_view, SettingsType &)> parse;
50
+ std::function<std::string(const SettingsType &)> toString;
50
51
};
51
52
52
53
template <typename SettingsType>
@@ -109,6 +110,18 @@ class ArgvInfo
109
110
return result;
110
111
}
111
112
113
+ std::string toString (const SettingsType &settings) const
114
+ {
115
+ auto stream = std::ostringstream ();
116
+
117
+ for (const auto &[key, option] : _options)
118
+ {
119
+ stream << " \n --" << key << " : " << option.toString (settings);
120
+ }
121
+
122
+ return stream.str ();
123
+ }
124
+
112
125
private:
113
126
std::string _description;
114
127
ArgvOptions<SettingsType> _options;
@@ -119,7 +132,7 @@ struct ArgvReflector;
119
132
120
133
template <typename T>
121
134
concept ReflectedArgvOption = std::same_as<std::string, decltype(ArgvReflector<T>::getType())>
122
- && std::same_as<std::string, decltype(ArgvReflector<T>::display (T()))>
135
+ && std::same_as<std::string, decltype(ArgvReflector<T>::toString (T()))>
123
136
&& std::same_as<T, decltype(ArgvReflector<T>::parse(std::string_view()))>;
124
137
125
138
template <>
@@ -130,7 +143,7 @@ struct ArgvReflector<bool>
130
143
return " boolean" ;
131
144
}
132
145
133
- static std::string display (bool value)
146
+ static std::string toString (bool value)
134
147
{
135
148
return value ? " true" : " false" ;
136
149
}
@@ -155,7 +168,7 @@ struct ArgvReflector<T>
155
168
return std::is_integral_v<T> ? " integer" : " number" ;
156
169
}
157
170
158
- static std::string display (const T &value)
171
+ static std::string toString (const T &value)
159
172
{
160
173
return fmt::format (" {}" , value);
161
174
}
@@ -174,7 +187,7 @@ struct ArgvReflector<std::string>
174
187
return " string" ;
175
188
}
176
189
177
- static std::string display (const std::string &value)
190
+ static std::string toString (const std::string &value)
178
191
{
179
192
return value;
180
193
}
@@ -203,7 +216,7 @@ class ArgvOptionBuilder
203
216
template <ReflectedArgvOption T>
204
217
ArgvOptionBuilder defaultValue (const T &value)
205
218
{
206
- _option->defaultValue = ArgvReflector<T>::display (value);
219
+ _option->defaultValue = ArgvReflector<T>::toString (value);
207
220
return *this ;
208
221
}
209
222
@@ -221,7 +234,8 @@ using GetOptionType = std::decay_t<std::remove_pointer_t<std::invoke_result_t<Ge
221
234
222
235
template <typename GetterType, typename SettingsType>
223
236
concept ArgvOptionGetter =
224
- std::invocable<GetterType, SettingsType &> && std::is_pointer_v<std::invoke_result_t <GetterType, SettingsType &>>
237
+ std::invocable<GetterType, SettingsType &> && std::invocable<GetterType, const SettingsType &>
238
+ && std::is_pointer_v<std::invoke_result_t <GetterType, SettingsType &>>
225
239
&& ReflectedArgvOption<GetOptionType<GetterType, SettingsType>>;
226
240
227
241
template <typename SettingsType>
@@ -249,6 +263,7 @@ class ArgvBuilder
249
263
option.key = std::move (key);
250
264
option.type = Reflector::getType ();
251
265
option.parse = [=](auto data, auto &settings) { *getOptionPtr (settings) = Reflector::parse (data); };
266
+ option.toString = [=](const auto &settings) { return Reflector::toString (*getOptionPtr (settings)); };
252
267
253
268
return ArgvOptionBuilder<SettingsType>(option);
254
269
}
@@ -294,4 +309,10 @@ SettingsType parseArgvAs(int argc, const char **argv)
294
309
auto map = parseArgv (argc, argv);
295
310
return parseArgvAs<SettingsType>(map);
296
311
}
312
+
313
+ template <ReflectedArgvSettings SettingsType>
314
+ std::string stringifyArgvSettings (const SettingsType &settings)
315
+ {
316
+ return reflectArgvSettings<SettingsType>().toString (settings);
317
+ }
297
318
}
0 commit comments