Skip to content

Commit 5a9445d

Browse files
author
Fytch
committed
make examples easier to understand
1 parent f1b7a75 commit 5a9445d

File tree

9 files changed

+84
-81
lines changed

9 files changed

+84
-81
lines changed

README.md

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ The following snippet is the complete source code of a simple program expecting
9696

9797
int main(int argc, char** argv) {
9898
po::parser parser;
99-
parser["optimization"] // corresponds to --optimization
100-
.abbreviation('O') // corresponds to -O
101-
.type(po::u32); // expects an unsigned 32-bit integer
99+
auto& O = parser["optimization"] // corresponds to --optimization
100+
.abbreviation('O') // corresponds to -O
101+
.type(po::u32); // expects an unsigned 32-bit integer
102102

103-
parser(argc, argv); // parses the command line arguments
103+
parser(argc, argv); // parses the command line arguments
104104

105-
auto&& O = parser["optimization"];
106105
if(!O.available())
107106
std::cout << "no optimization level set!\n";
108107
else
@@ -134,26 +133,25 @@ By calling the method `.multi()` we're telling the library to store *all* values
134133
135134
int main(int argc, char** argv) {
136135
po::parser parser;
137-
parser["optimization"]
138-
.abbreviation('O')
139-
.type(po::u32)
140-
.fallback(0); // if --optimization is not explicitly specified, assume 0
136+
auto& O = parser["optimization"] // corresponds to --optimization
137+
.abbreviation('O') // corresponds to -O
138+
.type(po::u32) // expects an unsigned 32-bit integer
139+
.fallback(0); // if --optimization is not explicitly specified, assume 0
141140
142-
parser["include-path"] // corresponds to --include-path
143-
.abbreviation('I') // corresponds to -I
144-
.type(po::string) // expects a string
145-
.multi(); // allows multiple arguments for the same option
141+
auto& I = parser["include-path"] // corresponds to --include-path
142+
.abbreviation('I') // corresponds to -I
143+
.type(po::string) // expects a string
144+
.multi(); // allows multiple arguments for the same option
146145
147-
parser(argc, argv);
146+
parser(argc, argv); // parses the command line arguments
148147
149-
auto&& O = parser["optimization"];
150148
// .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
151149
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
152150
153-
auto&& I = parser["include-path"];
154-
// .size() and .count() return the number of arguments given. Without .multi(), their return value would always be <= 1.
151+
// .size() and .count() return the number of given arguments. Without .multi(), their return value is always <= 1.
155152
std::cout << "include paths (" << I.size() << "):\n";
156-
// Here, the non-template .begin() / .end() methods are being used. Their value type is po::value,
153+
154+
// Here, the non-template .begin() / .end() methods were used. Their value type is po::value,
157155
// which is not a value in itself but contains the desired values as members, i.e. i.string.
158156
for(auto&& i : I)
159157
std::cout << '\t' << i.string << '\n';
@@ -182,25 +180,27 @@ Note that, in order to pass arguments starting with a hyphen to the unnamed para
182180

183181
int main(int argc, char** argv) {
184182
po::parser parser;
185-
parser["optimization"]
183+
auto& O = parser["optimization"]
186184
.abbreviation('O')
187185
.description("set the optimization level (default: -O0)")
188186
.type(po::u32)
189187
.fallback(0);
190188

191-
parser["include-path"]
189+
auto& I = parser["include-path"]
192190
.abbreviation('I')
193191
.description("add an include path")
194192
.type(po::string)
195193
.multi();
196194

197-
parser["help"] // corresponds to --help
198-
.abbreviation('?') // corresponds to -?
195+
auto& help = parser["help"]
196+
.abbreviation('?')
199197
.description("print this help screen")
198+
// .type(po::void_) // redundant; default for named parameters
199+
// .single() // redundant; default for named parameters
200200
.callback([&]{ std::cout << parser << '\n'; });
201201
// callbacks get invoked when the option occurs
202202

203-
parser[""] // the unnamed parameter is used for non-option arguments, i.e. gcc a.c b.c
203+
auto& files = parser[""] // the unnamed parameter is used for non-option arguments as in: gcc a.c b.c
204204
// .type(po::string) // redundant; default for the unnamed parameter
205205
// .multi() // redundant; default for the unnamed parameter
206206
.callback([&](std::string const& x){ std::cout << "processed \'" << x << "\' successfully!\n"; });
@@ -212,29 +212,33 @@ int main(int argc, char** argv) {
212212
return -1;
213213
}
214214
// we don't want to print anything else if the help screen has been displayed
215-
if(parser["help"].size())
215+
if(help.was_set())
216216
return 0;
217217

218-
std::cout << "processed files: " << parser[""].size() << '\n';
218+
std::cout << "processed files: " << files.size() << '\n';
219219

220-
auto&& O = parser["optimization"];
220+
// .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
221221
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
222222

223-
auto&& I = parser["include-path"];
223+
// .size() and .count() return the number of given arguments. Without .multi(), their return value is always <= 1.
224224
std::cout << "include paths (" << I.size() << "):\n";
225+
226+
// Here, the non-template .begin() / .end() methods were used. Their value type is
227+
// po::value, which is not a value in itself but contains the desired values as members, i.e. i.string.
225228
for(auto&& i : I)
226229
std::cout << '\t' << i.string << '\n';
227230
}
231+
228232
```
229233
How the help screen appears:
230234
```
231235
$ ./files --help
232236
Usage:
233237
files.exe [arguments...] [options]
234238
Available options:
239+
-O, --optimization set the optimization level (default: -O0)
235240
-I, --include-path add an include path
236241
-?, --help print this help screen
237-
-O, --optimization set the optimization level (default: -O0)
238242
```
239243
In action:
240244
```
@@ -281,10 +285,10 @@ In this example, we will employ already known mechanics but lay the focus on the
281285
int main(int argc, char** argv) {
282286
po::parser parser;
283287
284-
auto&& x = parser[""]
285-
.type(po::f64) // expects 64-bit floating point numbers
286-
.multi() // allows multiple arguments
287-
.fallback(-8, "+.5e2") // if no arguments were provided, assume these as default
288+
auto& x = parser[""] // the unnamed parameter
289+
.type(po::f64) // expects 64-bit floating point numbers
290+
.multi() // allows multiple arguments
291+
.fallback(-8, "+.5e2") // if no arguments were provided, assume these as default
288292
.callback([&]{ std::cout << "successfully parsed "; })
289293
.callback([&](std::string const& x){ std::cout << x; })
290294
.callback([&]{ std::cout << " which equals "; })
@@ -351,7 +355,7 @@ int main(int argc, char** argv) {
351355
.description("add an include path")
352356
.bind(include_paths); // append paths to the vector 'include_paths'
353357

354-
parser["help"]
358+
auto& help = parser["help"]
355359
.abbreviation('?')
356360
.description("print this help screen");
357361

@@ -363,7 +367,7 @@ int main(int argc, char** argv) {
363367
return -1;
364368

365369
// we don't want to print anything else if the help screen has been displayed
366-
if(parser["help"].was_set()) {
370+
if(help.was_set()) {
367371
std::cout << parser << '\n';
368372
return 0;
369373
}

examples/1 optimization.cxx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
int main(int argc, char** argv) {
55
po::parser parser;
6-
parser["optimization"] // corresponds to --optimization
7-
.abbreviation('O') // corresponds to -O
8-
.type(po::u32); // expects an unsigned 32-bit integer
6+
auto& O = parser["optimization"] // corresponds to --optimization
7+
.abbreviation('O') // corresponds to -O
8+
.type(po::u32); // expects an unsigned 32-bit integer
99

10-
parser(argc, argv); // parses the command line arguments
10+
parser(argc, argv); // parses the command line arguments
1111

12-
auto&& O = parser["optimization"];
1312
if(!O.available())
1413
std::cout << "no optimization level set!\n";
1514
else

examples/2 include.cxx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33

44
int main(int argc, char** argv) {
55
po::parser parser;
6-
parser["optimization"] // corresponds to --optimization
7-
.abbreviation('O') // corresponds to -O
8-
.type(po::u32) // expects an unsigned 32-bit integer
9-
.fallback(0); // if --optimization is not explicitly specified, assume 0
6+
auto& O = parser["optimization"] // corresponds to --optimization
7+
.abbreviation('O') // corresponds to -O
8+
.type(po::u32) // expects an unsigned 32-bit integer
9+
.fallback(0); // if --optimization is not explicitly specified, assume 0
1010

11-
parser["include-path"] // corresponds to --include-path
12-
.abbreviation('I') // corresponds to -I
13-
.type(po::string) // expects a string
14-
.multi(); // allows multiple arguments for the same option
11+
auto& I = parser["include-path"] // corresponds to --include-path
12+
.abbreviation('I') // corresponds to -I
13+
.type(po::string) // expects a string
14+
.multi(); // allows multiple arguments for the same option
1515

16-
parser(argc, argv); // parses the command line arguments
16+
parser(argc, argv); // parses the command line arguments
1717

18-
auto&& O = parser["optimization"];
1918
// .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
2019
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
2120

22-
auto&& I = parser["include-path"];
2321
// .size() and .count() return the number of given arguments. Without .multi(), their return value is always <= 1.
2422
std::cout << "include paths (" << I.size() << "):\n";
23+
2524
// Here, the non-template .begin() / .end() methods were used. Their value type is po::value,
2625
// which is not a value in itself but contains the desired values as members, i.e. i.string.
2726
for(auto&& i : I)

examples/3 files.cxx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,52 @@
33

44
int main(int argc, char** argv) {
55
po::parser parser;
6-
parser["optimization"] // corresponds to --optimization
7-
.abbreviation('O') // corresponds to -O
6+
auto& O = parser["optimization"] // corresponds to --optimization
7+
.abbreviation('O') // corresponds to -O
88
.description("set the optimization level (default: -O0)")
9-
// the description to be shown when printing the parser
10-
.type(po::u32) // expects an unsigned 32-bit integer
11-
.fallback(0); // if --optimization is not explicitly specified, assume 0
9+
// the description to be shown when printing the parser
10+
.type(po::u32) // expects an unsigned 32-bit integer
11+
.fallback(0); // if --optimization is not explicitly specified, assume 0
1212

13-
parser["include-path"] // corresponds to --include-path
14-
.abbreviation('I') // corresponds to -I
13+
auto& I = parser["include-path"] // corresponds to --include-path
14+
.abbreviation('I') // corresponds to -I
1515
.description("add an include path")
16-
.type(po::string) // expects a string
17-
.multi(); // allows multiple arguments for the same option
16+
// the description to be shown when printing the parser
17+
.type(po::string) // expects a string
18+
.multi(); // allows multiple arguments for the same option
1819

19-
parser["help"] // corresponds to --help
20-
.abbreviation('?') // corresponds to -?
20+
auto& help = parser["help"] // corresponds to --help
21+
.abbreviation('?') // corresponds to -?
2122
.description("print this help screen")
22-
// .type(po::void_) // redundant; default for named parameters
23-
// .single() // redundant; default for named parameters
23+
// the description to be shown when printing the parser
24+
// .type(po::void_) // redundant; default for named parameters
25+
// .single() // redundant; default for named parameters
2426
.callback([&]{ std::cout << parser << '\n'; });
25-
// callbacks get invoked when the option occurs
27+
// callbacks get invoked when the option occurs
2628

27-
parser[""] // the unnamed parameter is used for non-option arguments, i.e. gcc a.c b.c
28-
// .type(po::string) // redundant; default for the unnamed parameter
29-
// .multi() // redundant; default for the unnamed parameter
29+
auto& files = parser[""] // the unnamed parameter is used for non-option arguments as in: gcc a.c b.c
30+
// .type(po::string) // redundant; default for the unnamed parameter
31+
// .multi() // redundant; default for the unnamed parameter
3032
.callback([&](std::string const& x){ std::cout << "processed \'" << x << "\' successfully!\n"; });
31-
// as .get_type() == po::string, the callback may take an std::string
33+
// as .get_type() == po::string, the callback may take an std::string
3234

3335
// parsing returns false if at least one error has occurred
3436
if(!parser(argc, argv)) {
3537
std::cerr << "errors occurred; aborting\n";
3638
return -1;
3739
}
3840
// we don't want to print anything else if the help screen has been displayed
39-
if(parser["help"].size())
41+
if(help.was_set())
4042
return 0;
4143

42-
std::cout << "processed files: " << parser[""].size() << '\n';
44+
std::cout << "processed files: " << files.size() << '\n';
4345

44-
auto&& O = parser["optimization"];
4546
// .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
4647
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
4748

48-
auto&& I = parser["include-path"];
4949
// .size() and .count() return the number of given arguments. Without .multi(), their return value is always <= 1.
5050
std::cout << "include paths (" << I.size() << "):\n";
51+
5152
// Here, the non-template .begin() / .end() methods were used. Their value type is
5253
// po::value, which is not a value in itself but contains the desired values as members, i.e. i.string.
5354
for(auto&& i : I)

examples/4 sum.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
int main(int argc, char** argv) {
66
po::parser parser;
77

8-
auto&& x = parser[""] // the unnamed parameter
8+
auto& x = parser[""] // the unnamed parameter
99
.type(po::f64) // expects 64-bit floating point numbers
1010
.multi() // allows multiple arguments
1111
.fallback(-8, "+.5e2") // if no arguments were provided, assume these as default

examples/5 bind.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(int argc, char** argv) {
2121
.description("add an include path")
2222
.bind(include_paths); // append paths to the vector 'include_paths'
2323

24-
parser["help"] // corresponds to --help
24+
auto& help = parser["help"] // corresponds to --help
2525
.abbreviation('?') // corresponds to -?
2626
.description("print this help screen");
2727

@@ -34,7 +34,7 @@ int main(int argc, char** argv) {
3434
return -1;
3535

3636
// we don't want to print anything else if the help screen has been displayed
37-
if(parser["help"].was_set()) {
37+
if(help.was_set()) {
3838
std::cout << parser << '\n';
3939
return 0;
4040
}

examples/detect_type.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
int main(int argc, char** argv) {
55
po::parser parser;
6-
auto&& o = parser[""];
6+
auto& o = parser[""];
77
parser(argc, argv);
88

99
po::value_type testing_order[] = { po::void_, po::u32, po::u64, po::i32, po::i64, po::f32, po::f64, po::string };

examples/flag.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
int main(int argc, char** argv) {
55
po::parser parser;
6-
auto&& x = parser["x"]; // creates an option with name 'x'
6+
auto& x = parser["x"]; // creates an option with name 'x'
77

88
parser(argc, argv); // parses the command line arguments
99

examples/getopt.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
int main(int argc, char** argv) {
88
po::parser parser;
99

10-
auto&& a = parser["a"];
11-
auto&& b = parser["b"];
12-
auto&& c = parser["c"]
10+
auto& a = parser["a"];
11+
auto& b = parser["b"];
12+
auto& c = parser["c"]
1313
.type(po::string)
1414
.fallback("(null)");
15-
auto&& unnamed = parser[""]
15+
auto& unnamed = parser[""]
1616
.type(po::string)
1717
.multi();
1818

0 commit comments

Comments
 (0)