mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Better mixing of positionals and unlimited options (#102)
This commit is contained in:
parent
b7c031cc3e
commit
9d41ddef83
@ -211,7 +211,7 @@ Extra positional arguments will cause the program to exit, so at least one posit
|
||||
If you set `.allow_extras()` on the main `App`, you will not get an error. You can access the missing options using `remaining` (if you have subcommands, `app.remaining(true)` will get all remaining options, subcommands included).
|
||||
|
||||
You can access a vector of pointers to the parsed options in the original order using `parse_order()`.
|
||||
If `--` is present in the command line,
|
||||
If `--` is present in the command line that does not end an unlimited option, then
|
||||
everything after that is positional only.
|
||||
|
||||
|
||||
|
@ -1461,12 +1461,6 @@ class App {
|
||||
// If any positionals remain, don't keep eating
|
||||
if(_count_remaining_positionals() > 0)
|
||||
break;
|
||||
|
||||
// If there are any unlimited positionals, those also take priority
|
||||
if(std::any_of(std::begin(options_), std::end(options_), [](const Option_p &opt) {
|
||||
return opt->get_positional() && opt->get_items_expected() < 0;
|
||||
}))
|
||||
break;
|
||||
}
|
||||
op->add_result(args.back());
|
||||
parse_order_.push_back(op.get());
|
||||
|
@ -542,9 +542,23 @@ TEST_F(TApp, RequiredOptsUnlimited) {
|
||||
app.allow_extras(false);
|
||||
std::vector<std::string> remain;
|
||||
app.add_option("positional", remain);
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"one", "two"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>());
|
||||
|
||||
app.reset();
|
||||
args = {"--str", "one", "--", "two"};
|
||||
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"one"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>({"two"}));
|
||||
|
||||
app.reset();
|
||||
args = {"one", "--str", "two"};
|
||||
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"two"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>({"one"}));
|
||||
}
|
||||
|
||||
TEST_F(TApp, RequiredOptsUnlimitedShort) {
|
||||
@ -576,9 +590,23 @@ TEST_F(TApp, RequiredOptsUnlimitedShort) {
|
||||
app.allow_extras(false);
|
||||
std::vector<std::string> remain;
|
||||
app.add_option("positional", remain);
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"one", "two"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>());
|
||||
|
||||
app.reset();
|
||||
args = {"-s", "one", "--", "two"};
|
||||
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"one"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>({"two"}));
|
||||
|
||||
app.reset();
|
||||
args = {"one", "-s", "two"};
|
||||
|
||||
run();
|
||||
EXPECT_EQ(strs, std::vector<std::string>({"two"}));
|
||||
EXPECT_EQ(remain, std::vector<std::string>({"one"}));
|
||||
}
|
||||
|
||||
TEST_F(TApp, OptsUnlimitedEnd) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user