clipp
clipp copied to clipboard
Group of single parameter
Hi,
I am playing around this lib right now and found it is great.
I wanted to created a group of one prarameter:
cli = (
"group 1" % (
option("-a").set(a) % "option a"
),
"group 2" % (
option("-b").set(b) % "option b",
option("-c").set(c) % "option c"
)
)
but the documentation generated from this is incorrect:
-a group1
group2
-b option b
-c option c
The problem is that when there is only one entry in the parenthesis, no group is created. I tried doing clipp::group(option("-a").set(a) % "option a")
but it seems does not fix the problem.
Any idea?
I see the problem. Unfortunately, the expressions ( object )
and object
are equivalent and C++ doesn't offer any way to distinguish between them.
But the following works for me:
auto cli = (
"group 1" % group(
option("-a").set(a) % "option a"
),
"group 2" % (
option("-b").set(b) % "option b",
option("-c").set(c) % "option c"
)
);
which results in the following documentation:
group 1
-a option a
group 2
-b option b
-c option c
There is another "hacky" solution:
auto cli = (
"group 1" % (
option("-a").set(a) % "option a",
option("") // <-- empty option forces group
),
"group 2" % (
option("-b").set(b) % "option b",
option("-c").set(c) % "option c"
)
);
@muellan
But the following works for me:
"group doc" % group(option(...) % "opt doc")
for some reason does not work for me. After looking at the source code, I found the following work around: "group doc" % group("", option(...) % "opt doc")
.
Hm, that's odd. What compiler do you use?
It turns out that my initial example was not the one that has the problem. Below is the correct MWE:
// main.cpp
#include <iostream>
#include "include/clipp.h"
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
using namespace clipp; using std::cout; using std::string;
bool a, b, c;
auto cli = (
"group 1" % group(
"", // this is needed to avoid calling the copy c'tor
(option("-a").set(a) & value("A")) % "option a"
),
"group 2" % (
option("-b").set(b) % "option b",
option("-c").set(c) % "option c"
)
);
cout << make_man_page(cli, argv[0]);
}
run with clang main.cpp -std=c++17 -lc++ && ./a.out
As pointed out in the inline comment, the outer group(...)
will resolve to the copy constructor if the inner item is an option(...) & value(...)
pair (i.e., a group). The empty docstring ""
has to be used to make sure a new group is created.
Ah - that makes sense. Right now I can't think of a way to resolve this other than using the hack with an additional empty option or empty string.