42 const std::string &nestSeparator) :
43 description(description),
44 jsonFileArgumentName(jsonFileArgumentName),
45 nestSeparator(nestSeparator)
47 if (jsonFileArgumentName.empty()) {
51 if (nestSeparator.empty()) {
55 helpers[jsonFileArgumentName] = []() -> std::string {
56 return "Path to the JSON configuration file. Values in this files are loaded, and can be overriden by command line arguments.\nOptional";
64 ss <<
"Program description: " << description << std::endl;
65 ss <<
"Arguments: " << std::endl;
66 unsigned spacesBetweenArgAndDescription = 0;
67 for (
const auto &helper : helpers) {
68 if (helper.first.size() > spacesBetweenArgAndDescription) {
69 spacesBetweenArgAndDescription =
static_cast<unsigned int>(helper.first.size());
72 spacesBetweenArgAndDescription += 4;
74 for (
const auto &helper : helpers) {
75 std::stringstream argss(helper.second());
78 while (getline(argss, line,
'\n')) {
79 const unsigned lineSpace = first ? spacesBetweenArgAndDescription -
static_cast<unsigned>(helper.first.size()) : spacesBetweenArgAndDescription;
80 const std::string spaceBetweenArgAndDescription(lineSpace,
' ');
82 ss <<
"\t" << helper.first << spaceBetweenArgAndDescription << line << std::endl;
85 ss <<
"\t" << spaceBetweenArgAndDescription << line << std::endl;
92 ss <<
"Example JSON configuration file: " << std::endl << std::endl;
93 ss << exampleJson.dump(2) << std::endl;
101 const std::vector<std::string> arguments(argv + 1, argv + argc);
102 std::vector<unsigned> ignoredArguments;
103 const auto jsonFileArgumentPos = std::find(arguments.begin(), arguments.end(), jsonFileArgumentName);
105 if (jsonFileArgumentPos != arguments.end()) {
106 ignoredArguments.push_back(
static_cast<unsigned>(jsonFileArgumentPos - arguments.begin() + 1));
107 ignoredArguments.push_back(
static_cast<unsigned>(jsonFileArgumentPos - arguments.begin() + 2));
109 if (jsonFileArgumentPos == arguments.end() - 1) {
112 const std::string jsonFileName = *(jsonFileArgumentPos + 1);
113 std::ifstream jsonFile(jsonFileName);
114 if (!jsonFile.good()) {
115 std::stringstream ss;
116 ss <<
"Could not open JSON file " << jsonFileName <<
"! Make sure it exists and is readable" << std::endl;
119 j = json::parse(jsonFile);
123 for (
int i = 1; i < argc; ++i) {
124 const std::string arg = argv[i];
125 if (std::find(ignoredArguments.begin(), ignoredArguments.end(), i) != ignoredArguments.end()) {
128 if (arg ==
"-h" || arg ==
"--help") {
129 std::cout <<
help() << std::endl;
133 if (parsers.find(arg) != parsers.end()) {
135 updaters[arg](j, std::string(argv[i + 1]));
139 std::stringstream ss;
140 ss <<
"Argument " << arg <<
" was passed but no value was provided" << std::endl;
145 std::cerr <<
"Unknown parameter when parsing: " << arg << std::endl;
150 for (
const auto &parser : parsers) {
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 l...