clipp
clipp copied to clipboard
Weird problem with order of elements in the cli spec
This program gives:
test(false)=Failed test(true)=OK
With HEAD on Linux Ubuntu with gcc 9.2.1 and clang 9.0.0.
I don't think the changein order should matter.
#include "clipp.h"
#include
using namespace clipp; using namespace std;
enum class Mode { Unknown, Error, Help, DeviceList, DeviceInfo };
bool test_(int argc, char* argv[], bool order) { Mode m = Mode::Unknown; string target;
auto helpMode = (
command("help").set(m, Mode::Help)
);
auto deviceListMode = (
command("devicelist")
.set(m, Mode::DeviceList)
.doc("List available hardware devices")
);
auto deviceInfoMode = (
command("deviceinfo")
.set(m, Mode::DeviceInfo)
.doc("Display information about a hardware device")
,
value("device", target)
.doc("Device name to provide information for")
);
auto cli = order ? (
helpMode | deviceInfoMode | deviceListMode
) : (
helpMode | deviceListMode | deviceInfoMode
);
return (bool)clipp::parse(argc, argv, cli);
}
const char* test(bool order) { static char* argv[] = { (char*)"app", (char*)"deviceinfo", (char*)"foo", nullptr };
auto rc = test_(3, argv, order);
return rc ? "OK" : "Failed";
}
int main(int, char*[]) { std::cout << "test(false)=" << test(false) << std::endl; std::cout << "test(true)=" << test(true) << std::endl; return 0; } <<<<<<< bug.cpp.gz
I came here because I think I have the same thing. I spent quite some time messing with stuff because:
const auto genMode = (command("gen", "generate").set(ret.selected, mode::gen).doc("Generate a post template in the current folder. Edit this file afterwards to add a body.") &
(
repeatable(in_sequence(option("-d", "--description") & value("file description", ret.gen_opt.post.descriptions))).doc("Associate this description with the corresponding file."),
repeatable(in_sequence(option("-f", "--file", "--attach") & value("file path", ret.gen_opt.post.attachments))).doc("Attach these files to the post."),
in_sequence(option("-o", "--output"), value("filename", ret.gen_opt.filename)).doc("Specify an output file. Default is new_post."),
in_sequence(option("-r", "--reply-to"), value("reply_to", ret.gen_opt.post.reply_to_id)).doc("Reply to the specified post ID."),
in_sequence(option("-i", "--reply-id"), value("id", ret.gen_opt.post.reply_id)).doc("Set an ID so that this post can be replied to with --reply-to."),
in_sequence(option("-c", "--content-warning", "--cw"), value("warning", ret.gen_opt.post.content_warning)).doc("Set a content warning (or subject) for the post."),
in_sequence(option("-p", "--privacy", "--visibility"), visibilities).doc("Set the post's visibility."),
in_sequence(option("-b", "--body", "--content"), value("body", ret.gen_opt.post.text)).doc("Specify a body for the post.")
) % "generate options");
creates this situation where options that are later in the list can block earlier ones (for example, generate -o filename -r 1234567
fails to parse), but the following, with the last two swapped around, works fine:
const auto genMode = (command("gen", "generate").set(ret.selected, mode::gen).doc("Generate a post template in the current folder. Edit this file afterwards to add a body.") &
(
repeatable(in_sequence(option("-d", "--description") & value("file description", ret.gen_opt.post.descriptions))).doc("Associate this description with the corresponding file."),
repeatable(in_sequence(option("-f", "--file", "--attach") & value("file path", ret.gen_opt.post.attachments))).doc("Attach these files to the post."),
in_sequence(option("-o", "--output"), value("filename", ret.gen_opt.filename)).doc("Specify an output file. Default is new_post."),
in_sequence(option("-r", "--reply-to"), value("reply_to", ret.gen_opt.post.reply_to_id)).doc("Reply to the specified post ID."),
in_sequence(option("-i", "--reply-id"), value("id", ret.gen_opt.post.reply_id)).doc("Set an ID so that this post can be replied to with --reply-to."),
in_sequence(option("-c", "--content-warning", "--cw"), value("warning", ret.gen_opt.post.content_warning)).doc("Set a content warning (or subject) for the post."),
in_sequence(option("-b", "--body", "--content"), value("body", ret.gen_opt.post.text)).doc("Specify a body for the post."),
in_sequence(option("-p", "--privacy", "--visibility"), visibilities).doc("Set the post's visibility.")
) % "generate options");