34 #ifndef VP_JSON_ARGUMENT_PARSER_H
35 #define VP_JSON_ARGUMENT_PARSER_H
37 #include <visp3/core/vpConfig.h>
39 #if defined(VISP_HAVE_NLOHMANN_JSON)
40 #include <nlohmann/json.hpp>
41 #include <visp3/core/vpException.h>
53 nlohmann::json convertCommandLineArgument(
const std::string &arg)
55 nlohmann::json j = nlohmann::json::parse(arg);
65 nlohmann::json convertCommandLineArgument<std::string>(
const std::string &arg)
67 nlohmann::json j = arg;
155 vpJsonArgumentParser(
const std::string &description,
const std::string &jsonFileArgumentName,
const std::string &nestSeparator);
164 std::string help()
const;
184 const auto getter = [name,
this](nlohmann::json &j,
bool create) -> nlohmann::json * {
186 nlohmann::json *f = &j;
188 std::string name_copy = name;
190 while ((pos = name_copy.find(nestSeparator)) != std::string::npos) {
191 token = name_copy.substr(0, pos);
193 name_copy.erase(0, pos + nestSeparator.length());
194 if (create && !f->contains(token)) {
197 else if (!f->contains(token)) {
202 if (create && !f->contains(name_copy)) {
203 (*f)[name_copy] = {};
205 else if (!f->contains(name_copy)) {
208 f = &(f->at(name_copy));
212 parsers[name] = [¶meter, required, getter, name](nlohmann::json &j) {
213 const nlohmann::json *field = getter(j,
false);
214 const bool fieldHasNoValue = field ==
nullptr || (field !=
nullptr && field->is_null());
215 if (required && fieldHasNoValue) {
216 std::stringstream ss;
217 ss <<
"Argument " << name <<
" is required, but no value was provided" << std::endl;
220 else if (!fieldHasNoValue) {
221 field->get_to(parameter);
225 updaters[name] = [getter](nlohmann::json &j,
const std::string &s) {
226 nlohmann::json *field = getter(j,
true);
227 *field = convertCommandLineArgument<T>(s);
230 helpers[name] = [help, parameter, required]() -> std::string {
231 std::stringstream ss;
232 nlohmann::json repr = parameter;
233 ss << help << std::endl <<
"Default: " << repr;
235 ss << std::endl <<
"Required";
238 ss << std::endl <<
"Optional";
243 nlohmann::json *exampleField = getter(exampleJson,
true);
244 *exampleField = parameter;
255 void parse(
int argc,
const char *argv[]);
259 std::string description;
260 std::string jsonFileArgumentName;
261 std::string nestSeparator;
262 std::map<std::string, std::function<void(nlohmann::json &)>> parsers;
263 std::map<std::string, std::function<void(nlohmann::json &,
const std::string &)>> updaters;
264 std::map<std::string, std::function<std::string()>> helpers;
265 nlohmann::json exampleJson;
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
Command line argument parsing with support for JSON files. If a JSON file is supplied,...
vpJsonArgumentParser & addArgument(const std::string &name, T ¶meter, const bool required=true, const std::string &help="No description")
Add an argument that can be provided by the user, either via command line or through the json file.