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 if(arg.find(multiline_literal_quote) != std::string::npos) {
88 return binary_escape_string(arg,
true);
90 std::string return_string{multiline_literal_quote};
91 return_string.reserve(7 + arg.size());
92 if(arg.front() ==
'\n') {
93 return_string.push_back(
'\n');
95 return_string.append(arg);
96 if(arg.back() ==
'\n') {
97 return_string.push_back(
'\n');
99 return_string.append(multiline_literal_quote, 3);
100 return return_string;
102 return std::string(1, stringQuote) + detail::add_escaped_characters(arg) + stringQuote;
104 return std::string(1, stringQuote) + arg + stringQuote;
107CLI11_INLINE std::string ini_join(
const std::vector<std::string> &args,
113 bool disable_multi_line{
false};
115 if(args.size() > 1 && arrayStart !=
'\0') {
116 joined.push_back(arrayStart);
117 disable_multi_line =
true;
119 const bool sep_is_space = std::isspace<char>(sepChar, std::locale());
120 std::size_t start = 0;
121 for(
const auto &arg : args) {
123 joined.push_back(sepChar);
125 joined.push_back(
' ');
128 joined.append(convert_arg_for_ini(arg, stringQuote, literalQuote, disable_multi_line));
130 if(args.size() > 1 && arrayEnd !=
'\0') {
131 joined.push_back(arrayEnd);
136CLI11_INLINE std::vector<std::string>
137generate_parents(
const std::string §ion, std::string &name,
char parentSeparator) {
138 std::vector<std::string> parents;
139 if(detail::to_lower(section) !=
"default") {
140 if(section.find(parentSeparator) != std::string::npos) {
141 parents = detail::split_up(section, parentSeparator);
146 if(name.find(parentSeparator) != std::string::npos) {
147 std::vector<std::string> plist = detail::split_up(name, parentSeparator);
150 parents.insert(parents.end(), plist.begin(), plist.end());
154 detail::remove_quotes(parents);
155 }
catch(
const std::invalid_argument &iarg) {
162checkParentSegments(std::vector<ConfigItem> &output,
const std::string ¤tSection,
char parentSeparator) {
165 auto parents = detail::generate_parents(currentSection, estring, parentSeparator);
166 if(!output.empty() && output.back().name ==
"--") {
167 std::size_t msize = (parents.size() > 1U) ? parents.size() : 2;
168 while(output.back().parents.size() >= msize) {
169 output.push_back(output.back());
170 output.back().parents.pop_back();
173 if(parents.size() > 1) {
174 std::size_t common = 0;
175 std::size_t mpair = (std::min)(output.back().parents.size(), parents.size() - 1);
176 for(std::size_t ii = 0; ii < mpair; ++ii) {
177 if(output.back().parents[ii] != parents[ii]) {
182 if(common == mpair) {
185 while(output.back().parents.size() > common + 1) {
186 output.push_back(output.back());
187 output.back().parents.pop_back();
190 for(std::size_t ii = common; ii < parents.size() - 1; ++ii) {
191 output.emplace_back();
192 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
193 output.back().name =
"++";
196 }
else if(parents.size() > 1) {
197 for(std::size_t ii = 0; ii < parents.size() - 1; ++ii) {
198 output.emplace_back();
199 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
200 output.back().name =
"++";
205 output.emplace_back();
206 output.back().parents = std::move(parents);
207 output.back().name =
"++";
211CLI11_INLINE
bool hasMLString(std::string
const &fullString,
char check) {
212 if(fullString.length() < 3) {
215 auto it = fullString.rbegin();
216 return (*it == check) && (*(it + 1) == check) && (*(it + 2) == check);
220inline auto find_matching_config(std::vector<ConfigItem> &items,
221 const std::vector<std::string> &parents,
222 const std::string &name,
223 bool fullSearch) ->
decltype(items.begin()) {
227 auto search = items.end() - 1;
229 if(search->parents == parents && search->name == name) {
232 if(search == items.begin()) {
240CLI11_INLINE
void clean_name_string(std::string &name,
const std::string &keyChars) {
241 if(name.find_first_of(keyChars) != std::string::npos || (name.front() ==
'[' && name.back() ==
']') ||
242 (name.find_first_of(
"'`\"\\") != std::string::npos)) {
243 if(name.find_first_of(
'\'') == std::string::npos) {
244 name.insert(0, 1,
'\'');
245 name.push_back(
'\'');
247 if(detail::has_escapable_character(name)) {
248 name = detail::add_escaped_characters(name);
250 name.insert(0, 1,
'\"');
251 name.push_back(
'\"');
260 std::string currentSection =
"default";
261 std::string previousSection =
"default";
262 std::vector<ConfigItem> output;
265 bool inSection{
false};
266 bool inMLineComment{
false};
267 bool inMLineValue{
false};
269 char aStart = (isINIArray) ?
'[' :
arrayStart;
270 char aEnd = (isINIArray) ?
']' :
arrayEnd;
272 int currentSectionIndex{0};
275 while(getline(input, buffer)) {
276 std::vector<std::string> items_buffer;
278 line = detail::trim_copy(buffer);
279 std::size_t len = line.length();
284 if(line.compare(0, 3, multiline_string_quote) == 0 || line.compare(0, 3, multiline_literal_quote) == 0) {
287 if(len >= 6 && detail::hasMLString(line, line.front())) {
290 inMLineComment =
true;
291 auto cchar = line.front();
292 while(inMLineComment) {
293 if(getline(input, line)) {
298 if(detail::hasMLString(line, cchar)) {
299 inMLineComment =
false;
306 if(line.front() ==
'[' && line.back() !=
']' && line.find_first_of(
commentChar) != std::string::npos) {
307 std::size_t comment_search = 0;
308 while(comment_search < line.size()) {
309 auto test_char = line[comment_search];
310 if(test_char ==
'\"' || test_char ==
'\'' || test_char ==
'`') {
311 comment_search = detail::close_sequence(line, comment_search, line[comment_search]);
319 if(comment_search < line.size() && line[comment_search] ==
commentChar) {
320 line = detail::trim_copy(line.substr(0, comment_search));
327 if(line.front() ==
'[' && line.back() ==
']') {
328 if(currentSection !=
"default") {
330 output.emplace_back();
331 output.back().parents = detail::generate_parents(currentSection, name,
parentSeparatorChar);
332 output.back().name =
"--";
334 currentSection = line.substr(1, len - 2);
336 if(currentSection.size() > 1 && currentSection.front() ==
'[' && currentSection.back() ==
']') {
337 currentSection = currentSection.substr(1, currentSection.size() - 2);
339 if(detail::to_lower(currentSection) ==
"default") {
340 currentSection =
"default";
345 if(currentSection == previousSection) {
346 ++currentSectionIndex;
348 currentSectionIndex = 0;
349 previousSection = currentSection;
355 if(line.front() ==
';' || line.front() ==
'#' || line.front() ==
commentChar) {
358 std::size_t search_start = 0;
359 if(line.find_first_of(
"\"'`") != std::string::npos) {
360 while(search_start < line.size()) {
361 auto test_char = line[search_start];
362 if(test_char ==
'\"' || test_char ==
'\'' || test_char ==
'`') {
363 search_start = detail::close_sequence(line, search_start, line[search_start]);
371 search_start = line.find_first_of(line_sep_chars, search_start);
376 auto delimiter_pos = line.find_first_of(
valueDelimiter, search_start + 1);
377 auto comment_pos = line.find_first_of(
commentChar, search_start);
378 if(comment_pos < delimiter_pos) {
379 delimiter_pos = std::string::npos;
381 if(delimiter_pos != std::string::npos) {
383 name = detail::trim_copy(line.substr(0, delimiter_pos));
384 std::string item = detail::trim_copy(line.substr(delimiter_pos + 1, std::string::npos));
386 (item.compare(0, 3, multiline_literal_quote) == 0 || item.compare(0, 3, multiline_string_quote) == 0);
387 if(!mlquote && comment_pos != std::string::npos) {
389 item = detail::trim_copy(citems.front());
393 auto keyChar = item.front();
394 auto offset = buffer.find_first_not_of(
" \t");
395 item = buffer.substr((offset == std::string::npos ? 0 : offset) + delimiter_pos + 1, std::string::npos);
399 bool lineExtension{
false};
400 bool firstLine =
true;
401 if(!item.empty() && item.back() ==
'\\' && keyChar ==
'\"') {
403 lineExtension =
true;
404 }
else if(detail::hasMLString(item, keyChar)) {
409 if(keyChar ==
'\"') {
411 item = detail::remove_escaped_characters(item);
412 }
catch(
const std::invalid_argument &iarg) {
416 inMLineValue =
false;
418 while(inMLineValue) {
420 if(!std::getline(input, l2)) {
425 if(detail::hasMLString(line, keyChar)) {
431 }
else if(!(firstLine && item.empty())) {
432 item.push_back(
'\n');
436 inMLineValue =
false;
437 if(!item.empty() && item.back() ==
'\n') {
440 if(keyChar ==
'\"') {
442 item = detail::remove_escaped_characters(item);
443 }
catch(
const std::invalid_argument &iarg) {
450 }
else if(!(firstLine && item.empty())) {
451 item.push_back(
'\n');
453 lineExtension =
false;
455 if(!l2.empty() && l2.back() ==
'\\' && keyChar ==
'\"') {
456 lineExtension =
true;
462 items_buffer = {item};
463 }
else if(!item.empty() && item.front() == aStart) {
464 for(std::string multiline; item.back() != aEnd && std::getline(input, multiline);) {
465 detail::trim(multiline);
468 if(item.back() == aEnd) {
469 items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
471 items_buffer = detail::split_up(item.substr(1, std::string::npos), aSep);
473 }
else if((isDefaultArray || isINIArray) && item.find_first_of(aSep) != std::string::npos) {
474 items_buffer = detail::split_up(item, aSep);
475 }
else if((isDefaultArray || isINIArray) && item.find_first_of(
' ') != std::string::npos) {
476 items_buffer = detail::split_up(item,
'\0');
478 items_buffer = {item};
481 name = detail::trim_copy(line.substr(0, comment_pos));
482 items_buffer = {
"true"};
484 std::vector<std::string> parents;
487 detail::process_quoted_string(name,
'"',
'\'',
true);
489 for(
auto &it : items_buffer) {
492 }
catch(
const std::invalid_argument &ia) {
506 parents.erase(parents.begin());
510 if(match != output.end()) {
513 if(!(match->inputs.back().empty() || items_buffer.front().empty() || match->inputs.back() ==
"%%" ||
514 items_buffer.front() ==
"%%")) {
515 match->inputs.emplace_back(
"%%");
516 match->multiline =
true;
519 match->inputs.insert(match->inputs.end(), items_buffer.begin(), items_buffer.end());
521 output.emplace_back();
522 output.back().parents = std::move(parents);
523 output.back().name = std::move(name);
524 output.back().inputs = std::move(items_buffer);
527 if(currentSection !=
"default") {
530 output.emplace_back();
531 output.back().parents = detail::generate_parents(currentSection, ename,
parentSeparatorChar);
532 output.back().name =
"--";
533 while(output.back().parents.size() > 1) {
534 output.push_back(output.back());
535 output.back().parents.pop_back();
541CLI11_INLINE std::string
544 default_also ? ConfigOutputMode::AllDefaults : ConfigOutputMode::Active,
549CLI11_INLINE std::string
550ConfigBase::to_config(
const App *app, ConfigOutputMode mode,
bool write_description, std::string prefix)
const {
551 std::stringstream out;
552 const bool include_default_values = (mode != ConfigOutputMode::Active);
553 std::string commentLead;
555 commentLead.push_back(
' ');
557 std::string commentTest =
"#;";
561 std::string keyChars = commentTest;
569 std::vector<std::string> groups = app->get_groups();
570 bool defaultUsed =
false;
571 groups.insert(groups.begin(), std::string(
"OPTIONS"));
573 const std::vector<const Option *> options = app->get_options({});
574 for(
auto &group : groups) {
575 if(group ==
"OPTIONS" || group.empty()) {
581 if(write_description && group !=
"OPTIONS" && !group.empty()) {
582 out <<
'\n' <<
commentChar << commentLead << group <<
" Options\n";
584 for(
const Option *opt : app->get_options({})) {
586 if(opt->get_configurable()) {
587 if(opt->get_group() != group) {
588 if(!(group ==
"OPTIONS" && opt->get_group().empty())) {
592 std::string single_name = opt->get_single_name();
593 if(single_name.empty()) {
597 auto results = opt->reduced_results();
598 if(results.size() > 1 && opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Reverse) {
599 std::reverse(results.begin(), results.end());
601 if(opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Sum && opt->count() >= 1 &&
602 results.size() == 1) {
605 auto pos = opt->_validate(results[0], 0);
607 results = opt->results();
610 if(opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Join && opt->count() > 1) {
611 char delim = opt->get_delimiter();
614 results = opt->results();
618 auto delim_count = std::count(results[0].begin(), results[0].end(), delim);
619 if(results[0].back() == delim ||
620 static_cast<decltype(delim_count)
>(opt->count()) <= delim_count ||
621 results[0].find(std::string(2, delim)) != std::string::npos) {
622 results = opt->results();
628 if(opt->count() == 1 && results.size() == 2 && results.front() ==
"{}" && results.back() ==
"%%") {
638 bool isDefault =
false;
639 if(value.empty() && include_default_values) {
640 if(!opt->get_default_str().empty()) {
644 }
else if(opt->get_expected_min() == 0) {
646 }
else if(opt->get_run_callback_for_default() || !opt->get_required()) {
649 value =
"\"<REQUIRED>\"";
655 if(!opt->get_fnames().empty()) {
657 value = opt->get_flag_value(single_name, value);
660 for(
const auto &test_name : opt->get_fnames()) {
662 value = opt->get_flag_value(test_name, value);
663 single_name = test_name;
670 value = detail::ini_join(
675 if(write_description && opt->has_description()) {
676 if(out.tellp() != std::streampos(0)) {
679 out << commentLead << detail::fix_newlines(commentLead, opt->get_description()) <<
'\n';
681 detail::clean_name_string(single_name, keyChars);
683 std::string name = prefix + single_name;
693 auto subcommands = app->get_subcommands({});
694 for(
const App *subcom : subcommands) {
695 if(subcom->get_name().empty()) {
696 if(!include_default_values && (subcom->count_all() == 0)) {
699 if(write_description && !subcom->get_group().empty()) {
700 out <<
'\n' <<
commentChar << commentLead << subcom->get_group() <<
" Options\n";
714 out <<
to_config(subcom, mode, write_description, prefix);
718 for(
const App *subcom : subcommands) {
719 if(!subcom->get_name().empty()) {
720 if((!include_default_values && (subcom->count_all() == 0)) ||
721 (mode == ConfigOutputMode::ActiveSubcommandDefaults && !app->got_subcommand(subcom))) {
724 std::string subname = subcom->get_name();
725 detail::clean_name_string(subname, keyChars);
727 if(subcom->get_configurable() && (app->got_subcommand(subcom) || (mode == ConfigOutputMode::AllDefaults))) {
728 if(!prefix.empty() || app->get_parent() ==
nullptr) {
730 out <<
'[' << prefix << subname <<
"]\n";
732 std::string appname = app->get_name();
733 detail::clean_name_string(appname, keyChars);
735 const auto *p = app->get_parent();
736 while(p->get_parent() !=
nullptr) {
737 std::string pname = p->get_name();
738 detail::clean_name_string(pname, keyChars);
742 out <<
'[' << subname <<
"]\n";
744 out <<
to_config(subcom, mode, write_description,
"");
751 if(write_description && !out.str().empty()) {
752 std::string outString =
753 commentChar + commentLead + detail::fix_newlines(
commentChar + commentLead, app->get_description()) +
'\n';
754 return outString + out.str();
Creates a command line program, with very few defaults.
Definition App.hpp:114
Thrown when the wrong number of arguments has been received.
Definition Error.hpp:264
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
Definition Config_inl.hpp:257
std::string configSection
Specify the configuration section that should be used.
Definition ConfigFwd.hpp:128
std::string to_config(const App *, ConfigOutputMode mode, bool write_description, std::string prefix) const override
Convert an app into a configuration.
char arraySeparator
the character used to separate elements in an array
Definition ConfigFwd.hpp:110
char stringQuote
the character to use around strings
Definition ConfigFwd.hpp:114
uint8_t maximumLayers
the maximum number of layers to allow
Definition ConfigFwd.hpp:118
char valueDelimiter
the character used separate the name from the value
Definition ConfigFwd.hpp:112
char arrayStart
the character used to start an array '\0' is a default to not use
Definition ConfigFwd.hpp:106
char parentSeparatorChar
the separator used to separator parent layers
Definition ConfigFwd.hpp:120
bool allowMultipleDuplicateFields
specify the config reader should collapse repeated field names to a single vector
Definition ConfigFwd.hpp:124
char literalQuote
the character to use around single characters and literal strings
Definition ConfigFwd.hpp:116
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition ConfigFwd.hpp:108
bool commentDefaultsBool
comment default values
Definition ConfigFwd.hpp:122
int16_t configIndex
Specify the configuration index to use for arrayed sections.
Definition ConfigFwd.hpp:126
char commentChar
the character used for comments
Definition ConfigFwd.hpp:104
Anything that can error in Parse.
Definition Error.hpp:160