Skip to content

Commit 399e729

Browse files
authored
Fix issue with multiline array interpretation for toml (#1196)
if the first line only contained a single character. Fixes #1195
1 parent e7e8de0 commit 399e729

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

include/CLI/impl/Config_inl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
414414
}
415415
}
416416
items_buffer = {item};
417-
} else if(item.size() > 1 && item.front() == aStart) {
417+
} else if(!item.empty() && item.front() == aStart) {
418418
for(std::string multiline; item.back() != aEnd && std::getline(input, multiline);) {
419419
detail::trim(multiline);
420420
item += multiline;

tests/ConfigFileTest.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,57 @@ TEST_CASE_METHOD(TApp, "TOMLVector", "[config]") {
15101510
CHECK(three == std::vector<int>({1, 2, 3}));
15111511
}
15121512

1513+
TEST_CASE_METHOD(TApp, "TOMLMultiLineVector", "[config]") {
1514+
1515+
TempFile tmptoml{"TestTomlTmp.toml"};
1516+
1517+
app.set_config("--config", tmptoml);
1518+
1519+
{
1520+
std::ofstream out{tmptoml};
1521+
out << "#this is a comment line\n";
1522+
out << "[default]\n";
1523+
out << "two=[\n";
1524+
out << " 2, 3\n";
1525+
out << "]\n";
1526+
out << "three=[\n\t1,\n\t2,\n\t3\n]\n";
1527+
}
1528+
1529+
std::vector<int> two, three;
1530+
app.add_option("--two", two)->expected(2)->required();
1531+
app.add_option("--three", three)->required();
1532+
1533+
run();
1534+
1535+
CHECK(two == std::vector<int>({2, 3}));
1536+
CHECK(three == std::vector<int>({1, 2, 3}));
1537+
}
1538+
1539+
TEST_CASE_METHOD(TApp, "TOMLMultiLineVector2", "[config]") {
1540+
1541+
TempFile tmptoml{"TestTomlTmp.toml"};
1542+
1543+
app.set_config("--config", tmptoml);
1544+
1545+
{
1546+
std::ofstream out{tmptoml};
1547+
out << "#this is a comment line\n";
1548+
out << "[default]\n";
1549+
out << "two=[\n";
1550+
out << " 2, 3]\n";
1551+
out << "three=[\n\t1,\n\t2,\n\t3\n]\n";
1552+
}
1553+
1554+
std::vector<int> two, three;
1555+
app.add_option("--two", two)->expected(2)->required();
1556+
app.add_option("--three", three)->required();
1557+
1558+
run();
1559+
1560+
CHECK(two == std::vector<int>({2, 3}));
1561+
CHECK(three == std::vector<int>({1, 2, 3}));
1562+
}
1563+
15131564
TEST_CASE_METHOD(TApp, "ColonValueSep", "[config]") {
15141565

15151566
TempFile tmpini{"TestIniTmp.ini"};
@@ -1700,6 +1751,30 @@ TEST_CASE_METHOD(TApp, "TOMLStringVector", "[config]") {
17001751
CHECK(three == std::vector<std::string>({"1", "2", "3"}));
17011752
}
17021753

1754+
TEST_CASE_METHOD(TApp, "TOMLStringVectorMultiline", "[config]") {
1755+
1756+
TempFile tmptoml{"TestTomlTmp.toml"};
1757+
1758+
app.set_config("--config", tmptoml);
1759+
1760+
{
1761+
std::ofstream out{tmptoml};
1762+
out << "#this is a comment line\n";
1763+
out << "[default]\n";
1764+
out << "two=[\n\t\t\"2\",\"3\"]\n";
1765+
out << "three=[\n \"1\",\n \"2\",\n \"3\"\n] \n";
1766+
}
1767+
1768+
std::vector<std::string> two, three;
1769+
1770+
app.add_option("--two", two)->required();
1771+
app.add_option("--three", three)->required();
1772+
1773+
run();
1774+
CHECK(two == std::vector<std::string>({"2", "3"}));
1775+
CHECK(three == std::vector<std::string>({"1", "2", "3"}));
1776+
}
1777+
17031778
TEST_CASE_METHOD(TApp, "IniVectorCsep", "[config]") {
17041779

17051780
TempFile tmpini{"TestIniTmp.ini"};

0 commit comments

Comments
 (0)