mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Move to using remaining, drop return from parse
This commit is contained in:
parent
fc35014dad
commit
cb7c3ff85a
@ -8,13 +8,14 @@ int main(int argc, char **argv) {
|
||||
std::vector<int> vals;
|
||||
app.add_option("--vals,-v", vals)->expected(1);
|
||||
|
||||
std::vector<std::string> more_comms;
|
||||
try {
|
||||
more_comms = app.parse(argc, argv);
|
||||
app.parse(argc, argv);
|
||||
} catch(const CLI::ParseError &e) {
|
||||
return app.exit(e);
|
||||
}
|
||||
|
||||
std::vector<std::string> more_comms = app.remaining();
|
||||
|
||||
std::cout << "Prefix:";
|
||||
for(int v : vals)
|
||||
std::cout << v << ":";
|
||||
|
@ -626,21 +626,20 @@ class App {
|
||||
|
||||
/// Parses the command line - throws errors
|
||||
/// This must be called after the options are in but before the rest of the program.
|
||||
std::vector<std::string> parse(int argc, char **argv) {
|
||||
void parse(int argc, char **argv) {
|
||||
name_ = argv[0];
|
||||
std::vector<std::string> args;
|
||||
for(int i = argc - 1; i > 0; i--)
|
||||
args.emplace_back(argv[i]);
|
||||
return parse(args);
|
||||
parse(args);
|
||||
}
|
||||
|
||||
/// The real work is done here. Expects a reversed vector.
|
||||
/// Changes the vector to the remaining options.
|
||||
std::vector<std::string> &parse(std::vector<std::string> &args) {
|
||||
void parse(std::vector<std::string> &args) {
|
||||
_validate();
|
||||
_parse(args);
|
||||
run_callback();
|
||||
return args;
|
||||
}
|
||||
|
||||
/// Print a nice error message and return the exit code
|
||||
@ -872,6 +871,15 @@ class App {
|
||||
return miss_list;
|
||||
}
|
||||
|
||||
/// This returns the number of remaining options, minus the -- seperator
|
||||
size_t remaining_size() const {
|
||||
return std::count_if(
|
||||
std::begin(missing_), std::end(missing_),
|
||||
[](const std::pair<detail::Classifer, std::string> &val) {
|
||||
return val.first != detail::Classifer::POSITIONAL_MARK;
|
||||
});
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
protected:
|
||||
@ -1020,17 +1028,10 @@ class App {
|
||||
|
||||
// Convert missing (pairs) to extras (string only)
|
||||
if(parent_ == nullptr) {
|
||||
args.resize(missing()->size());
|
||||
std::transform(std::begin(*missing()),
|
||||
std::end(*missing()),
|
||||
std::begin(args),
|
||||
[](const std::pair<detail::Classifer, std::string> &val) { return val.second; });
|
||||
args = remaining();
|
||||
std::reverse(std::begin(args), std::end(args));
|
||||
|
||||
size_t num_left_over = std::count_if(
|
||||
std::begin(*missing()), std::end(*missing()), [](std::pair<detail::Classifer, std::string> &val) {
|
||||
return val.first != detail::Classifer::POSITIONAL_MARK;
|
||||
});
|
||||
size_t num_left_over = remaining_size();
|
||||
|
||||
if(num_left_over > 0 && !(allow_extras_ || prefix_command_))
|
||||
throw ExtrasError("[" + detail::rjoin(args, " ") + "]");
|
||||
|
@ -917,10 +917,10 @@ TEST_F(TApp, AllowExtras) {
|
||||
EXPECT_FALSE(val);
|
||||
|
||||
args = {"-x", "-f"};
|
||||
std::vector<std::string> left_over;
|
||||
EXPECT_NO_THROW({ left_over = run(); });
|
||||
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_TRUE(val);
|
||||
EXPECT_EQ(std::vector<std::string>({"-x"}), left_over);
|
||||
EXPECT_EQ(app.remaining(), std::vector<std::string>({"-x"}));
|
||||
}
|
||||
|
||||
TEST_F(TApp, AllowExtrasOrder) {
|
||||
@ -928,14 +928,13 @@ TEST_F(TApp, AllowExtrasOrder) {
|
||||
app.allow_extras();
|
||||
|
||||
args = {"-x", "-f"};
|
||||
std::vector<std::string> left_over;
|
||||
EXPECT_NO_THROW({ left_over = run(); });
|
||||
EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over);
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(app.remaining(), std::vector<std::string>({"-x", "-f"}));
|
||||
app.reset();
|
||||
|
||||
std::vector<std::string> left_over_2;
|
||||
left_over_2 = app.parse(left_over);
|
||||
EXPECT_EQ(left_over, left_over_2);
|
||||
std::vector<std::string> left_over = app.remaining();
|
||||
app.parse(left_over);
|
||||
EXPECT_EQ(app.remaining(), left_over);
|
||||
}
|
||||
|
||||
// Test horrible error
|
||||
|
@ -339,9 +339,9 @@ TEST_F(TApp, PrefixProgram) {
|
||||
app.add_flag("--simple");
|
||||
|
||||
args = {"--simple", "other", "--simple", "--mine"};
|
||||
auto ret_args = run();
|
||||
run();
|
||||
|
||||
EXPECT_EQ(ret_args, std::vector<std::string>({"--mine", "--simple", "other"}));
|
||||
EXPECT_EQ(app.remaining(), std::vector<std::string>({"other", "--simple", "--mine"}));
|
||||
}
|
||||
|
||||
struct SubcommandProgram : public TApp {
|
||||
|
@ -15,10 +15,10 @@ struct TApp : public ::testing::Test {
|
||||
CLI::App app{"My Test Program"};
|
||||
input_t args;
|
||||
|
||||
std::vector<std::string> run() {
|
||||
void run() {
|
||||
input_t newargs = args;
|
||||
std::reverse(std::begin(newargs), std::end(newargs));
|
||||
return app.parse(newargs);
|
||||
app.parse(newargs);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user