MCPcopy Create free account
hub / github.com/CLIUtils/CLI11 / main

Function main

examples/inter_argument_order.cpp:14–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include <vector>
13
14int main(int argc, char **argv) {
15 CLI::App app{"An app to practice mixing unlimited arguments, but still recover the original order."};
16
17 std::vector<int> foos;
18 auto *foo = app.add_option("--foo,-f", foos, "Some unlimited argument");
19
20 std::vector<int> bars;
21 auto *bar = app.add_option("--bar", bars, "Some unlimited argument");
22
23 app.add_flag("--z,--x", "Random other flags");
24
25 // Standard parsing lines (copy and paste in, or use CLI11_PARSE)
26 try {
27 app.parse(argc, argv);
28 } catch(const CLI::ParseError &e) {
29 return app.exit(e);
30 }
31
32 // I prefer using the back and popping
33 std::reverse(std::begin(foos), std::end(foos));
34 std::reverse(std::begin(bars), std::end(bars));
35
36 std::vector<std::pair<std::string, int>> keyval;
37 for(auto *option : app.parse_order()) {
38 if(option == foo) {
39 keyval.emplace_back("foo", foos.back());
40 foos.pop_back();
41 }
42 if(option == bar) {
43 keyval.emplace_back("bar", bars.back());
44 bars.pop_back();
45 }
46 }
47
48 // Prove the vector is correct
49 for(auto &pair : keyval) {
50 std::cout << pair.first << " : " << pair.second << '\n';
51 }
52}

Callers

nothing calls this directly

Calls 4

add_flagMethod · 0.80
parseMethod · 0.80
exitMethod · 0.80
add_optionMethod · 0.45

Tested by

no test coverage detected