12#include "../Config.hpp"
24static constexpr auto multiline_literal_quote = R
"(''')";
25static constexpr auto multiline_string_quote = R
"(""")";
29CLI11_INLINE
bool is_printable(
const std::string &test_string) {
30 return std::all_of(test_string.begin(), test_string.end(), [](
char x) {
31 return (isprint(static_cast<unsigned char>(x)) != 0 || x ==
'\n' || x ==
'\t');
35CLI11_INLINE std::string
36convert_arg_for_ini(
const std::string &arg,
char stringQuote,
char literalQuote,
bool disable_multi_line) {
38 return std::string(2, stringQuote);
41 if(arg ==
"true" || arg ==
"false" || arg ==
"nan" || arg ==
"inf") {
45 if(arg.compare(0, 2,
"0x") != 0 && arg.compare(0, 2,
"0X") != 0) {
46 using CLI::detail::lexical_cast;
48 if(lexical_cast(arg, val)) {
49 if(arg.find_first_not_of(
"0123456789.-+eE") == std::string::npos) {
56 if(isprint(
static_cast<unsigned char>(arg.front())) == 0) {
57 return binary_escape_string(arg);
60 return std::string(1, stringQuote) +
"'" + stringQuote;
62 return std::string(1, literalQuote) + arg + literalQuote;
65 if(arg.front() ==
'0') {
67 if(std::all_of(arg.begin() + 2, arg.end(), [](
char x) {
68 return (x >=
'0' && x <=
'9') || (x >=
'A' && x <=
'F') || (x >=
'a' && x <=
'f');
72 }
else if(arg[1] ==
'o') {
73 if(std::all_of(arg.begin() + 2, arg.end(), [](
char x) { return (x >=
'0' && x <=
'7'); })) {
76 }
else if(arg[1] ==
'b') {
77 if(std::all_of(arg.begin() + 2, arg.end(), [](
char x) { return (x ==
'0' || x ==
'1'); })) {
82 if(!is_printable(arg)) {
83 return binary_escape_string(arg);
85 if(detail::has_escapable_character(arg)) {
86 if(arg.size() > 100 && !disable_multi_line) {
87 return std::string(multiline_literal_quote) + arg + multiline_literal_quote;
89 return std::string(1, stringQuote) + detail::add_escaped_characters(arg) + stringQuote;
91 return std::string(1, stringQuote) + arg + stringQuote;
94CLI11_INLINE std::string ini_join(
const std::vector<std::string> &args,
100 bool disable_multi_line{
false};
102 if(args.size() > 1 && arrayStart !=
'\0') {
103 joined.push_back(arrayStart);
104 disable_multi_line =
true;
106 std::size_t start = 0;
107 for(
const auto &arg : args) {
109 joined.push_back(sepChar);
110 if(!std::isspace<char>(sepChar, std::locale())) {
111 joined.push_back(
' ');
114 joined.append(convert_arg_for_ini(arg, stringQuote, literalQuote, disable_multi_line));
116 if(args.size() > 1 && arrayEnd !=
'\0') {
117 joined.push_back(arrayEnd);
122CLI11_INLINE std::vector<std::string>
123generate_parents(
const std::string §ion, std::string &name,
char parentSeparator) {
124 std::vector<std::string> parents;
125 if(detail::to_lower(section) !=
"default") {
126 if(section.find(parentSeparator) != std::string::npos) {
127 parents = detail::split_up(section, parentSeparator);
132 if(name.find(parentSeparator) != std::string::npos) {
133 std::vector<std::string> plist = detail::split_up(name, parentSeparator);
136 parents.insert(parents.end(), plist.begin(), plist.end());
140 detail::remove_quotes(parents);
141 }
catch(
const std::invalid_argument &iarg) {
148checkParentSegments(std::vector<ConfigItem> &output,
const std::string ¤tSection,
char parentSeparator) {
151 auto parents = detail::generate_parents(currentSection, estring, parentSeparator);
152 if(!output.empty() && output.back().name ==
"--") {
153 std::size_t msize = (parents.size() > 1U) ? parents.size() : 2;
154 while(output.back().parents.size() >= msize) {
155 output.push_back(output.back());
156 output.back().parents.pop_back();
159 if(parents.size() > 1) {
160 std::size_t common = 0;
161 std::size_t mpair = (std::min)(output.back().parents.size(), parents.size() - 1);
162 for(std::size_t ii = 0; ii < mpair; ++ii) {
163 if(output.back().parents[ii] != parents[ii]) {
168 if(common == mpair) {
171 while(output.back().parents.size() > common + 1) {
172 output.push_back(output.back());
173 output.back().parents.pop_back();
176 for(std::size_t ii = common; ii < parents.size() - 1; ++ii) {
177 output.emplace_back();
178 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
179 output.back().name =
"++";
182 }
else if(parents.size() > 1) {
183 for(std::size_t ii = 0; ii < parents.size() - 1; ++ii) {
184 output.emplace_back();
185 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
186 output.back().name =
"++";
191 output.emplace_back();
192 output.back().parents = std::move(parents);
193 output.back().name =
"++";
197CLI11_INLINE
bool hasMLString(std::string
const &fullString,
char check) {
198 if(fullString.length() < 3) {
201 auto it = fullString.rbegin();
202 return (*it == check) && (*(it + 1) == check) && (*(it + 2) == check);
206inline auto find_matching_config(std::vector<ConfigItem> &items,
207 const std::vector<std::string> &parents,
208 const std::string &name,
209 bool fullSearch) ->
decltype(items.begin()) {
213 auto search = items.end() - 1;
215 if(search->parents == parents && search->name == name) {
218 if(search == items.begin()) {
230 std::string currentSection =
"default";
231 std::string previousSection =
"default";
232 std::vector<ConfigItem> output;
235 bool inSection{
false};
236 bool inMLineComment{
false};
237 bool inMLineValue{
false};
239 char aStart = (isINIArray) ?
'[' :
arrayStart;
240 char aEnd = (isINIArray) ?
']' :
arrayEnd;
242 int currentSectionIndex{0};
245 while(getline(input, buffer)) {
246 std::vector<std::string> items_buffer;
248 line = detail::trim_copy(buffer);
249 std::size_t len = line.length();
254 if(line.compare(0, 3, multiline_string_quote) == 0 || line.compare(0, 3, multiline_literal_quote) == 0) {
255 inMLineComment =
true;
256 auto cchar = line.front();
257 while(inMLineComment) {
258 if(getline(input, line)) {
263 if(detail::hasMLString(line, cchar)) {
264 inMLineComment =
false;
269 if(line.front() ==
'[' && line.back() ==
']') {
270 if(currentSection !=
"default") {
272 output.emplace_back();
273 output.back().parents = detail::generate_parents(currentSection, name,
parentSeparatorChar);
274 output.back().name =
"--";
276 currentSection = line.substr(1, len - 2);
278 if(currentSection.size() > 1 && currentSection.front() ==
'[' && currentSection.back() ==
']') {
279 currentSection = currentSection.substr(1, currentSection.size() - 2);
281 if(detail::to_lower(currentSection) ==
"default") {
282 currentSection =
"default";
287 if(currentSection == previousSection) {
288 ++currentSectionIndex;
290 currentSectionIndex = 0;
291 previousSection = currentSection;
297 if(line.front() ==
';' || line.front() ==
'#' || line.front() ==
commentChar) {
300 std::size_t search_start = 0;
301 if(line.find_first_of(
"\"'`") != std::string::npos) {
302 while(search_start < line.size()) {
303 auto test_char = line[search_start];
304 if(test_char ==
'\"' || test_char ==
'\'' || test_char ==
'`') {
305 search_start = detail::close_sequence(line, search_start, line[search_start]);
313 search_start = line.find_first_of(line_sep_chars, search_start);
318 auto delimiter_pos = line.find_first_of(
valueDelimiter, search_start + 1);
319 auto comment_pos = line.find_first_of(
commentChar, search_start);
320 if(comment_pos < delimiter_pos) {
321 delimiter_pos = std::string::npos;
323 if(delimiter_pos != std::string::npos) {
325 name = detail::trim_copy(line.substr(0, delimiter_pos));
326 std::string item = detail::trim_copy(line.substr(delimiter_pos + 1, std::string::npos));
328 (item.compare(0, 3, multiline_literal_quote) == 0 || item.compare(0, 3, multiline_string_quote) == 0);
329 if(!mlquote && comment_pos != std::string::npos) {
331 item = detail::trim_copy(citems.front());
335 auto keyChar = item.front();
336 item = buffer.substr(delimiter_pos + 1, std::string::npos);
340 bool lineExtension{
false};
341 bool firstLine =
true;
342 if(!item.empty() && item.back() ==
'\\') {
344 lineExtension =
true;
345 }
else if(detail::hasMLString(item, keyChar)) {
350 if(keyChar ==
'\"') {
352 item = detail::remove_escaped_characters(item);
353 }
catch(
const std::invalid_argument &iarg) {
357 inMLineValue =
false;
359 while(inMLineValue) {
361 if(!std::getline(input, l2)) {
366 if(detail::hasMLString(line, keyChar)) {
372 }
else if(!(firstLine && item.empty())) {
373 item.push_back(
'\n');
377 inMLineValue =
false;
378 if(!item.empty() && item.back() ==
'\n') {
381 if(keyChar ==
'\"') {
383 item = detail::remove_escaped_characters(item);
384 }
catch(
const std::invalid_argument &iarg) {
391 }
else if(!(firstLine && item.empty())) {
392 item.push_back(
'\n');
394 lineExtension =
false;
396 if(!l2.empty() && l2.back() ==
'\\') {
397 lineExtension =
true;
403 items_buffer = {item};
404 }
else if(item.size() > 1 && item.front() == aStart) {
405 for(std::string multiline; item.back() != aEnd && std::getline(input, multiline);) {
406 detail::trim(multiline);
409 if(item.back() == aEnd) {
410 items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
412 items_buffer = detail::split_up(item.substr(1, std::string::npos), aSep);
414 }
else if((isDefaultArray || isINIArray) && item.find_first_of(aSep) != std::string::npos) {
415 items_buffer = detail::split_up(item, aSep);
416 }
else if((isDefaultArray || isINIArray) && item.find_first_of(
' ') != std::string::npos) {
417 items_buffer = detail::split_up(item,
'\0');
419 items_buffer = {item};
422 name = detail::trim_copy(line.substr(0, comment_pos));
423 items_buffer = {
"true"};
425 std::vector<std::string> parents;
428 detail::process_quoted_string(name);
430 for(
auto &it : items_buffer) {
433 }
catch(
const std::invalid_argument &ia) {
447 parents.erase(parents.begin());
451 if(match != output.end()) {
454 if(!(match->inputs.back().empty() || items_buffer.front().empty() || match->inputs.back() ==
"%%" ||
455 items_buffer.front() ==
"%%")) {
456 match->inputs.emplace_back(
"%%");
457 match->multiline =
true;
460 match->inputs.insert(match->inputs.end(), items_buffer.begin(), items_buffer.end());
462 output.emplace_back();
463 output.back().parents = std::move(parents);
464 output.back().name = std::move(name);
465 output.back().inputs = std::move(items_buffer);
468 if(currentSection !=
"default") {
471 output.emplace_back();
472 output.back().parents = detail::generate_parents(currentSection, ename,
parentSeparatorChar);
473 output.back().name =
"--";
474 while(output.back().parents.size() > 1) {
475 output.push_back(output.back());
476 output.back().parents.pop_back();
482CLI11_INLINE std::string &clean_name_string(std::string &name,
const std::string &keyChars) {
483 if(name.find_first_of(keyChars) != std::string::npos || (name.front() ==
'[' && name.back() ==
']') ||
484 (name.find_first_of(
"'`\"\\") != std::string::npos)) {
485 if(name.find_first_of(
'\'') == std::string::npos) {
486 name.insert(0, 1,
'\'');
487 name.push_back(
'\'');
489 if(detail::has_escapable_character(name)) {
490 name = detail::add_escaped_characters(name);
492 name.insert(0, 1,
'\"');
493 name.push_back(
'\"');
499CLI11_INLINE std::string
500ConfigBase::to_config(
const App *app,
bool default_also,
bool write_description, std::string prefix)
const {
501 std::stringstream out;
502 std::string commentLead;
504 commentLead.push_back(
' ');
506 std::string commentTest =
"#;";
510 std::string keyChars = commentTest;
518 std::vector<std::string> groups = app->get_groups();
519 bool defaultUsed =
false;
520 groups.insert(groups.begin(), std::string(
"OPTIONS"));
522 for(
auto &group : groups) {
523 if(group ==
"OPTIONS" || group.empty()) {
529 if(write_description && group !=
"OPTIONS" && !group.empty()) {
530 out <<
'\n' <<
commentChar << commentLead << group <<
" Options\n";
532 for(
const Option *opt : app->get_options({})) {
534 if(opt->get_configurable()) {
535 if(opt->get_group() != group) {
536 if(!(group ==
"OPTIONS" && opt->get_group().empty())) {
540 std::string single_name = opt->get_single_name();
541 if(single_name.empty()) {
545 auto results = opt->reduced_results();
549 bool isDefault =
false;
550 if(value.empty() && default_also) {
551 if(!opt->get_default_str().empty()) {
553 }
else if(opt->get_expected_min() == 0) {
555 }
else if(opt->get_run_callback_for_default() || !opt->get_required()) {
558 value =
"\"<REQUIRED>\"";
564 if(opt->get_expected_max() > 1 && detail::is_binary_escaped_string(value) && results.size() == 1 &&
565 !results[0].empty()) {
566 if(results[0].front() ==
'[' && results[0].back() ==
']') {
568 results[0].insert(0, 1, results[0].front());
569 results[0].push_back(results[0].back());
570 value = detail::ini_join(
574 if(!opt->get_fnames().empty()) {
576 value = opt->get_flag_value(single_name, value);
579 for(
const auto &test_name : opt->get_fnames()) {
581 value = opt->get_flag_value(test_name, value);
582 single_name = test_name;
589 value = detail::ini_join(
594 if(write_description && opt->has_description()) {
595 if(out.tellp() != std::streampos(0)) {
598 out << commentLead << detail::fix_newlines(commentLead, opt->get_description()) <<
'\n';
600 clean_name_string(single_name, keyChars);
602 std::string name = prefix + single_name;
612 auto subcommands = app->get_subcommands({});
613 for(
const App *subcom : subcommands) {
614 if(subcom->get_name().empty()) {
615 if(!default_also && (subcom->count_all() == 0)) {
618 if(write_description && !subcom->get_group().empty()) {
619 out <<
'\n' << commentLead << subcom->get_group() <<
" Options\n";
633 out <<
to_config(subcom, default_also, write_description, prefix);
637 for(
const App *subcom : subcommands) {
638 if(!subcom->get_name().empty()) {
639 if(!default_also && (subcom->count_all() == 0)) {
642 std::string subname = subcom->get_name();
643 clean_name_string(subname, keyChars);
645 if(subcom->get_configurable() && (default_also || app->got_subcommand(subcom))) {
646 if(!prefix.empty() || app->get_parent() ==
nullptr) {
648 out <<
'[' << prefix << subname <<
"]\n";
650 std::string appname = app->get_name();
651 clean_name_string(appname, keyChars);
653 const auto *p = app->get_parent();
654 while(p->get_parent() !=
nullptr) {
655 std::string pname = p->get_name();
656 clean_name_string(pname, keyChars);
660 out <<
'[' << subname <<
"]\n";
662 out <<
to_config(subcom, default_also, write_description,
"");
669 if(write_description && !out.str().empty()) {
670 std::string outString =
671 commentChar + commentLead + detail::fix_newlines(
commentChar + commentLead, app->get_description()) +
'\n';
672 return outString + out.str();
Thrown when the wrong number of arguments has been received.
Definition Error.hpp:263
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
std::string configSection
Specify the configuration section that should be used.
Definition ConfigFwd.hpp:112
char arraySeparator
the character used to separate elements in an array
Definition ConfigFwd.hpp:94
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
char stringQuote
the character to use around strings
Definition ConfigFwd.hpp:98
uint8_t maximumLayers
the maximum number of layers to allow
Definition ConfigFwd.hpp:102
char valueDelimiter
the character used separate the name from the value
Definition ConfigFwd.hpp:96
char arrayStart
the character used to start an array '\0' is a default to not use
Definition ConfigFwd.hpp:90
char parentSeparatorChar
the separator used to separator parent layers
Definition ConfigFwd.hpp:104
bool allowMultipleDuplicateFields
specify the config reader should collapse repeated field names to a single vector
Definition ConfigFwd.hpp:108
char literalQuote
the character to use around single characters and literal strings
Definition ConfigFwd.hpp:100
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition ConfigFwd.hpp:92
bool commentDefaultsBool
comment default values
Definition ConfigFwd.hpp:106
int16_t configIndex
Specify the configuration index to use for arrayed sections.
Definition ConfigFwd.hpp:110
char commentChar
the character used for comments
Definition ConfigFwd.hpp:88
Anything that can error in Parse.
Definition Error.hpp:159