Visual Servoing Platform
version 3.6.1 under development (2024-11-14)
|
#include <visp3/io/vpJsonArgumentParser.h>
Public Types | |
enum | vpJsonArgumentType { WITH_FIELD = 0 , FLAG = 1 } |
Public Member Functions | |
vpJsonArgumentParser (const std::string &description, const std::string &jsonFileArgumentName, const std::string &nestSeparator) | |
std::string | help () const |
template<typename T > | |
vpJsonArgumentParser & | addArgument (const std::string &name, T ¶meter, const bool required=true, const std::string &help="No description") |
vpJsonArgumentParser & | addFlag (const std::string &name, bool ¶meter, const std::string &help="No description") |
void | parse (int argc, const char *argv[]) |
Command line argument parsing with support for JSON files. If a JSON file is supplied, it is parsed and command line arguments take precedence over values given in the file.
This argument parser can take any number and type of arguments, as long they can be serialized to and from JSON.
A very basic program that uses both a JSON file and command line arguments can be found below:
Compiling this sample and calling the program with the arguments from the command line would yield:
Here the arguments are specified from the command line. Since the "string" argument is optional, it does not have to be specified.
For programs with more arguments it is helpful to use a JSON file that contains a base configuration. For the program above, a JSON file could look like:
we could then call the program with:
The values contained in the JSON file can be overridden with command line arguments
The program can also be called with the "-h" or "--help" argument to display the help associated to the arguments, as well as an example json configuration file
Definition at line 137 of file vpJsonArgumentParser.h.
Enumerator | |
---|---|
WITH_FIELD | |
FLAG |
Definition at line 141 of file vpJsonArgumentParser.h.
vpJsonArgumentParser::vpJsonArgumentParser | ( | const std::string & | description, |
const std::string & | jsonFileArgumentName, | ||
const std::string & | nestSeparator | ||
) |
Create a new argument parser, that can take into account both a JSON configuration file and command line arguments.
json namespace shortcut
description | Description of the program tied to this parser |
jsonFileArgumentName | Name of the argument that points to the JSON file to load |
nestSeparator | Delimiter that is used map a nested json object to a command line argument. For example, with a delimiter set to "/", the command line argument "a/b" will map to the json key "b" in the following json document: {
"a": {
"b": 10.0
},
"otherArgument": false
}
|
Definition at line 41 of file vpJsonArgumentParser.cpp.
References vpException::badValue.
|
inline |
Add an argument that can be provided by the user, either via command line or through the json file.
T | Type of the argument to pass. The methods from_json(const nlohmann::json&, T&) and to_json(nlohmann::json&, const T&) must be defined. This is the case for most basic types or stl containers. For your own types, you should define the method. |
name | Name of the parameter that will be used to look up the argument values when parsing command line arguments or the json file. This name may contain the nestSeparator, in which case the look up in the JSON file will seek a nested object to parse. |
parameter | Reference where the parsed value will be stored. It is modified when calling parse. |
required | Whether this argument is required. If it is, it should be specified either through command line or through the json file. If not, then you should take special care to initialize parameter with a sensible value. |
help | The description of the argument. |
Definition at line 189 of file vpJsonArgumentParser.h.
References vpException::badValue.
vpJsonArgumentParser & vpJsonArgumentParser::addFlag | ( | const std::string & | name, |
bool & | parameter, | ||
const std::string & | help = "No description" |
||
) |
Add an argument that acts as a flag when specified on the command line. When this flag is specified, the boolean passed in argument will be inverted.
name | Name of the flag. |
parameter | The boolean to modify when the flag is specified |
help | The description of the argument. |
Definition at line 97 of file vpJsonArgumentParser.cpp.
std::string vpJsonArgumentParser::help | ( | ) | const |
Generate a help message, containing the description of the arguments, their default value and whether they are required or not. This message also contains an example json file, generated from the default values of the arguments. This method is called when running the program with the "-h" or "--help" arguments.
Definition at line 60 of file vpJsonArgumentParser.cpp.
void vpJsonArgumentParser::parse | ( | int | argc, |
const char * | argv[] | ||
) |
Parse the arguments.
argc | Number of arguments (including program name) |
argv | Arguments |
Definition at line 154 of file vpJsonArgumentParser.cpp.
References FLAG, help(), vpException::ioError, and WITH_FIELD.