prefix_command tests and improvements (#1266)

adding a PrefixCommandMode option to the prefix_command to allow
specification of a separator and catch other errors

Addresses #1264

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top
2025-12-17 06:26:16 -08:00
committed by GitHub
parent 6919ea8624
commit fe8f9f7db3
5 changed files with 106 additions and 35 deletions

View File

@@ -2427,6 +2427,57 @@ TEST_CASE_METHOD(TApp, "AllowExtrasCascade", "[app]") {
CHECK(45 == v1);
CHECK(27 == v2);
}
TEST_CASE_METHOD(TApp, "PrefixCommand", "[app]") {
int v1{0};
int v2{0};
app.add_option("-f", v1);
app.add_option("-x", v2);
app.prefix_command();
args = {"-x", "45", "-f", "27"};
run();
auto rem = app.remaining();
CHECK(rem.empty());
args = {"-x", "45", "-f", "27", "--test", "23"};
run();
rem = app.remaining();
CHECK(rem.size() == 2U);
args = {"-x", "45", "-f", "27", "--", "--test", "23"};
run();
rem = app.remaining();
CHECK(rem.size() == 3U);
args = {"-x", "45", "--test4", "-f", "27", "--test", "23"};
run();
rem = app.remaining();
CHECK(rem.size() == 5U);
app.prefix_command(CLI::PrefixCommandMode::SeparatorOnly);
CHECK_THROWS_AS(run(), CLI::ExtrasError);
args = {"-x", "45", "positional", "-f", "27", "--test", "23"};
CHECK_THROWS_AS(run(), CLI::ExtrasError);
args = {"-x", "45", "-f", "27", "--", "--test", "23"};
run();
rem = app.remaining();
CHECK(rem.size() == 3U);
args = {"-x", "45", "--test4", "-f", "27", "--", "--test", "23"};
CHECK_THROWS_AS(run(), CLI::ExtrasError);
app.allow_extras(true);
run();
rem = app.remaining();
CHECK(rem.size() == 4U);
}
// makes sure the error throws on the rValue version of the parse
TEST_CASE_METHOD(TApp, "ExtrasErrorRvalueParse", "[app]") {