1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Adding fail if required values missing

This commit is contained in:
Henry Fredrick Schreiner 2017-11-25 19:03:03 -05:00
parent d28c230e0b
commit 3d309dc0be
5 changed files with 37 additions and 5 deletions

View File

@ -8,7 +8,10 @@ int main(int argc, char **argv) {
CLI::Option *opt = app.add_option("-f,--file,file", file, "File name");
int count;
CLI::Option *copt = app.add_flag("-c,--count", count, "Counter");
CLI::Option *copt = app.add_option("-c,--count", count, "Counter");
int v;
CLI::Option *flag = app.add_flag("--flag", v, "Some flag that can be passed multiple times");
double value; // = 3.14;
app.add_option("-d,--double", value, "Some Value");
@ -19,6 +22,7 @@ int main(int argc, char **argv) {
<< ", opt count: " << opt->count() << std::endl;
std::cout << "Working on count: " << count << ", direct count: " << app.count("--count")
<< ", opt count: " << copt->count() << std::endl;
std::cout << "Recieved flag: " << v << " (" << flag->count() << ") times\n";
std::cout << "Some value: " << value << std::endl;
return 0;

View File

@ -1196,7 +1196,9 @@ class App {
if(num_left_over > 0) {
args = remaining(false);
std::reverse(std::begin(args), std::end(args));
throw ExtrasError("[" + detail::rjoin(args, " ") + "]");
throw ExtrasError((args.size() > 1 ? "The following argument was not expected: "
: "The following arguments were not expected: ") +
detail::rjoin(args, " "));
}
}
}
@ -1415,7 +1417,7 @@ class App {
args.pop_back();
already_ate_one = true;
}
} else
} else {
while(num > 0 && !args.empty()) {
num--;
std::string current_ = args.back();
@ -1424,6 +1426,12 @@ class App {
parse_order_.push_back(op.get());
}
if(num > 0) {
throw RequiredError(op->single_name() + ": " + std::to_string(num) + " required " +
op->get_type_name() + " missing");
}
}
if(!rest.empty()) {
rest = "-" + rest;
args.push_back(rest);
@ -1499,6 +1507,10 @@ class App {
parse_order_.push_back(op.get());
args.pop_back();
}
if(num > 0) {
throw RequiredError(op->single_name() + ": " + std::to_string(num) + " required " +
op->get_type_name() + " missing");
}
}
return;
}

View File

@ -551,6 +551,9 @@ class Option : public OptionBase<Option> {
/// Set the type name displayed on this option
void set_type_name(std::string val) { typeval_ = val; }
/// Get the typename for this option
std::string get_type_name() const { return typeval_; }
///@}
protected:

View File

@ -230,6 +230,19 @@ TEST_F(TApp, TakeLastOpt) {
EXPECT_EQ(str, "two");
}
TEST_F(TApp, MissingValueNonRequiredOpt) {
int count;
app.add_option("-c,--count", count);
args = {"-c"};
EXPECT_ANY_THROW(run());
app.reset();
args = {"--count"};
EXPECT_ANY_THROW(run());
}
TEST_F(TApp, RequiredOptsSingle) {
std::string str;
@ -1189,7 +1202,7 @@ TEST_F(TApp, OrderedModifingTransforms) {
TEST_F(TApp, ThrowingTransform) {
std::string val;
auto m = app.add_option("-m,--mess", val);
m->transform([](std::string x) -> std::string { throw CLI::ValidationError("My Message"); });
m->transform([](std::string) -> std::string { throw CLI::ValidationError("My Message"); });
EXPECT_NO_THROW(run());
app.reset();

View File

@ -91,5 +91,5 @@ TEST_F(TApp, BuiltinComplexFail) {
args = {"-c", "4"};
EXPECT_THROW(run(), CLI::ConversionError);
EXPECT_THROW(run(), CLI::RequiredError);
}