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' || arg.front() ==
'\r') {
93 return_string.push_back(
'\n');
95 return_string.append(arg);
96 if(arg.back() ==
'\n' || arg.back() ==
'\r') {
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 std::size_t start = 0;
120 for(
const auto &arg : args) {
122 joined.push_back(sepChar);
123 if(!std::isspace<char>(sepChar, std::locale())) {
124 joined.push_back(
' ');
127 joined.append(convert_arg_for_ini(arg, stringQuote, literalQuote, disable_multi_line));
129 if(args.size() > 1 && arrayEnd !=
'\0') {
130 joined.push_back(arrayEnd);
135CLI11_INLINE std::vector<std::string>
136generate_parents(
const std::string §ion, std::string &name,
char parentSeparator) {
137 std::vector<std::string> parents;
138 if(detail::to_lower(section) !=
"default") {
139 if(section.find(parentSeparator) != std::string::npos) {
140 parents = detail::split_up(section, parentSeparator);
145 if(name.find(parentSeparator) != std::string::npos) {
146 std::vector<std::string> plist = detail::split_up(name, parentSeparator);
149 parents.insert(parents.end(), plist.begin(), plist.end());
153 detail::remove_quotes(parents);
154 }
catch(
const std::invalid_argument &iarg) {
161checkParentSegments(std::vector<ConfigItem> &output,
const std::string ¤tSection,
char parentSeparator) {
164 auto parents = detail::generate_parents(currentSection, estring, parentSeparator);
165 if(!output.empty() && output.back().name ==
"--") {
166 std::size_t msize = (parents.size() > 1U) ? parents.size() : 2;
167 while(output.back().parents.size() >= msize) {
168 output.push_back(output.back());
169 output.back().parents.pop_back();
172 if(parents.size() > 1) {
173 std::size_t common = 0;
174 std::size_t mpair = (std::min)(output.back().parents.size(), parents.size() - 1);
175 for(std::size_t ii = 0; ii < mpair; ++ii) {
176 if(output.back().parents[ii] != parents[ii]) {
181 if(common == mpair) {
184 while(output.back().parents.size() > common + 1) {
185 output.push_back(output.back());
186 output.back().parents.pop_back();
189 for(std::size_t ii = common; ii < parents.size() - 1; ++ii) {
190 output.emplace_back();
191 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
192 output.back().name =
"++";
195 }
else if(parents.size() > 1) {
196 for(std::size_t ii = 0; ii < parents.size() - 1; ++ii) {
197 output.emplace_back();
198 output.back().parents.assign(parents.begin(), parents.begin() +
static_cast<std::ptrdiff_t
>(ii) + 1);
199 output.back().name =
"++";
204 output.emplace_back();
205 output.back().parents = std::move(parents);
206 output.back().name =
"++";
210CLI11_INLINE
bool hasMLString(std::string
const &fullString,
char check) {
211 if(fullString.length() < 3) {
214 auto it = fullString.rbegin();
215 return (*it == check) && (*(it + 1) == check) && (*(it + 2) == check);
219inline auto find_matching_config(std::vector<ConfigItem> &items,
220 const std::vector<std::string> &parents,
221 const std::string &name,
222 bool fullSearch) ->
decltype(items.begin()) {
226 auto search = items.end() - 1;
228 if(search->parents == parents && search->name == name) {
231 if(search == items.begin()) {
243 std::string currentSection =
"default";
244 std::string previousSection =
"default";
245 std::vector<ConfigItem> output;
248 bool inSection{
false};
249 bool inMLineComment{
false};
250 bool inMLineValue{
false};
252 char aStart = (isINIArray) ?
'[' :
arrayStart;
253 char aEnd = (isINIArray) ?
']' :
arrayEnd;
255 int currentSectionIndex{0};
258 while(getline(input, buffer)) {
259 std::vector<std::string> items_buffer;
261 line = detail::trim_copy(buffer);
262 std::size_t len = line.length();
267 if(line.compare(0, 3, multiline_string_quote) == 0 || line.compare(0, 3, multiline_literal_quote) == 0) {
268 inMLineComment =
true;
269 auto cchar = line.front();
270 while(inMLineComment) {
271 if(getline(input, line)) {
276 if(detail::hasMLString(line, cchar)) {
277 inMLineComment =
false;
282 if(line.front() ==
'[' && line.back() ==
']') {
283 if(currentSection !=
"default") {
285 output.emplace_back();
286 output.back().parents = detail::generate_parents(currentSection, name,
parentSeparatorChar);
287 output.back().name =
"--";
289 currentSection = line.substr(1, len - 2);
291 if(currentSection.size() > 1 && currentSection.front() ==
'[' && currentSection.back() ==
']') {
292 currentSection = currentSection.substr(1, currentSection.size() - 2);
294 if(detail::to_lower(currentSection) ==
"default") {
295 currentSection =
"default";
300 if(currentSection == previousSection) {
301 ++currentSectionIndex;
303 currentSectionIndex = 0;
304 previousSection = currentSection;
310 if(line.front() ==
';' || line.front() ==
'#' || line.front() ==
commentChar) {
313 std::size_t search_start = 0;
314 if(line.find_first_of(
"\"'`") != std::string::npos) {
315 while(search_start < line.size()) {
316 auto test_char = line[search_start];
317 if(test_char ==
'\"' || test_char ==
'\'' || test_char ==
'`') {
318 search_start = detail::close_sequence(line, search_start, line[search_start]);
326 search_start = line.find_first_of(line_sep_chars, search_start);
331 auto delimiter_pos = line.find_first_of(
valueDelimiter, search_start + 1);
332 auto comment_pos = line.find_first_of(
commentChar, search_start);
333 if(comment_pos < delimiter_pos) {
334 delimiter_pos = std::string::npos;
336 if(delimiter_pos != std::string::npos) {
338 name = detail::trim_copy(line.substr(0, delimiter_pos));
339 std::string item = detail::trim_copy(line.substr(delimiter_pos + 1, std::string::npos));
341 (item.compare(0, 3, multiline_literal_quote) == 0 || item.compare(0, 3, multiline_string_quote) == 0);
342 if(!mlquote && comment_pos != std::string::npos) {
344 item = detail::trim_copy(citems.front());
348 auto keyChar = item.front();
349 item = buffer.substr(delimiter_pos + 1, std::string::npos);
353 bool lineExtension{
false};
354 bool firstLine =
true;
355 if(!item.empty() && item.back() ==
'\\' && keyChar ==
'\"') {
357 lineExtension =
true;
358 }
else if(detail::hasMLString(item, keyChar)) {
363 if(keyChar ==
'\"') {
365 item = detail::remove_escaped_characters(item);
366 }
catch(
const std::invalid_argument &iarg) {
370 inMLineValue =
false;
372 while(inMLineValue) {
374 if(!std::getline(input, l2)) {
379 if(detail::hasMLString(line, keyChar)) {
385 }
else if(!(firstLine && item.empty())) {
386 item.push_back(
'\n');
390 inMLineValue =
false;
391 if(!item.empty() && item.back() ==
'\n') {
394 if(keyChar ==
'\"') {
396 item = detail::remove_escaped_characters(item);
397 }
catch(
const std::invalid_argument &iarg) {
404 }
else if(!(firstLine && item.empty())) {
405 item.push_back(
'\n');
407 lineExtension =
false;
409 if(!l2.empty() && l2.back() ==
'\\' && keyChar ==
'\"') {
410 lineExtension =
true;
416 items_buffer = {item};
417 }
else if(item.size() > 1 && item.front() == aStart) {
418 for(std::string multiline; item.back() != aEnd && std::getline(input, multiline);) {
419 detail::trim(multiline);
422 if(item.back() == aEnd) {
423 items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
425 items_buffer = detail::split_up(item.substr(1, std::string::npos), aSep);
427 }
else if((isDefaultArray || isINIArray) && item.find_first_of(aSep) != std::string::npos) {
428 items_buffer = detail::split_up(item, aSep);
429 }
else if((isDefaultArray || isINIArray) && item.find_first_of(
' ') != std::string::npos) {
430 items_buffer = detail::split_up(item,
'\0');
432 items_buffer = {item};
435 name = detail::trim_copy(line.substr(0, comment_pos));
436 items_buffer = {
"true"};
438 std::vector<std::string> parents;
441 detail::process_quoted_string(name,
'"',
'\'',
true);
443 for(
auto &it : items_buffer) {
446 }
catch(
const std::invalid_argument &ia) {
460 parents.erase(parents.begin());
464 if(match != output.end()) {
467 if(!(match->inputs.back().empty() || items_buffer.front().empty() || match->inputs.back() ==
"%%" ||
468 items_buffer.front() ==
"%%")) {
469 match->inputs.emplace_back(
"%%");
470 match->multiline =
true;
473 match->inputs.insert(match->inputs.end(), items_buffer.begin(), items_buffer.end());
475 output.emplace_back();
476 output.back().parents = std::move(parents);
477 output.back().name = std::move(name);
478 output.back().inputs = std::move(items_buffer);
481 if(currentSection !=
"default") {
484 output.emplace_back();
485 output.back().parents = detail::generate_parents(currentSection, ename,
parentSeparatorChar);
486 output.back().name =
"--";
487 while(output.back().parents.size() > 1) {
488 output.push_back(output.back());
489 output.back().parents.pop_back();
495CLI11_INLINE std::string &clean_name_string(std::string &name,
const std::string &keyChars) {
496 if(name.find_first_of(keyChars) != std::string::npos || (name.front() ==
'[' && name.back() ==
']') ||
497 (name.find_first_of(
"'`\"\\") != std::string::npos)) {
498 if(name.find_first_of(
'\'') == std::string::npos) {
499 name.insert(0, 1,
'\'');
500 name.push_back(
'\'');
502 if(detail::has_escapable_character(name)) {
503 name = detail::add_escaped_characters(name);
505 name.insert(0, 1,
'\"');
506 name.push_back(
'\"');
512CLI11_INLINE std::string
514 std::stringstream out;
515 std::string commentLead;
517 commentLead.push_back(
' ');
519 std::string commentTest =
"#;";
523 std::string keyChars = commentTest;
531 std::vector<std::string> groups = app->
get_groups();
532 bool defaultUsed =
false;
533 groups.insert(groups.begin(), std::string(
"OPTIONS"));
535 for(
auto &group : groups) {
536 if(group ==
"OPTIONS" || group.empty()) {
542 if(write_description && group !=
"OPTIONS" && !group.empty()) {
543 out <<
'\n' <<
commentChar << commentLead << group <<
" Options\n";
547 if(opt->get_configurable()) {
548 if(opt->get_group() != group) {
549 if(!(group ==
"OPTIONS" && opt->get_group().empty())) {
553 std::string single_name = opt->get_single_name();
554 if(single_name.empty()) {
558 auto results = opt->reduced_results();
559 if(results.size() > 1 && opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Reverse) {
560 std::reverse(results.begin(), results.end());
562 if(opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Sum && opt->count() >= 1 &&
563 results.size() == 1) {
566 auto pos = opt->_validate(results[0], 0);
568 results = opt->results();
571 if(opt->get_multi_option_policy() == CLI::MultiOptionPolicy::Join && opt->count() > 1) {
572 char delim = opt->get_delimiter();
575 results = opt->results();
579 auto delim_count = std::count(results[0].begin(), results[0].end(), delim);
580 if(results[0].back() == delim ||
581 static_cast<decltype(delim_count)
>(opt->count()) < delim_count - 1 ||
582 results[0].find(std::string(2, delim)) != std::string::npos) {
583 results = opt->results();
590 bool isDefault =
false;
591 if(value.empty() && default_also) {
592 if(!opt->get_default_str().empty()) {
596 }
else if(opt->get_expected_min() == 0) {
598 }
else if(opt->get_run_callback_for_default() || !opt->get_required()) {
601 value =
"\"<REQUIRED>\"";
607 if(!opt->get_fnames().empty()) {
609 value = opt->get_flag_value(single_name, value);
612 for(
const auto &test_name : opt->get_fnames()) {
614 value = opt->get_flag_value(test_name, value);
615 single_name = test_name;
622 value = detail::ini_join(
627 if(write_description && opt->has_description()) {
628 if(out.tellp() != std::streampos(0)) {
631 out << commentLead << detail::fix_newlines(commentLead, opt->get_description()) <<
'\n';
633 clean_name_string(single_name, keyChars);
635 std::string name = prefix + single_name;
646 for(
const App *subcom : subcommands) {
647 if(subcom->get_name().empty()) {
648 if(!default_also && (subcom->count_all() == 0)) {
651 if(write_description && !subcom->get_group().empty()) {
652 out <<
'\n' << commentLead << subcom->get_group() <<
" Options\n";
666 out <<
to_config(subcom, default_also, write_description, prefix);
670 for(
const App *subcom : subcommands) {
671 if(!subcom->get_name().empty()) {
672 if(!default_also && (subcom->count_all() == 0)) {
675 std::string subname = subcom->get_name();
676 clean_name_string(subname, keyChars);
678 if(subcom->get_configurable() && (default_also || app->
got_subcommand(subcom))) {
679 if(!prefix.empty() || app->
get_parent() ==
nullptr) {
681 out <<
'[' << prefix << subname <<
"]\n";
683 std::string appname = app->
get_name();
684 clean_name_string(appname, keyChars);
687 while(p->get_parent() !=
nullptr) {
688 std::string pname = p->get_name();
689 clean_name_string(pname, keyChars);
693 out <<
'[' << subname <<
"]\n";
695 out <<
to_config(subcom, default_also, write_description,
"");
702 if(write_description && !out.str().empty()) {
703 std::string outString =
705 return outString + out.str();
Creates a command line program, with very few defaults.
Definition App.hpp:98
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1223
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:967
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:940
bool got_subcommand(const App *subcom) const
Check to see if given subcommand was selected.
Definition App.hpp:951
std::vector< const Option * > get_options(const std::function< bool(const Option *)> filter={}) const
Get the list of options (user facing function, so returns raw pointers), has optional filter function...
Definition App_inl.hpp:830
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1074
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1229
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.
Definition Config_inl.hpp:240
std::string configSection
Specify the configuration section that should be used.
Definition ConfigFwd.hpp:112
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
Definition Config_inl.hpp:513
char arraySeparator
the character used to separate elements in an array
Definition ConfigFwd.hpp:94
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
Definition Option.hpp:233
Anything that can error in Parse.
Definition Error.hpp:159