@@ -96,13 +96,12 @@ The following snippet is the complete source code of a simple program expecting
96
96
97
97
int main (int argc, char** argv) {
98
98
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
102
102
103
- parser(argc, argv); // parses the command line arguments
103
+ parser(argc, argv); // parses the command line arguments
104
104
105
- auto&& O = parser["optimization"];
106
105
if(!O.available())
107
106
std::cout << "no optimization level set!\n";
108
107
else
@@ -134,26 +133,25 @@ By calling the method `.multi()` we're telling the library to store *all* values
134
133
135
134
int main(int argc, char** argv) {
136
135
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
141
140
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
146
145
147
- parser(argc, argv);
146
+ parser(argc, argv); // parses the command line arguments
148
147
149
- auto&& O = parser["optimization"];
150
148
// .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
151
149
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
152
150
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.
155
152
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,
157
155
// which is not a value in itself but contains the desired values as members, i.e. i.string.
158
156
for(auto&& i : I)
159
157
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
182
180
183
181
int main (int argc, char** argv) {
184
182
po::parser parser;
185
- parser[ "optimization"]
183
+ auto& O = parser[ "optimization"]
186
184
.abbreviation('O')
187
185
.description("set the optimization level (default: -O0)")
188
186
.type(po::u32)
189
187
.fallback(0);
190
188
191
- parser["include-path"]
189
+ auto& I = parser["include-path"]
192
190
.abbreviation('I')
193
191
.description("add an include path")
194
192
.type(po::string)
195
193
.multi();
196
194
197
- parser["help"] // corresponds to --help
198
- .abbreviation('?') // corresponds to -?
195
+ auto& help = parser["help"]
196
+ .abbreviation('?')
199
197
.description("print this help screen")
198
+ // .type(po::void_) // redundant; default for named parameters
199
+ // .single() // redundant; default for named parameters
200
200
.callback([&]{ std::cout << parser << '\n'; });
201
201
// callbacks get invoked when the option occurs
202
202
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
204
204
// .type(po::string) // redundant; default for the unnamed parameter
205
205
// .multi() // redundant; default for the unnamed parameter
206
206
.callback([&](std::string const& x){ std::cout << "processed \'" << x << "\' successfully!\n"; });
@@ -212,29 +212,33 @@ int main(int argc, char** argv) {
212
212
return -1;
213
213
}
214
214
// 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 ())
216
216
return 0;
217
217
218
- std::cout << "processed files: " << parser[""] .size() << '\n';
218
+ std::cout << "processed files: " << files .size() << '\n';
219
219
220
- auto&& O = parser["optimization"];
220
+ // .was_set() reports whether the option was specified by the user or relied on the predefined fallback value.
221
221
std::cout << "optimization level (" << (O.was_set() ? "manual" : "auto") << ") = " << O.get().u32 << '\n';
222
222
223
- auto&& I = parser["include-path"];
223
+ // .size() and .count() return the number of given arguments. Without .multi(), their return value is always <= 1.
224
224
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.
225
228
for(auto&& i : I)
226
229
std::cout << '\t' << i.string << '\n';
227
230
}
231
+
228
232
```
229
233
How the help screen appears:
230
234
```
231
235
$ ./files --help
232
236
Usage:
233
237
files.exe [ arguments...] [ options]
234
238
Available options:
239
+ -O, --optimization set the optimization level (default: -O0)
235
240
-I, --include-path add an include path
236
241
-?, --help print this help screen
237
- -O, --optimization set the optimization level (default: -O0)
238
242
```
239
243
In action:
240
244
```
@@ -281,10 +285,10 @@ In this example, we will employ already known mechanics but lay the focus on the
281
285
int main(int argc, char** argv) {
282
286
po::parser parser;
283
287
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
288
292
.callback([&]{ std::cout << "successfully parsed "; })
289
293
.callback([&](std::string const& x){ std::cout << x; })
290
294
.callback([&]{ std::cout << " which equals "; })
@@ -351,7 +355,7 @@ int main(int argc, char** argv) {
351
355
.description("add an include path")
352
356
.bind(include_paths); // append paths to the vector 'include_paths'
353
357
354
- parser["help"]
358
+ auto& help = parser["help"]
355
359
.abbreviation('?')
356
360
.description("print this help screen");
357
361
@@ -363,7 +367,7 @@ int main(int argc, char** argv) {
363
367
return -1;
364
368
365
369
// 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()) {
367
371
std::cout << parser << '\n';
368
372
return 0;
369
373
}
0 commit comments