-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme_example.cpp
106 lines (77 loc) · 3.34 KB
/
readme_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "args_all.hpp"
int main(int argc, char** argv) {
using namespace args::condition;
auto flag = args::flag('f', "flag")
.description("a flag"); // description to show in the help message
auto integer_flag = args::flag<int>("integer")
.metavars("INT") // parameter name to display in the help message
.description("takes an integer");
auto multi_param_flag = args::flag<int,bool,std::string>('m', "multi-param")
.metavars("INT BOOL STR")
.description("this is used to show how multiple parameters can be passed");
auto requires_flag = args::flag('r', "requires-flag", True(), requires(flag))
.description("does nothing, but requires -f or --flag");
auto help_flag = args::flag('h', "help", True(), max(0))
.description("print this message and exit");
auto once_flag = args::flag('o', "once", True(), min(1))
.description("can only be specified once");
enum class Command { BUILD, RUN };
auto command = args::map_lookup_arg<Command>({
{ "build", Command::BUILD },
{ "run", Command::RUN }
}, max(0), min(1))
.description("available commands");
enum class Target { ALL, CURRENT };
auto target = args::map_lookup_arg<Target>({
{ "all", Target::ALL },
{ "current", Target::CURRENT }
}, all(after(command), max(0)), min(1))
.description("available targets");
// flags require the flags() parser because they need extra logic for shortopts
// and the endopts mark. anything else can be in a args() parser.
// order in which they are specified is the order in which the parser will
// try to parse them.
auto flag_parser = args::flags(flag, integer_flag, multi_param_flag,
requires_flag, help_flag, once_flag);
auto argument_parser = args::args(command, target);
// takes two InputIterators (args_begin and args_end). value_type must be
// convertible to args::StrView (const char* or std::string)
auto result = args::parse(argv+1, argv+argc, flag_parser, argument_parser);
using std::cout;
using std::boolalpha;
if (result.is_ok()) {
cout << "Flag has been given " << flag.multiplicity << " times\n";
cout << "Found --integer values: ";
// .result is a vector<T>. In the case of flags, T is a tuple
// of the parameter types, in this case, tuple<int>
for (const auto& tup : integer_flag.result)
cout << std::get<0>(tup) << ' ';
cout << "\n";
cout << "Found --multi-param values: ";
for (const auto& tup : multi_param_flag.result)
cout << '(' << boolalpha << std::get<0>(tup) << ','
<< ' ' << boolalpha << std::get<1>(tup) << ','
<< ' ' << boolalpha << std::get<2>(tup) << ") ";
cout << "\n";
cout << "Found -r or --requires-flag? " << boolalpha <<
(bool) requires_flag << '\n';
cout << "Command is BUILD? " << boolalpha <<
(command.result.front() == Command::BUILD) << '\n';
cout << "Target is ALL? " << boolalpha <<
(target.result.front() == Target::ALL) << '\n';
} else if (help_flag) {
cout << "Help flag found\n";
cout << "Usage: "
<< argv[0] << " <flags> COMMAND <flags> TARGET <flags>"
<< "\n\n";
cout << "Fixed arguments:\n";
// indent with two spaces and put the description 4 spaces away
args::print_help(argument_parser, {" ", 4}, cout);
cout << "\n";
cout << "Flags:\n";
args::print_help(flag_parser, {" ", 4}, cout);
} else {
cout << "Error ocurred:\n";
args::report_error(result, cout);
}
}