1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Move to using remaining, drop return from parse

This commit is contained in:
Henry Fredrick Schreiner 2017-11-16 11:59:39 -05:00 committed by Henry Schreiner
parent fc35014dad
commit cb7c3ff85a
5 changed files with 29 additions and 28 deletions

View File

@ -8,13 +8,14 @@ int main(int argc, char **argv) {
std::vector<int> vals; std::vector<int> vals;
app.add_option("--vals,-v", vals)->expected(1); app.add_option("--vals,-v", vals)->expected(1);
std::vector<std::string> more_comms;
try { try {
more_comms = app.parse(argc, argv); app.parse(argc, argv);
} catch(const CLI::ParseError &e) { } catch(const CLI::ParseError &e) {
return app.exit(e); return app.exit(e);
} }
std::vector<std::string> more_comms = app.remaining();
std::cout << "Prefix:"; std::cout << "Prefix:";
for(int v : vals) for(int v : vals)
std::cout << v << ":"; std::cout << v << ":";

View File

@ -626,21 +626,20 @@ class App {
/// Parses the command line - throws errors /// Parses the command line - throws errors
/// This must be called after the options are in but before the rest of the program. /// 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]; name_ = argv[0];
std::vector<std::string> args; std::vector<std::string> args;
for(int i = argc - 1; i > 0; i--) for(int i = argc - 1; i > 0; i--)
args.emplace_back(argv[i]); args.emplace_back(argv[i]);
return parse(args); parse(args);
} }
/// The real work is done here. Expects a reversed vector. /// The real work is done here. Expects a reversed vector.
/// Changes the vector to the remaining options. /// 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(); _validate();
_parse(args); _parse(args);
run_callback(); run_callback();
return args;
} }
/// Print a nice error message and return the exit code /// Print a nice error message and return the exit code
@ -872,6 +871,15 @@ class App {
return miss_list; 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: protected:
@ -1020,17 +1028,10 @@ class App {
// Convert missing (pairs) to extras (string only) // Convert missing (pairs) to extras (string only)
if(parent_ == nullptr) { if(parent_ == nullptr) {
args.resize(missing()->size()); args = remaining();
std::transform(std::begin(*missing()),
std::end(*missing()),
std::begin(args),
[](const std::pair<detail::Classifer, std::string> &val) { return val.second; });
std::reverse(std::begin(args), std::end(args)); std::reverse(std::begin(args), std::end(args));
size_t num_left_over = std::count_if( size_t num_left_over = remaining_size();
std::begin(*missing()), std::end(*missing()), [](std::pair<detail::Classifer, std::string> &val) {
return val.first != detail::Classifer::POSITIONAL_MARK;
});
if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) if(num_left_over > 0 && !(allow_extras_ || prefix_command_))
throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); throw ExtrasError("[" + detail::rjoin(args, " ") + "]");

View File

@ -917,10 +917,10 @@ TEST_F(TApp, AllowExtras) {
EXPECT_FALSE(val); EXPECT_FALSE(val);
args = {"-x", "-f"}; args = {"-x", "-f"};
std::vector<std::string> left_over;
EXPECT_NO_THROW({ left_over = run(); }); EXPECT_NO_THROW(run());
EXPECT_TRUE(val); 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) { TEST_F(TApp, AllowExtrasOrder) {
@ -928,14 +928,13 @@ TEST_F(TApp, AllowExtrasOrder) {
app.allow_extras(); app.allow_extras();
args = {"-x", "-f"}; args = {"-x", "-f"};
std::vector<std::string> left_over; EXPECT_NO_THROW(run());
EXPECT_NO_THROW({ left_over = run(); }); EXPECT_EQ(app.remaining(), std::vector<std::string>({"-x", "-f"}));
EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over);
app.reset(); app.reset();
std::vector<std::string> left_over_2; std::vector<std::string> left_over = app.remaining();
left_over_2 = app.parse(left_over); app.parse(left_over);
EXPECT_EQ(left_over, left_over_2); EXPECT_EQ(app.remaining(), left_over);
} }
// Test horrible error // Test horrible error

View File

@ -339,9 +339,9 @@ TEST_F(TApp, PrefixProgram) {
app.add_flag("--simple"); app.add_flag("--simple");
args = {"--simple", "other", "--simple", "--mine"}; 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 { struct SubcommandProgram : public TApp {

View File

@ -15,10 +15,10 @@ struct TApp : public ::testing::Test {
CLI::App app{"My Test Program"}; CLI::App app{"My Test Program"};
input_t args; input_t args;
std::vector<std::string> run() { void run() {
input_t newargs = args; input_t newargs = args;
std::reverse(std::begin(newargs), std::end(newargs)); std::reverse(std::begin(newargs), std::end(newargs));
return app.parse(newargs); app.parse(newargs);
} }
}; };