aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Belikov <pavel.fuchs.belikov@gmail.com>2017-11-14 21:33:31 +0300
committerPavel Belikov <pavel.fuchs.belikov@gmail.com>2017-11-14 22:31:06 +0300
commit01955238036ac546ba1e488546101f65d213f4dd (patch)
tree924b4d0f14c2e7f28a8cab616b244b00c61326dc
parentadd more HelpParams for options (diff)
downloadargs.hxx-01955238036ac546ba1e488546101f65d213f4dd.tar.xz
add leading whitespace support in Wrap
-rw-r--r--args.hxx39
-rw-r--r--test.cxx32
2 files changed, 56 insertions, 15 deletions
diff --git a/args.hxx b/args.hxx
index 655ef12..a381435 100644
--- a/args.hxx
+++ b/args.hxx
@@ -114,37 +114,48 @@ namespace args
std::istringstream stream(in);
std::vector<std::string> output;
- std::ostringstream line;
- std::string::size_type linesize = 0;
+ std::string line;
+ bool empty = true;
+
+ for (char c : in)
+ {
+ if (!isspace(c))
+ {
+ break;
+ }
+ line += c;
+ }
+
while (stream)
{
std::string item;
stream >> item;
auto itemsize = Glyphs(item);
- if ((linesize + 1 + itemsize) > currentwidth)
+ if ((line.length() + 1 + itemsize) > currentwidth)
{
- if (linesize > 0)
+ if (!empty)
{
- output.push_back(line.str());
- line.str(std::string());
- linesize = 0;
+ output.push_back(line);
+ line.clear();
+ empty = true;
currentwidth = width;
}
}
if (itemsize > 0)
{
- if (linesize)
+ if (!empty)
{
- ++linesize;
- line << " ";
+ line += ' ';
}
- line << item;
- linesize += itemsize;
+
+ line += item;
+ empty = false;
}
}
- if (linesize > 0)
+
+ if (!empty)
{
- output.push_back(line.str());
+ output.push_back(line);
}
return output;
}
diff --git a/test.cxx b/test.cxx
index 2c4cd6e..991d98e 100644
--- a/test.cxx
+++ b/test.cxx
@@ -968,7 +968,7 @@ TEST_CASE("HelpParams work as expected", "[args]")
{
args::ArgumentParser p("parser");
args::ValueFlag<std::string> f(p, "name", "description", {'f', "foo"});
- args::ValueFlag<std::string> g(p, "name", "description", {'g'});
+ args::ValueFlag<std::string> g(p, "name", "description\n d1\n d2", {'g'});
p.Prog("prog");
REQUIRE(p.Help() == R"( prog {OPTIONS}
@@ -979,6 +979,8 @@ TEST_CASE("HelpParams work as expected", "[args]")
-f[name], --foo=[name] description
-g[name] description
+ d1
+ d2
)");
@@ -993,6 +995,8 @@ TEST_CASE("HelpParams work as expected", "[args]")
-f, --foo [name] description
-g[name] description
+ d1
+ d2
)");
@@ -1004,6 +1008,8 @@ TEST_CASE("HelpParams work as expected", "[args]")
-f, --foo description
-g description
+ d1
+ d2
)");
@@ -1018,6 +1024,8 @@ TEST_CASE("HelpParams work as expected", "[args]")
-f, --foo
description
-g description
+ d1
+ d2
)");
@@ -1032,8 +1040,30 @@ TEST_CASE("HelpParams work as expected", "[args]")
description
-g
description
+ d1
+ d2
)");
+
+ args::ValueFlag<std::string> e(p, "name", "some reaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaally loooooooooooooooooooooooooooong description", {'e'});
+ REQUIRE(p.Help() == R"( usage: prog {OPTIONS}
+
+ parser
+
+ Options
+
+ -f, --foo
+ description
+ -g
+ description
+ d1
+ d2
+ -e
+ some reaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaally
+ loooooooooooooooooooooooooooong description
+
+)");
+
}
#undef ARGS_HXX