Skip to content

Commit

Permalink
Adding test and fix for #90
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Mar 25, 2018
1 parent 2ef176e commit 5c9fae3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## In progress

* Fix unlimited short options eating two values before checking for positionals when no space present #90

## Version 1.4: More feedback

This version adds lots of smaller fixes and additions after the refactor in version 1.3. More ways to download and use CLI11 in CMake have been added. INI files have improved support.
Expand Down
11 changes: 9 additions & 2 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,25 +1422,32 @@ class App {

int num = op->get_expected();

// Make sure we always eat the minimum for unlimited vectors
int collected = 0;

// --this=value
if(!value.empty()) {
if(num != -1)
// If exact number expected
if(num > 0)
num--;
op->add_result(value);
parse_order_.push_back(op.get());
collected += 1;
} else if(num == 0) {
op->add_result("");
parse_order_.push_back(op.get());
// -Trest
} else if(!rest.empty()) {
if(num > 0)
num--;
op->add_result(rest);
parse_order_.push_back(op.get());
rest = "";
collected += 1;
}

// Unlimited vector parser
if(num < 0) {
int collected = 0; // Make sure we always eat the minimum
while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) {
if(collected >= -num) {
// We could break here for allow extras, but we don't
Expand Down
24 changes: 24 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,30 @@ TEST_F(TApp, BigPositional) {
EXPECT_EQ(args, vec);
}

// This makes sure unlimited option priority is
// correct for space vs. no space #90
TEST_F(TApp, PositionalNoSpace) {
std::vector<std::string> options;
std::string foo, bar;

app.add_option("-O", options);
app.add_option("foo", foo)->required();
app.add_option("bar", bar)->required();

args = {"-O", "Test", "param1", "param2"};
run();

EXPECT_EQ(options.size(), (size_t) 1);
EXPECT_EQ(options.at(0), "Test");

app.reset();
args = {"-OTest", "param1", "param2"};
run();

EXPECT_EQ(options.size(), (size_t) 1);
EXPECT_EQ(options.at(0), "Test");
}

TEST_F(TApp, Reset) {

app.add_flag("--simple");
Expand Down

0 comments on commit 5c9fae3

Please sign in to comment.