1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 21:53:51 +00:00

fix an issue with default strings of arrays and config out. (#1155)

Fixes #1154

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top 2025-04-28 17:39:29 -07:00 committed by GitHub
parent cbbf20ed93
commit 3ed270937e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 2 deletions

View File

@ -549,7 +549,9 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description,
bool isDefault = false;
if(value.empty() && default_also) {
if(!opt->get_default_str().empty()) {
value = detail::convert_arg_for_ini(opt->get_default_str(), stringQuote, literalQuote, false);
results_t res;
opt->results(res);
value = detail::ini_join(res, arraySeparator, arrayStart, arrayEnd, stringQuote, literalQuote);
} else if(opt->get_expected_min() == 0) {
value = "false";
} else if(opt->get_run_callback_for_default() || !opt->get_required()) {

View File

@ -686,7 +686,8 @@ CLI11_INLINE int Option::_add_result(std::string &&result, std::vector<std::stri
}
}
if((allow_extra_args_ || get_expected_max() > 1) && !result.empty() && result.front() == '[' &&
if((allow_extra_args_ || get_expected_max() > 1 || get_type_size() > 1) && !result.empty() &&
result.front() == '[' &&
result.back() == ']') { // this is now a vector string likely from the default or user entry
result.pop_back();

View File

@ -14,6 +14,8 @@
#include <tuple>
#include <vector>
#include <array>
TEST_CASE("StringBased: convert_arg_for_ini", "[config]") {
CHECK("\"\"" == CLI::detail::convert_arg_for_ini(std::string{}));
@ -4114,3 +4116,20 @@ TEST_CASE_METHOD(TApp, "RoundTripEmptyVector", "[config]") {
app.parse_from_stream(out);
CHECK(cv.empty());
}
TEST_CASE_METHOD(TApp, "RoundTripArrayFloat", "[config]") {
std::array<float, 2> cv{-1.0F, 1.0F};
app.add_option("-c", cv)->capture_default_str();
args = {};
run();
std::string configOut = app.config_to_str(true, true);
app.clear();
std::stringstream out(configOut);
cv[0] = -3.0F;
cv[1] = 4.0F;
app.parse_from_stream(out);
CHECK(cv[0] == -1.0F);
CHECK(cv[1] == 1.0F);
}