@@ -96,13 +96,12 @@ The following snippet is the complete source code of a simple program expecting
9696
9797int 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
135134int 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
183181int 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```
229233How the help screen appears:
230234```
231235$ ./files --help
232236Usage:
233237 files.exe [ arguments...] [ options]
234238Available 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```
239243In action:
240244```
@@ -281,10 +285,10 @@ In this example, we will employ already known mechanics but lay the focus on the
281285int 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 }
0 commit comments